2

I have an Azure Function App v2 in NodeJS that returns status 500 even if I override the property res.status. The code is as follows:

module.exports = async (context, req) => {

...

try {
        const data = await myfunc(body)
        context.res = {
            status: 200,
            body: data,
            headers: {
                'Content-Type': 'application/json'
            }
        }
    } catch (err) {
        context.log.error(err)
        if (err instanceof NotFoundException) {
            context.res = {
                status: 204,
                body: 'No answer found.'
            }
        } else {
            context.res = {
                status: 500,
                body: err.message
            }
        }
    }
}

However, when myfunc throws an exception, the return status is always 500 regardless of the type of the exception. So when a NotFoundException is thrown in this block, I get a status 204 when running locally, but 500 when running in production.

I've tried putting context.log calls just before setting the status to 204. The log appears normally, but the status is still 500, and there is no body.

I've also tried setting context.res.isRawto true, but it didn't work.

Both local and remote projects are running the same stack (Node 12/AzureFunction v2, more specifically the image mcr.microsoft.com/azure-functions/node:2.0, which runs node 10.0).

Is there anything different I'm missing about the way Azure Functions deal with error handling? How come I override the status property to 204, but, still, in the Azure Runtime, it returns 500? And more importantly, how am I facing two completely different behaviours running code in supposedly the same runtime (v2 node10) ?

My function.json:

{
  "disabled": false,
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
phramos07
  • 136
  • 1
  • 8
  • Are you sure the condition in the catch is correct? Since there is no response body, at present you have nothing to confirm which path of execution was followed. Add more tracing code or debug to prove it is really hitting the code that sets 204. – Reg Edit Mar 20 '20 at 22:45
  • I know the example does not show that, but I literally logged a line before assigning to `res` in both paths of the branch and Azure functions runtime (in production) yielded 500 whilst my local container (from the same image version) yields 204. I can't figure this out. – phramos07 Mar 25 '20 at 22:28

0 Answers0