Is it good to make api call made in try-catch block or outside, in redux-saga?Fail try/catch block reading a csvHow to test API request failures with Redux Saga?Asynchronous api calls with redux-sagatesting try-catch in redux-sagaHow to catch exceptions in an NSBlockOperation while in unit tests?Redux-Saga takeEvery not calling sagaTest Redux-Saga 'yield call'Redux saga channel blocking inconsistentlyHow to write C# unit test for proper code coverage in try catch blockCalling component methods after redux-saga API call completion: use componentDidUpdate or pass the method to the action creator?

Won 50K! Now what should I do with it

3D-Plot with an inequality condition for parameter values

Do native speakers use ZVE or CPU?

Concatenation using + and += operator in Python

Add region constraint to Graphics

Replacing URI when using dynamic hosts in Nginx reverse proxy

Adding a vertical line at the right end of the horizontal line in frac

Why use null function instead of == []

How are "soeben" and "eben" different from one another?

Would letting a multiclass character rebuild their character to be single-classed be game-breaking?

Does entangle require vegetation?

Book or series about stones and a magician named Gwydion

Can a pizza stone be fixed after soap has been used to clean it?

Why is "dark" an adverb in this sentence?

Are there any double stars that I can actually see orbit each other?

Why do legislative committees exist?

Is this a Lost Mine of Phandelver Plot Hole?

Aborting 'wrong username' logins

Is killing off one of my queer characters homophobic?

Too many spies!

Absconding a company after 1st day of joining

Deep Learning based time series forecasting

Ezek. 24:1-2, "Again in the ninth year, in the tenth month, in the tenth day of the month, ...." Which month was the tenth month?

Doing research in academia and not liking competition



Is it good to make api call made in try-catch block or outside, in redux-saga?


Fail try/catch block reading a csvHow to test API request failures with Redux Saga?Asynchronous api calls with redux-sagatesting try-catch in redux-sagaHow to catch exceptions in an NSBlockOperation while in unit tests?Redux-Saga takeEvery not calling sagaTest Redux-Saga 'yield call'Redux saga channel blocking inconsistentlyHow to write C# unit test for proper code coverage in try catch blockCalling component methods after redux-saga API call completion: use componentDidUpdate or pass the method to the action creator?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have a following function in my saga.ts file :



export function* getProductsList(action) 
yield put(productsListAttempt(true));

const productsList = yield api.getProductsList();

try

yield put(productsListSuccess(productsList));

catch

yield put(productsListError('Error occured'));




I am trying to write a unit test for this function. The problem I am encountering with this is, whenever I execute my test for FAILURE of the function it does not pass well. It never gives result as per the catch block. But when I put this line: const productsList = yield api.getProductsList(); in the try block then the test pass as per expectation.



-- Is it really good to put api call outside try block or not? Why so ?










share|improve this question






















  • In many of the tutorials which I referred to learn Redux-saga, the api call was in the Try block. But at my work, my seniors has put it outside the try-catch block

    – NRJ
    Mar 21 at 15:54











  • Not sure why you are not sure. Better to put it in the try so you can test it but more importantly so you can react in the real usage scenario.

    – Gabriele Petrioli
    Mar 21 at 15:54











  • @GabrielePetrioli other than the unit test scenarios, it does not cause any problem.

    – NRJ
    Mar 21 at 15:58











  • perhaps there is global error handling in the app and so they do not want the error handled locally.

    – Gabriele Petrioli
    Mar 21 at 16:02











  • @GabrielePetrioli when it make a call to the function productsListError('Error occurred'), they set this string message to the state in reducer and then show it on the view. Other than that there is no error handling logic.

    – NRJ
    Mar 21 at 16:15


















1















I have a following function in my saga.ts file :



export function* getProductsList(action) 
yield put(productsListAttempt(true));

const productsList = yield api.getProductsList();

try

yield put(productsListSuccess(productsList));

catch

yield put(productsListError('Error occured'));




I am trying to write a unit test for this function. The problem I am encountering with this is, whenever I execute my test for FAILURE of the function it does not pass well. It never gives result as per the catch block. But when I put this line: const productsList = yield api.getProductsList(); in the try block then the test pass as per expectation.



-- Is it really good to put api call outside try block or not? Why so ?










share|improve this question






















  • In many of the tutorials which I referred to learn Redux-saga, the api call was in the Try block. But at my work, my seniors has put it outside the try-catch block

    – NRJ
    Mar 21 at 15:54











  • Not sure why you are not sure. Better to put it in the try so you can test it but more importantly so you can react in the real usage scenario.

    – Gabriele Petrioli
    Mar 21 at 15:54











  • @GabrielePetrioli other than the unit test scenarios, it does not cause any problem.

    – NRJ
    Mar 21 at 15:58











  • perhaps there is global error handling in the app and so they do not want the error handled locally.

    – Gabriele Petrioli
    Mar 21 at 16:02











  • @GabrielePetrioli when it make a call to the function productsListError('Error occurred'), they set this string message to the state in reducer and then show it on the view. Other than that there is no error handling logic.

    – NRJ
    Mar 21 at 16:15














1












1








1








I have a following function in my saga.ts file :



export function* getProductsList(action) 
yield put(productsListAttempt(true));

const productsList = yield api.getProductsList();

try

yield put(productsListSuccess(productsList));

catch

yield put(productsListError('Error occured'));




I am trying to write a unit test for this function. The problem I am encountering with this is, whenever I execute my test for FAILURE of the function it does not pass well. It never gives result as per the catch block. But when I put this line: const productsList = yield api.getProductsList(); in the try block then the test pass as per expectation.



