This repository was archived by the owner on Nov 13, 2021. It is now read-only.
This repository was archived by the owner on Nov 13, 2021. It is now read-only.
Serialization and Accept header #102
Open
Description
Shouldn't we let ASP.NET handle the serialization of the error in the format that is specified by the accept header of the request instead of deciding for ourselves that it should always be json ?
_Originally posted by @fgheysels
public static void UseExceptionHandlerWithProblemJson(this IApplicationBuilder applicationBuilder)
{
applicationBuilder?.UseExceptionHandler(errorApplication =>
{
errorApplication.Run(async context =>
{
var errorFeature = context.Features.Get<IExceptionHandlerFeature>();
var exception = errorFeature.Error;
var errorDetail = context.Request.IsLocalRequest()
? exception.Demystify().ToString()
: "The instance value should be used to identify the problem when calling customer support";
var problemDetails = new ProblemDetailsError
{
Title = "An unexpected error occurred!",
Status = StatusCodes.Status500InternalServerError,
Detail = errorDetail,
Instance = $"urn:codit.eu:server-error:{Guid.NewGuid()}"
};
// TODO: Plug in telemetry
context.Response.WriteJson(problemDetails, contentType: ContentTypeNames.Application.JsonProblem);
});
});
}