Azure function success status is true even when result code is 500

Viewed 1665

I've got an azure function that contains some logic with a pretty basic try catch (code shortened for brevity)

    try {
        // do logic here that can potentially fail
    } catch (ex) {
        context.log(`Log details about the exception: ${ex.message}`);
        context.res = {
            status: 500,
            headers: {
                "Content-Type": "application/json" 
            },
            body: {
                message: ex.message
            }
        };

        return;
    }

So in the event of an exception during that logic I handle the exception and gracefully return in the http response and terminate the app. I would expect that azure functions would consider 500 status code's as failure (considering they're internal server errors), but my function is still considered to have successfully executed. Considering this a low volume function with somewhat high importance for it's context, I was to be notified about all failures.

What's the intelligent thing to do error? Do I throw an exception here instead of simply returning?

1 Answers

This is the internal behavior of azure function status check.

1.If your code handles the error by using try catch block and return 500 manually, azure function considers it as success.

2.But if the error is not handled by the code using try catch block, it will be marked as failed.

Refer to this issue for more details.

Related