I have written an Azure Function that persists data to a Cosmos collection:
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
try {
if (req.body) {
const body: Body = req.body
context.bindings.cosmosDocument = JSON.stringify(body);
context.res = {
status: 200,
body: 'Successfully persisted document'
};
}
} catch (error) {
context.res = {
status: 400,
body: 'Colliding id'
}
}
}
I am handling errors according to the documentation.
My payload looks like this:
{
"shipmentId": "shipmentId",
"timestamp": 1586327829354,
"totalVolume": 1,
"totalWeight": 1,
"packages": [{
"packageId": "courierPackageId",
"shipmentId": "shipmentId",
"weight:": 1000,
"dimensions": {
"length": 100,
"width": 100,
"height": 100
}
}]
}
However, when I try to persist something that will have a colliding id in the database, that error is not caught.
I see the following error:
Entity with the specified id already exists in the system
And when I call this trigger through Postman, I get status 500 and no message body.
In this issue on Github, a solution is suggested for C#.
How can I handle this exception and give the API user a meaningful message? What is the usual approach when building APIs in Azure Function and handling errors produced by output bindings?
