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.isRaw
to 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"
}
]
}