public static IApplicationBuilder UseProblemDetails(this IApplicationBuilder app, Action <ProblemDetailsOptions> configure)
        {
            var options = new ProblemDetailsOptions();

            configure?.Invoke(options);

            ConfigureDefaults(options);

            return(app.UseMiddleware <ProblemDetailsMiddleware>(Options.Create(options)));
        }
        private static void ConfigureDefaults(ProblemDetailsOptions options)
        {
            if (options.IncludeExceptionDetails == null)
            {
                options.IncludeExceptionDetails = IncludeExceptionDetails;
            }

            options.TryMap <NotImplementedException>(ex =>
                                                     new ExceptionProblemDetails(ex, StatusCodes.Status501NotImplemented));
        }
Exemple #3
0
        public static IApplicationBuilder UseProblemDetails(this IApplicationBuilder app, Action <ProblemDetailsOptions> configure)
        {
            var options = new ProblemDetailsOptions();

            configure?.Invoke(options);

            // Try to pull the IConfigureOptions<ProblemDetailsOptions> service from the container
            // in case the user has called AddProblemDetails and still use this overload.
            // If the setup hasn't been configured. Create an instance explicitly and use that.
            var setup = app.ApplicationServices.GetService <IConfigureOptions <ProblemDetailsOptions> >() ?? new ProblemDetailsOptionsSetup();

            setup.Configure(options);

            return(app.UseMiddleware <ProblemDetailsMiddleware>(Options.Create(options)));
        }
Exemple #4
0
        public static ProblemDetailsOptions AddDatabaseProblemDetailsExceptionMapping(this ProblemDetailsOptions options)
        {
            options.Map <SqlException>(sqlEx => GetResponseToSqlException(sqlEx));
            options.Map <DbUpdateException>(dbEx => GetResponseToDbUpdateException(dbEx));

            return(options);
        }
Exemple #5
0
        public static ProblemDetailsOptions AddInvalidInputProblemDetailsExceptionMapping(this ProblemDetailsOptions options)
        {
            options.Map <FormatException>(ex => new StatusCodeProblemDetails(StatusCodes.Status400BadRequest));
            options.Map <JsonException>(ex => new StatusCodeProblemDetails(StatusCodes.Status400BadRequest));
            options.Map <ValidationException>(ex => new StatusCodeProblemDetails(StatusCodes.Status400BadRequest));

            return(options);
        }
Exemple #6
0
        public static ProblemDetailsOptions AddSecurityProblemDetailsExceptionMapping(this ProblemDetailsOptions options)
        {
            /* The 402 code is named "Unauthorized" in the HTTP specification, but the description of the code makes it
             * clear that it means "not authenticated". For a true "unauthorized response, use status code 403 - Forbidden. */
            options.Map <InvalidCredentialException>(ex => new StatusCodeProblemDetails(StatusCodes.Status401Unauthorized));

            options.Map <NotAuthenticatedException>(ex => new StatusCodeProblemDetails(StatusCodes.Status401Unauthorized));
            options.Map <NotAuthorizedException>(ex => new StatusCodeProblemDetails(StatusCodes.Status401Unauthorized));
            return(options);
        }
Exemple #7
0
        public static ProblemDetailsOptions AddProblemDetailsExceptionMappingForWhenItsOurFault(this ProblemDetailsOptions options)
        {
            options.Map <InvalidOperationException>(ex => new StatusCodeProblemDetails(StatusCodes.Status500InternalServerError));
            options.Map <NotImplementedException>(ex => new StatusCodeProblemDetails(StatusCodes.Status501NotImplemented));
            options.Map <HttpRequestException>(ex => new StatusCodeProblemDetails(StatusCodes.Status503ServiceUnavailable));
            options.Map <Exception>(ex => new StatusCodeProblemDetails(StatusCodes.Status500InternalServerError));

            return(options);
        }