Azure Function automatic retry on failure UnhandledPromiseRejectionWarning





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















const fetch = require('node-fetch');
let url = 'something.com';

module.exports = function(context) {
let a = fetch(url)

a.then(res => {
if(res.status!=200) throw new Error(res.statusText)
else{
context.done(null, res.body);
}
});
a.catch(err => {
console.log(err)
throw new Error(err)
});

};


I have a durable function that calls an activity function like above. I have set automatic retry on failure on this activity function. To retry the function needs to get an error.



So In get request I want to throw an error when i get response like 404 or something similar. But when i throw from catch block i get an error like below




UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled
with .catch().




function pauses there and stops execution.I have to manually stop and start the execution. How can i handle this so that the function retries?










share|improve this question































    0















    const fetch = require('node-fetch');
    let url = 'something.com';

    module.exports = function(context) {
    let a = fetch(url)

    a.then(res => {
    if(res.status!=200) throw new Error(res.statusText)
    else{
    context.done(null, res.body);
    }
    });
    a.catch(err => {
    console.log(err)
    throw new Error(err)
    });

    };


    I have a durable function that calls an activity function like above. I have set automatic retry on failure on this activity function. To retry the function needs to get an error.



    So In get request I want to throw an error when i get response like 404 or something similar. But when i throw from catch block i get an error like below




    UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
    error originated either by throwing inside of an async function
    without a catch block, or by rejecting a promise which was not handled
    with .catch().




    function pauses there and stops execution.I have to manually stop and start the execution. How can i handle this so that the function retries?










    share|improve this question



























      0












      0








      0








      const fetch = require('node-fetch');
      let url = 'something.com';

      module.exports = function(context) {
      let a = fetch(url)

      a.then(res => {
      if(res.status!=200) throw new Error(res.statusText)
      else{
      context.done(null, res.body);
      }
      });
      a.catch(err => {
      console.log(err)
      throw new Error(err)
      });

      };


      I have a durable function that calls an activity function like above. I have set automatic retry on failure on this activity function. To retry the function needs to get an error.



      So In get request I want to throw an error when i get response like 404 or something similar. But when i throw from catch block i get an error like below




      UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
      error originated either by throwing inside of an async function
      without a catch block, or by rejecting a promise which was not handled
      with .catch().




      function pauses there and stops execution.I have to manually stop and start the execution. How can i handle this so that the function retries?










      share|improve this question
















      const fetch = require('node-fetch');
      let url = 'something.com';

      module.exports = function(context) {
      let a = fetch(url)

      a.then(res => {
      if(res.status!=200) throw new Error(res.statusText)
      else{
      context.done(null, res.body);
      }
      });
      a.catch(err => {
      console.log(err)
      throw new Error(err)
      });

      };


      I have a durable function that calls an activity function like above. I have set automatic retry on failure on this activity function. To retry the function needs to get an error.



      So In get request I want to throw an error when i get response like 404 or something similar. But when i throw from catch block i get an error like below




      UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
      error originated either by throwing inside of an async function
      without a catch block, or by rejecting a promise which was not handled
      with .catch().




      function pauses there and stops execution.I have to manually stop and start the execution. How can i handle this so that the function retries?







      node.js promise azure-functions node-fetch azure-durable-functions






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 16:42







      Nafis Islam

















      asked Nov 26 '18 at 16:37









      Nafis IslamNafis Islam

      136112




      136112
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Your code branches.



          Ignoring the detail, what you have is :



          let a = <Promise>; // root
          a.then(...); // branch_1
          a.catch(...); // branch_2


          So whereas you catch errors arising in a, any error arising in branch 1 will be uncaught. Hence the warning



          Compare that with :



          let a = <Promise>; // root
          a.then(...).catch(...); // branch


          or



          <Promise>.then(...).catch(...); // no assignment necessary


          So, you might write :



          module.exports = function(context) {
          return fetch(url)
          .then(res => {
          if(res.status!=200) {
          throw new Error(res.statusText);
          } else {
          context.done(null, res.body);
          }
          })
          .catch(err => {
          console.log(err)
          throw new Error(err)
          });
          };


          Alternatively, depending on the required division of responsibilities between module and caller(s) ...



          module.exports = function(context) {
          return fetch(url)
          .then(res => {
          if(res.status!=200) {
          throw new Error(res.statusText);
          } else {
          return res;
          }
          });
          };


          ... and call .context.done(null, res.body); in a .then() callback in the caller.



          In both cases, with return included, then the caller will need to catch errors otherwise you will again get an unhandled error warning.






          share|improve this answer
























          • I tried the non branching promise before. When the catch block throws the error i get unhandled error warning. With out throwing error my function will not re-start. Is there no way for the caller to throw error with out catching it? @Roamer-1888

            – Nafis Islam
            Nov 26 '18 at 17:27






          • 1





            If you don't return Promise from the fnction, then simply don't re-throw the error; it will be caught and stay caught. If you return Promise from the function then re-throw the error in the catch block, and heed my last paragraph.

            – Roamer-1888
            Nov 26 '18 at 17:36





















          0














          Found that with the use of async/await this problem goes away and the function re-try after exception is thrown.



          const fetch = require('node-fetch');
          let url = 'something.com';

          module.exports = async function(context) {

          let res = await fetch(url)

          if(res.status!=200) throw new Error(res.statusText);
          else return res.body;

          };





          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%2f53485452%2fazure-function-automatic-retry-on-failure-unhandledpromiserejectionwarning%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Your code branches.



            Ignoring the detail, what you have is :



            let a = <Promise>; // root
            a.then(...); // branch_1
            a.catch(...); // branch_2


            So whereas you catch errors arising in a, any error arising in branch 1 will be uncaught. Hence the warning



            Compare that with :



            let a = <Promise>; // root
            a.then(...).catch(...); // branch


            or



            <Promise>.then(...).catch(...); // no assignment necessary


            So, you might write :



            module.exports = function(context) {
            return fetch(url)
            .then(res => {
            if(res.status!=200) {
            throw new Error(res.statusText);
            } else {
            context.done(null, res.body);
            }
            })
            .catch(err => {
            console.log(err)
            throw new Error(err)
            });
            };


            Alternatively, depending on the required division of responsibilities between module and caller(s) ...



            module.exports = function(context) {
            return fetch(url)
            .then(res => {
            if(res.status!=200) {
            throw new Error(res.statusText);
            } else {
            return res;
            }
            });
            };


            ... and call .context.done(null, res.body); in a .then() callback in the caller.



            In both cases, with return included, then the caller will need to catch errors otherwise you will again get an unhandled error warning.






            share|improve this answer
























            • I tried the non branching promise before. When the catch block throws the error i get unhandled error warning. With out throwing error my function will not re-start. Is there no way for the caller to throw error with out catching it? @Roamer-1888

              – Nafis Islam
              Nov 26 '18 at 17:27






            • 1





              If you don't return Promise from the fnction, then simply don't re-throw the error; it will be caught and stay caught. If you return Promise from the function then re-throw the error in the catch block, and heed my last paragraph.

              – Roamer-1888
              Nov 26 '18 at 17:36


















            1














            Your code branches.



            Ignoring the detail, what you have is :



            let a = <Promise>; // root
            a.then(...); // branch_1
            a.catch(...); // branch_2


            So whereas you catch errors arising in a, any error arising in branch 1 will be uncaught. Hence the warning



            Compare that with :



            let a = <Promise>; // root
            a.then(...).catch(...); // branch


            or



            <Promise>.then(...).catch(...); // no assignment necessary


            So, you might write :



            module.exports = function(context) {
            return fetch(url)
            .then(res => {
            if(res.status!=200) {
            throw new Error(res.statusText);
            } else {
            context.done(null, res.body);
            }
            })
            .catch(err => {
            console.log(err)
            throw new Error(err)
            });
            };


            Alternatively, depending on the required division of responsibilities between module and caller(s) ...



            module.exports = function(context) {
            return fetch(url)
            .then(res => {
            if(res.status!=200) {
            throw new Error(res.statusText);
            } else {
            return res;
            }
            });
            };


            ... and call .context.done(null, res.body); in a .then() callback in the caller.



            In both cases, with return included, then the caller will need to catch errors otherwise you will again get an unhandled error warning.






            share|improve this answer
























            • I tried the non branching promise before. When the catch block throws the error i get unhandled error warning. With out throwing error my function will not re-start. Is there no way for the caller to throw error with out catching it? @Roamer-1888

              – Nafis Islam
              Nov 26 '18 at 17:27






            • 1





              If you don't return Promise from the fnction, then simply don't re-throw the error; it will be caught and stay caught. If you return Promise from the function then re-throw the error in the catch block, and heed my last paragraph.

              – Roamer-1888
              Nov 26 '18 at 17:36
















            1












            1








            1







            Your code branches.



            Ignoring the detail, what you have is :



            let a = <Promise>; // root
            a.then(...); // branch_1
            a.catch(...); // branch_2


            So whereas you catch errors arising in a, any error arising in branch 1 will be uncaught. Hence the warning



            Compare that with :



            let a = <Promise>; // root
            a.then(...).catch(...); // branch


            or



            <Promise>.then(...).catch(...); // no assignment necessary


            So, you might write :



            module.exports = function(context) {
            return fetch(url)
            .then(res => {
            if(res.status!=200) {
            throw new Error(res.statusText);
            } else {
            context.done(null, res.body);
            }
            })
            .catch(err => {
            console.log(err)
            throw new Error(err)
            });
            };


            Alternatively, depending on the required division of responsibilities between module and caller(s) ...



            module.exports = function(context) {
            return fetch(url)
            .then(res => {
            if(res.status!=200) {
            throw new Error(res.statusText);
            } else {
            return res;
            }
            });
            };


            ... and call .context.done(null, res.body); in a .then() callback in the caller.



            In both cases, with return included, then the caller will need to catch errors otherwise you will again get an unhandled error warning.






            share|improve this answer













            Your code branches.



            Ignoring the detail, what you have is :



            let a = <Promise>; // root
            a.then(...); // branch_1
            a.catch(...); // branch_2


            So whereas you catch errors arising in a, any error arising in branch 1 will be uncaught. Hence the warning



            Compare that with :



            let a = <Promise>; // root
            a.then(...).catch(...); // branch


            or



            <Promise>.then(...).catch(...); // no assignment necessary


            So, you might write :



            module.exports = function(context) {
            return fetch(url)
            .then(res => {
            if(res.status!=200) {
            throw new Error(res.statusText);
            } else {
            context.done(null, res.body);
            }
            })
            .catch(err => {
            console.log(err)
            throw new Error(err)
            });
            };


            Alternatively, depending on the required division of responsibilities between module and caller(s) ...



            module.exports = function(context) {
            return fetch(url)
            .then(res => {
            if(res.status!=200) {
            throw new Error(res.statusText);
            } else {
            return res;
            }
            });
            };


            ... and call .context.done(null, res.body); in a .then() callback in the caller.



            In both cases, with return included, then the caller will need to catch errors otherwise you will again get an unhandled error warning.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 26 '18 at 17:11









            Roamer-1888Roamer-1888

            15.6k42339




            15.6k42339













            • I tried the non branching promise before. When the catch block throws the error i get unhandled error warning. With out throwing error my function will not re-start. Is there no way for the caller to throw error with out catching it? @Roamer-1888

              – Nafis Islam
              Nov 26 '18 at 17:27






            • 1





              If you don't return Promise from the fnction, then simply don't re-throw the error; it will be caught and stay caught. If you return Promise from the function then re-throw the error in the catch block, and heed my last paragraph.

              – Roamer-1888
              Nov 26 '18 at 17:36





















            • I tried the non branching promise before. When the catch block throws the error i get unhandled error warning. With out throwing error my function will not re-start. Is there no way for the caller to throw error with out catching it? @Roamer-1888

              – Nafis Islam
              Nov 26 '18 at 17:27






            • 1





              If you don't return Promise from the fnction, then simply don't re-throw the error; it will be caught and stay caught. If you return Promise from the function then re-throw the error in the catch block, and heed my last paragraph.

              – Roamer-1888
              Nov 26 '18 at 17:36



















            I tried the non branching promise before. When the catch block throws the error i get unhandled error warning. With out throwing error my function will not re-start. Is there no way for the caller to throw error with out catching it? @Roamer-1888

            – Nafis Islam
            Nov 26 '18 at 17:27





            I tried the non branching promise before. When the catch block throws the error i get unhandled error warning. With out throwing error my function will not re-start. Is there no way for the caller to throw error with out catching it? @Roamer-1888

            – Nafis Islam
            Nov 26 '18 at 17:27




            1




            1





            If you don't return Promise from the fnction, then simply don't re-throw the error; it will be caught and stay caught. If you return Promise from the function then re-throw the error in the catch block, and heed my last paragraph.

            – Roamer-1888
            Nov 26 '18 at 17:36







            If you don't return Promise from the fnction, then simply don't re-throw the error; it will be caught and stay caught. If you return Promise from the function then re-throw the error in the catch block, and heed my last paragraph.

            – Roamer-1888
            Nov 26 '18 at 17:36















            0














            Found that with the use of async/await this problem goes away and the function re-try after exception is thrown.



            const fetch = require('node-fetch');
            let url = 'something.com';

            module.exports = async function(context) {

            let res = await fetch(url)

            if(res.status!=200) throw new Error(res.statusText);
            else return res.body;

            };





            share|improve this answer




























              0














              Found that with the use of async/await this problem goes away and the function re-try after exception is thrown.



              const fetch = require('node-fetch');
              let url = 'something.com';

              module.exports = async function(context) {

              let res = await fetch(url)

              if(res.status!=200) throw new Error(res.statusText);
              else return res.body;

              };





              share|improve this answer


























                0












                0








                0







                Found that with the use of async/await this problem goes away and the function re-try after exception is thrown.



                const fetch = require('node-fetch');
                let url = 'something.com';

                module.exports = async function(context) {

                let res = await fetch(url)

                if(res.status!=200) throw new Error(res.statusText);
                else return res.body;

                };





                share|improve this answer













                Found that with the use of async/await this problem goes away and the function re-try after exception is thrown.



                const fetch = require('node-fetch');
                let url = 'something.com';

                module.exports = async function(context) {

                let res = await fetch(url)

                if(res.status!=200) throw new Error(res.statusText);
                else return res.body;

                };






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 4 '18 at 6:25









                Nafis IslamNafis Islam

                136112




                136112






























                    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%2f53485452%2fazure-function-automatic-retry-on-failure-unhandledpromiserejectionwarning%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

                    Wiesbaden

                    Marschland

                    Dieringhausen