Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Return Internal Server Error details from .Net Core Azure Function

I finally go back to writing a few lines of code.

I created an Azure function by using Visual Studio 2019 and .Net Core 3.1.

The thing that left me puzzled is that I haven't found a class to handle generic exceptions, the Http Status Code InternalServerError.

I found OkObjectResult, BadRequestObjectResult, UnauthorizedResult... but I  can't find a generic Internal Server Error.

The first solution to handle this scenario, is to rethrow the exception in case of error.

catch (Exception ex)
            {
                log.LogError(ex, ex.Message);
                throw;
            }

This solution returns the Internal Server Error code, but does not return to the client the reason of the exception.

My solution, in my opinion very obvious, is to create a custom class named InternalServerErrorObjectResult within the namespace Microsoft.AspNetCore.Mvc which inherits from ObjectResult class.

This class set the value of the response to the Exception detail and the status code to HttpStatusCode.InternalServerError.


namespace Microsoft.AspNetCore.Mvc
{
    public class InternalServerErrorObjectResult : ObjectResult
    {
        public InternalServerErrorObjectResult(Exception ex) : base(ex)
        {
            this.StatusCode = (int)HttpStatusCode.InternalServerError;
            //this.Value = ex;
            
        }
        public InternalServerErrorObjectResult(string message) : base(new ApplicationException(message))
        {
            this.StatusCode = (int)HttpStatusCode.InternalServerError;
        }
    }
}

Inside the error handle code, I return a new object of type InternalServerErrorObjectResult, using as parameter the managed exception (if I had it) or a generic string error message.

  catch (Exception ex)
            {
                log.LogError(ex, ex.Message);
                return new InternalServerErrorObjectResult(ex);
            }

In this case, the client receives the full detail of the exception.

HTTP/1.1 500 Internal Server Error
Content-Length: 499
Content-Type: application/json; charset=utf-8
Request-Context: appId=cid-v1:d35b06ae-57cc-4c8a-85f8-5695d6943614
Access-Control-Allow-Origin: https://zsis.sharepoint.com
Access-Control-Allow-Credentials: true
Date: Fri, 04 Dec 2020 08:05:16 GMT


{"stackTrace":"   at WarehouseLabel.Functions.Functions.GetAllLookupValues.Run(HttpRequest req, ILogger log, ExecutionContext context, ClaimsPrincipal claimsPrincipal) in C:\\git\\WarehouseLabel\\Source\\.WarehouseLabel\\WarehouseLabel.Functions\\Functions\\GetAllLookupValues.cs:line 32","message":"Questa eccezione la genero io apposta, vediamo se arriva il messaggio","data":{},"innerException":null,"helpLink":null,"source":"WarehouseLabel.Functions","hResult":-2146232832}




This post first appeared on ZSvipullo, please read the originial post: here

Share the post

Return Internal Server Error details from .Net Core Azure Function

×

Subscribe to Zsvipullo

Get updates delivered right to your inbox!

Thank you for your subscription

×