Siva SM
Siva SM

Reputation: 37

JavaScript: Azure function blob binding handling exceptions

Hi I have created a azure function httptrigger to read a blob from blob storage with blob input bindings.

below is the function.json:

{
  "disabled": false,
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req"
    },
    {
      "name" : "blobContent",
      "type": "blob",
      "direction": "in",
      "path": "containerName/{id}.{extn}",
       "connection": "AzureWebJobsStorage"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    }
  ]
}

and the index.js file is :

module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.id || (req.body && req.body.id)) {   
        context.res = {
            body : {'data' : context.bindings.blobContent},
            headers : {'Content-type': 'application/xml"'}
        }
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a object/chuck id on the query string or in the request body"
        };
    }
    context.done(null,context.res);
};

I am using both get and post method to call the httptrigger. Since i am using blob input binding, the content is retrieved before processing index.js. With this, i couldn't validate whether the API called with id and extn. Is there a way to handle the exception and give a message back to the API caller to pass the necessary parameters. Thanks in advance.

Upvotes: 1

Views: 790

Answers (1)

Connor McMahon
Connor McMahon

Reputation: 1358

So Functions does have some way of doing this, called Function Filters. This feature allows you to write methods that are called before or after the job function is ran (Invocation Filters), or that are invoked whenever the code encounters an exception in the functions runtime (Exception Filters). You could write an exception filter that catches an exception when the input binding fails and that would accomplish what you want.

Unfortunately, at the time of writing this answer, Function Filters are only compatible with pre-compiled C# functions. There is currently this issue tracking this feature's addition into other scenarios.

Upvotes: 3

Related Questions