Skip to content
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
@MassimoC

Description

@MassimoC

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);
                });
            });
        }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions