I have an Azure function containing some logic with a basic try-catch structure (code shortened).
try {
// perform logic here that may fail
} catch (ex) {
context.log(`Logging exception details: ${ex.message}`);
context.res = {
status: 500,
headers: {
"Content-Type": "application/json"
},
body: {
message: ex.message
}
};
return;
}
In case of an exception during the logic execution, I handle it gracefully by returning an HTTP response and terminating the application. Although Azure Functions typically treat a 500 status code as a failure (internal server error), my function is still marked as successfully executed. As this function is important despite being low volume, I want to receive notifications for all failures.
What would be the best approach to handle errors intelligently? Should I throw an exception instead of just returning?