Azure turns function 401 unauthorised HTTP response into 500 internal server error

Alistair Young 56 Reputation points
2023-09-01T11:19:25.8133333+00:00

I have an Azure function that calls an API and when running locally it works when the response from the API is 401 unauthorised. The same function when running in Azure doesn't get as far as handling the 401 as Azure takes over and raises 500 internal server error instead.

I can see this in application insights:

2023-09-01T11:01:59Z [Information] End processing HTTP request after 173.1307ms - 401

Running locally, the correct error response JSON from the function is displayed. Running in Azure nothing is displayed as Azure has taken over and thrown 500 internal server error instead of allowing the 401 to propagate to the handler code in the function.

Application insights shows this, which is not correct as it's 401 and not 500:

Response code 500 Successful request false

I checked and the HTTP status code is 401. Azure is throwing 500 internal server error instead of allowing the function deal with it.

Is this normal for Azure?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MayankBargali-MSFT 70,936 Reputation points Moderator
    2023-09-01T11:46:57.08+00:00

    @Alistair Young Thanks for reaching out.

    I will suggest you to review the Diagnose and solve problems blade on your function app to know more details on the 500 error. Looks like there might be some dependency missing or your have configure some application setting in your local.setting.json which is not configured in your function app application setting blade which would cause this behavior. There could be multiple other reasons for the 500 error but reviewing the application insights logs/ diagnose and solve problem blade will give you more insights on the issue and the steps to resolve the issue if there are any.

    In case if you need any assistance then please let me know.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.