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.
{
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.
{
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;
}
}
}
In this case, the client receives the full detail of the exception.
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}