3

I wonder about a good practice of handling exceptions in the Http-triggered azure functions -in the detail I wonder about standard output in case of the unhandled exception - it produces HTTP error status 500 and a JSON body about detailed error message and errorCode

It is about azure functions v1 - .NET 4.6.1 - an assembly project in VS 2017 (I am not sure about situation in v2 - it might differ)

Bellow is an example of response body of the thrown (unhadled) exception with message "Test1" (and with inner exception "Test2"

{
    "id": "942e63a9-6c47-425a-94da-247297953035",
    "requestId": "fa2d2bd9-8e9e-49eb-8751-b023f3a87f73",
    "statusCode": 500,
    "errorCode": 0,
    "message": "Exception while executing function: CheckSite -> Test1 -> Test2",
    "errorDetails": "Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: TestSite ---> System.Exception : Test1 ---> System.Exception : Test2 \r\n   End of inner exception\r\n   at async TestSite.Check..."
}

also added an example of code:

public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequestMessage req, TraceWriter log, ExecutionContext execCtx)
{
    try
    {
        // a business logic
        // ...
        throw new Exception("something wrong with internal data", new Exception("something inner wrong with internal data2"));
        // ...
    }
    catch (Exception ex)
    {
        var correlationId = Guid.NewGuid().ToString();
        log.Error($"Exception: correlationId: {correlationId}, type: {ex.GetType().Name} : {ex.Message}");
        log.Error($"Exception: correlationId: {correlationId}, stack: {ex.StackTrace}");
        return HttpUt.ErrResponse(HttpStatusCode.InternalServerError, "unexpected error", correlationId);
    }       
}

My goal is was to intercept that "low-level-detailed messages" messages with some upper level and with correlation id. It was almost successful just with global try-catch block on the Run method and with throwing new Exception with correlation id (and logging the original one)

Throwing new exception seems to be better over returning HttpResponseMessage(errStatusCode) with content under full control, because it makes the azure function "successfully" failed :) and that failure is logged. But is it possibility how to better control the JSON body for that failure - e.g. have own errorCode value or additional custom items in the JSON body?

(A solution might be in a possibility to somehow make the function failure returning even the HttpResponseMessage(errStatusCode), but I don't know how to do) I also wonder whether there is solution of that problem in azure fn v2?

4
  • Share your code snippet please. Commented Jun 7, 2019 at 10:48
  • I have just added it
    – rychlmoj
    Commented Jun 7, 2019 at 13:44
  • What do you finally want to return? Commented Jun 7, 2019 at 14:35
  • I would like the function to be failed and returning such a json merged with additional custom items like { "errorCode" "100", ...} I wonder whether it is possible to do it. Also I would like to make function failed in case of Bad Request status code - here I actually realized, that I don't know how to do it :(
    – rychlmoj
    Commented Jun 7, 2019 at 15:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.