I'd like to know why my function is succeeding, even though I'm purposely throwing an exception inside the function. I created a new timer function and all I did is added a try/catch block and an always throwing error:
module.exports = async function (context, myTimer) {
try {
if (myTimer.isPastDue) {
context.log('Node is running late!');
}
context.log('TEST');
throw 'Error occurred';
} catch (err) {
context.log(err);
}
};
After deploying the above function to Azure and manually triggering it, will result in a succeeded function execution and the logs showing 'TEST' but not 'Node is running late!':
2022-04-08T13:32:30Z [Information] TEST
2022-04-08T13:32:30Z [Information] Executed 'Functions.TEST' (Succeeded, Id=..., Duration=36ms)
the docs only state that I should use a try/catch at the highest level of the function. I couldn't find any information on why this function would succeed with an error happening in execution.
Any help will be appreciated, many thanks in advance!
CodePudding user response:
It does not fail because you are catching the error, meaning it is not surfaced, it expects you to actively decide what to do, log and re throw the exception is one option, log only is another it really depends on your business needs.
You need to either remove the try/catch blocks or add a throw to your catch similar to this
catch (err) {
context.log(err);
throw;
}