JavaScript: Azure function blob binding handling exceptions

邮差的信 提交于 2019-12-24 07:06:00

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/49700893/javascript-azure-function-blob-binding-handling-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!