-- Is it really good to put api call outside try block or not? Why so ?










share|improve this question














I have a following function in my saga.ts file :



export function* getProductsList(action) 
yield put(productsListAttempt(true));

const productsList = yield api.getProductsList();

try

yield put(productsListSuccess(productsList));

catch

yield put(productsListError('Error occured'));




I am trying to write a unit test for this function. The problem I am encountering with this is, whenever I execute my test for FAILURE of the function it does not pass well. It never gives result as per the catch block. But when I put this line: const productsList = yield api.getProductsList(); in the try block then the test pass as per expectation.



-- Is it really good to put api call outside try block or not? Why so ?







reactjs unit-testing redux-saga






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 21 at 15:52









NRJNRJ

351 silver badge6 bronze badges




351 silver badge6 bronze badges












  • In many of the tutorials which I referred to learn Redux-saga, the api call was in the Try block. But at my work, my seniors has put it outside the try-catch block

    – NRJ
    Mar 21 at 15:54











  • Not sure why you are not sure. Better to put it in the try so you can test it but more importantly so you can react in the real usage scenario.

    – Gabriele Petrioli
    Mar 21 at 15:54











  • @GabrielePetrioli other than the unit test scenarios, it does not cause any problem.

    – NRJ
    Mar 21 at 15:58











  • perhaps there is global error handling in the app and so they do not want the error handled locally.

    – Gabriele Petrioli
    Mar 21 at 16:02











  • @GabrielePetrioli when it make a call to the function productsListError('Error occurred'), they set this string message to the state in reducer and then show it on the view. Other than that there is no error handling logic.

    – NRJ
    Mar 21 at 16:15


















  • In many of the tutorials which I referred to learn Redux-saga, the api call was in the Try block. But at my work, my seniors has put it outside the try-catch block

    – NRJ
    Mar 21 at 15:54











  • Not sure why you are not sure. Better to put it in the try so you can test it but more importantly so you can react in the real usage scenario.

    – Gabriele Petrioli
    Mar 21 at 15:54











  • @GabrielePetrioli other than the unit test scenarios, it does not cause any problem.

    – NRJ
    Mar 21 at 15:58











  • perhaps there is global error handling in the app and so they do not want the error handled locally.

    – Gabriele Petrioli
    Mar 21 at 16:02











  • @GabrielePetrioli when it make a call to the function productsListError('Error occurred'), they set this string message to the state in reducer and then show it on the view. Other than that there is no error handling logic.

    – NRJ
    Mar 21 at 16:15

















In many of the tutorials which I referred to learn Redux-saga, the api call was in the Try block. But at my work, my seniors has put it outside the try-catch block

– NRJ
Mar 21 at 15:54





In many of the tutorials which I referred to learn Redux-saga, the api call was in the Try block. But at my work, my seniors has put it outside the try-catch block

– NRJ
Mar 21 at 15:54













Not sure why you are not sure. Better to put it in the try so you can test it but more importantly so you can react in the real usage scenario.

– Gabriele Petrioli
Mar 21 at 15:54





Not sure why you are not sure. Better to put it in the try so you can test it but more importantly so you can react in the real usage scenario.

– Gabriele Petrioli
Mar 21 at 15:54













@GabrielePetrioli other than the unit test scenarios, it does not cause any problem.

– NRJ
Mar 21 at 15:58





@GabrielePetrioli other than the unit test scenarios, it does not cause any problem.

– NRJ
Mar 21 at 15:58













perhaps there is global error handling in the app and so they do not want the error handled locally.

– Gabriele Petrioli
Mar 21 at 16:02





perhaps there is global error handling in the app and so they do not want the error handled locally.

– Gabriele Petrioli
Mar 21 at 16:02













@GabrielePetrioli when it make a call to the function productsListError('Error occurred'), they set this string message to the state in reducer and then show it on the view. Other than that there is no error handling logic.

– NRJ
Mar 21 at 16:15






@GabrielePetrioli when it make a call to the function productsListError('Error occurred'), they set this string message to the state in reducer and then show it on the view. Other than that there is no error handling logic.

– NRJ
Mar 21 at 16:15













1 Answer
1






active

oldest

votes


















0














There's nothing wrong putting the call inside a try/catch block, I do the same in my projects and it's fine because the call can fail and the try/catch let you, by definition, manage this error case 😉






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55284383%2fis-it-good-to-make-api-call-made-in-try-catch-block-or-outside-in-redux-saga%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    There's nothing wrong putting the call inside a try/catch block, I do the same in my projects and it's fine because the call can fail and the try/catch let you, by definition, manage this error case 😉






    share|improve this answer



























      0














      There's nothing wrong putting the call inside a try/catch block, I do the same in my projects and it's fine because the call can fail and the try/catch let you, by definition, manage this error case 😉






      share|improve this answer

























        0












        0








        0







        There's nothing wrong putting the call inside a try/catch block, I do the same in my projects and it's fine because the call can fail and the try/catch let you, by definition, manage this error case 😉






        share|improve this answer













        There's nothing wrong putting the call inside a try/catch block, I do the same in my projects and it's fine because the call can fail and the try/catch let you, by definition, manage this error case 😉







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 6:40









        NoriSteNoriSte

        1,4801 gold badge10 silver badges9 bronze badges




        1,4801 gold badge10 silver badges9 bronze badges


















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55284383%2fis-it-good-to-make-api-call-made-in-try-catch-block-or-outside-in-redux-saga%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript