找不到blob文件时的FunctionInvocationException

时间:2017-10-03 20:26:03

标签: azure azure-functions

我正在创建一个在我们的存储中读取自定义文件的函数。 function.json与此类似:

{
  "disabled": false,
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "route": "resource/{code}",
      "methods": [
        "get"
      ]
    },
    {
      "type": "blob",
      "name": "item",
      "path": "data/20171003/{code}.json",
      "connection": "blog_STORAGE",
      "direction": "in"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

在postman中,如果我输入现有文件,它将正确调用该函数。如果文件不存在,另一方面,azure运行时将抛出错误:

  

{       “id”:“4ef77dcd-f8ed-4327-9447-969da6b328a9”,       “requestId”:“0a9874dd-6a7b-4717-ae9f-c3c1e3bb16c8”,       “statusCode”:500,       “errorCode”:0,       “message”:“执行函数时出现异常:Functions.func_ativos - >Valornãopodeser nulo。\ r \ nNome do   parâmetro:stream“,       “errorDetails”:“Microsoft.Azure.WebJobs.Host.FunctionInvocationException:Exception   执行函数时:Functions.func_ativos --->   System.ArgumentNullException:Valornãopodeser nulo。\ r \ nNome do   parâmetro:stream \ r \ n在System.IO.StreamReader..ctor(Stream   流,编码编码,布尔值   detectEncodingFromByteOrderMarks,Int32 bufferSize,Boolean   leaveOpen)\ r \ n在System.IO.StreamReader..ctor(Stream stream)\ r \ n
  在   Microsoft.Azure.WebJobs.Script.Binding.FunctionBinding.ConvertStreamToValue(流   stream,DataType dataType,Object&转换)\ r \ n在异步   Microsoft.Azure.WebJobs.Script.Binding.FunctionBinding.BindStreamAsync(BindingContext中   上下文,FileAccess访问)\ r \ n在async   Microsoft.Azure.WebJobs.Script.Binding.ExtensionBinding.BindAsync(BindingContext中   上下文\ r \ n在异步   Microsoft.Azure.WebJobs.Script.Description.NodeFunctionInvoker.ProcessInputBindingsAsync(粘结剂   binder,Dictionary 2 executionContext,Dictionary 2 bindingData)\ r \ n
  在async   Microsoft.Azure.WebJobs.Script.Description.NodeFunctionInvoker.InvokeCore(对象[]   参数,FunctionInvocationContext上下文)\ r \ n在async中   Microsoft.Azure.WebJobs.Script.Description.FunctionInvokerBase.Invoke(对象[]   参数)\ r \ n在异步   Microsoft.Azure.WebJobs.Host.Executors.VoidTaskMethodInvoker 2.InvokeAsync[TReflected,TReturnType](TReflected instance,Object[] arguments)\r\n at async Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker 2.InvokeAsync [TReflected,TReturnValue](对象[]   参数)\ r \ n在异步   Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker   invoker,ParameterHelper parameterHelper,CancellationTokenSource   timeoutTokenSource,CancellationTokenSource   functionCancellationTokenSource,Boolean throwOnTimeout,TimeSpan   timerInterval,IFunctionInstance实例)\ r \ n在async中   Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstance   实例,ParameterHelper parameterHelper,TraceWriter   traceWriter,ILogger logger,CancellationTokenSource   functionCancellationTokenSource)\ r \ n在异步   Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)\ r \ n   在async   Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)   \ r \ n内部异常结束\ r \ n at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(个)\ r \ n
  在async   Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??)\ r \ n   在async   Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryExecuteAsync(IFunctionInstance   functionInstance,CancellationToken cancellationToken)\ r \ n at   Microsoft.Azure.WebJobs.Host.Executors.ExceptionDispatchInfoDelayedException.Throw(个)\ r \ n   异步Microsoft.Azure.WebJobs.JobHost.CallAsync(??)\ r \ n atync   Microsoft.Azure.WebJobs.Script.ScriptHost.CallAsync(字符串   方法,字典2 arguments,CancellationToken cancellationToken)\r\n at async Microsoft.Azure.WebJobs.Script.WebHost.WebScriptHostManager.HandleRequestAsync(FunctionDescriptor function,HttpRequestMessage request,CancellationToken cancellationToken)\r\n at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.ProcessRequestAsync(HttpRequestMessage request,FunctionDescriptor function,CancellationToken cancellationToken)\r\n at async Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.<>c__DisplayClass3_0.<ExecuteAsync>b__0(??)\r\n at async Microsoft.Azure.WebJobs.Extensions.Http.HttpRequestManager.ProcessRequestAsync(HttpRequestMessage request,Func 3 processRequestHandler,CancellationToken   cancelToken)\ r \ n在异步   Microsoft.Azure.WebJobs.Script.WebHost.Controllers.FunctionsController.ExecuteAsync(HttpControllerContext   controllerContext,CancellationToken cancellationToken)\ r \ n在异步   System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage   请求,取消语音取消语句)\ r \ n在异步   System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage   请求,取消语音取消语句)\ r \ n在异步   Microsoft.Azure.WebJobs.Script.WebHost.Handlers.SystemTraceHandler.SendAsync(HttpRequestMessage   请求,取消语音取消语句)\ r \ n在异步   Microsoft.Azure.WebJobs.Script.WebHost.Handlers.WebScriptHostHandler.SendAsync(HttpRequestMessage   请求,取消语音取消语句)\ r \ n在异步   System.Web.Http.HttpServer.SendAsync(HttpRequestMessage   请求,取消语音取消语句)“}

我想在找不到文件时发送404状态代码,而不是像那样丑陋的堆栈(和错误500)。我应该遵循的最佳方法是什么?

== update

请注意 FunctionInvocationException 在调用我的函数之前发生。在 Azure Functions Runtime 中似乎是一个错误,我不知道它是错误还是预期。

1 个答案:

答案 0 :(得分:0)

我们也可以在Azure功能App中捕获异常。在您的情况下,您可以捕获异常并返回http状态代码HttpStatusCode.NotFound

 public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");

            try
            {
                //Todo :Add your own logic
            }
            catch (Exception ex)
            {
                return req.CreateResponse(HttpStatusCode.NotFound);
            }
            return req.CreateResponse(HttpStatusCode.OK);
        }

修改

正如您所说,它在调用函数之前发生,似乎是Azure功能集成SDK返回的。它可能是按设计的。我们也可以将我们的想法提交给azure function team

如果我们想控制逻辑,我的工作是我们可以删除输入blob并自己实现自定义逻辑。我们可以从Microsoft Azure Storage SDK for Node.js获取示例代码。

enter image description here