Пример #1
0
 public ForbiddenResponseExamples(
     IHttpContextAccessor httpContextAccessor,
     ProblemDetailsHelper problemDetailsHelper)
 {
     _httpContextAccessor  = httpContextAccessor;
     _problemDetailsHelper = problemDetailsHelper;
 }
 public InternalServerErrorResponseExamples(
     IHttpContextAccessor httpContextAccessor,
     ProblemDetailsHelper problemDetailsHelper)
 {
     _httpContextAccessor  = httpContextAccessor;
     _problemDetailsHelper = problemDetailsHelper;
 }
Пример #3
0
 public PreconditionFailedResponseExamples(
     IHttpContextAccessor httpContextAccessor,
     ProblemDetailsHelper problemDetailsHelper)
 {
     _httpContextAccessor  = httpContextAccessor;
     _problemDetailsHelper = problemDetailsHelper;
 }
Пример #4
0
 public NotAcceptableResponseExamples(
     IHttpContextAccessor httpContextAccessor,
     ProblemDetailsHelper problemDetailsHelper)
 {
     _httpContextAccessor  = httpContextAccessor;
     _problemDetailsHelper = problemDetailsHelper;
 }
Пример #5
0
 public BadRequestResponseExamples(
     IHttpContextAccessor httpContextAccessor,
     ProblemDetailsHelper problemDetailsHelper)
 {
     _httpContextAccessor  = httpContextAccessor;
     _problemDetailsHelper = problemDetailsHelper;
 }
Пример #6
0
 public UnauthorizedResponseExamples(
     IHttpContextAccessor httpContextAccessor,
     ProblemDetailsHelper problemDetailsHelper)
 {
     _httpContextAccessor  = httpContextAccessor;
     _problemDetailsHelper = problemDetailsHelper;
 }
Пример #7
0
 public ValidationErrorResponseExamples(
     IHttpContextAccessor httpContextAccessor,
     ProblemDetailsHelper problemDetailsHelper)
 {
     _httpContextAccessor  = httpContextAccessor;
     _problemDetailsHelper = problemDetailsHelper;
 }
        public ProblemDetails Map(ApiProblemDetailsException exception, ProblemDetailsHelper problemDetailsHelper)
        {
            if (!Handles(exception))
            {
                throw new InvalidCastException("Could not map problem details!");
            }

            return(MapException(exception, problemDetailsHelper));
        }
Пример #9
0
        public void Configure(ProblemDetailsOptions options)
        {
            options.IncludeExceptionDetails = ctx => Environment.IsDevelopment();

            options.MapStatusCode = MapStatusCode;

            // keep consistent with asp.net core 2.2 conventions that adds a tracing value
            options.OnBeforeWriteDetails = (ctx, details) => ProblemDetailsHelper.SetTraceId(details, HttpContextAccessor.HttpContext);
        }
Пример #10
0
 public static IEnumerable <IExceptionHandler> GetHandlers(ProblemDetailsHelper problemDetailsHelper) => new IExceptionHandler[]
 {
     new DomainExceptionHandler(problemDetailsHelper),
     new ApiExceptionHandler(problemDetailsHelper),
     new AggregateNotFoundExceptionHandler(problemDetailsHelper),
     new ValidationExceptionHandler(problemDetailsHelper),
     new HttpRequestExceptionHandler(problemDetailsHelper),
     new DBConcurrencyExceptionHandler(problemDetailsHelper),
     new NotImplementedExceptionHandler(problemDetailsHelper),
 };
Пример #11
0
 public ProblemDetailsSetup(
     IHostingEnvironment environment,
     IHttpContextAccessor httpContextAccessor,
     IOptions <ApiBehaviorOptions> apiOptions,
     ProblemDetailsHelper problemDetailsHelper)
 {
     ProblemDetailsHelper = problemDetailsHelper;
     Environment          = environment;
     HttpContextAccessor  = httpContextAccessor;
     ApiOptions           = apiOptions.Value;
 }
Пример #12
0
 public ExceptionHandler(
     ILogger <ApiExceptionHandler> logger,
     IEnumerable <ApiProblemDetailsExceptionMapping> apiProblemDetailsExceptionMappings,
     IEnumerable <IExceptionHandler> customExceptionHandlers,
     ProblemDetailsHelper problemDetailsHelper)
 {
     _logger = logger;
     _apiProblemDetailsExceptionMappers = apiProblemDetailsExceptionMappings;
     _problemDetailsHelper = problemDetailsHelper ?? throw new ArgumentNullException(nameof(problemDetailsHelper));
     _exceptionHandlers    = customExceptionHandlers.Concat(DefaultExceptionHandlers.GetHandlers(_problemDetailsHelper));
 }
Пример #13
0
        private ProblemDetails MapStatusCode(HttpContext context, int statusCode)
        => !ApiOptions.SuppressMapClientErrors && ApiOptions.ClientErrorMapping.TryGetValue(statusCode, out var errorData)
                ? new ProblemDetails
        {
            HttpStatus         = statusCode,
            ProblemTypeUri     = errorData.Link,
            Title              = errorData.Title,
            Detail             = string.Empty,
            ProblemInstanceUri = ProblemDetailsHelper.GetInstanceUri(context)
        }

                : new StatusCodeProblemDetails(statusCode)
        {
            Detail             = string.Empty,
            ProblemInstanceUri = ProblemDetailsHelper.GetInstanceUri(context)
        };
Пример #14
0
 public static void SetTraceId(this ProblemDetails details, ProblemDetailsHelper problemDetailsHelper, HttpContext httpContext)
 => details.ProblemInstanceUri = problemDetailsHelper.GetInstanceUri(httpContext);
Пример #15
0
        private static IApplicationBuilder UseApiExceptionHandling(
            IApplicationBuilder app,
            string corsPolicyName,
            StartupUseOptions startupUseOptions,
            ProblemDetailsHelper problemDetailsHelper)
        {
            var logger               = startupUseOptions.Common.LoggerFactory.CreateLogger <ApiExceptionHandler>();
            var customHandlers       = startupUseOptions.Api.CustomExceptionHandlers ?? new IExceptionHandler[] { };
            var problemDetailMappers = startupUseOptions.Api.ProblemDetailsExceptionMappers ?? new ApiProblemDetailsExceptionMapping[] {};
            var exceptionHandler     = new ExceptionHandler(logger, problemDetailMappers, customHandlers, problemDetailsHelper);

            app.UseExceptionHandler(builder =>
            {
                builder.UseCors(policyName: corsPolicyName);
                startupUseOptions.MiddlewareHooks.AfterCors?.Invoke(builder);

                builder.UseProblemDetails();
                startupUseOptions.MiddlewareHooks.AfterProblemDetails?.Invoke(builder);

                builder
                .UseMiddleware <EnableRequestRewindMiddleware>()

                // https://github.com/serilog/serilog-aspnetcore/issues/59
                .UseMiddleware <AddCorrelationIdToResponseMiddleware>()
                .UseMiddleware <AddCorrelationIdMiddleware>()
                .UseMiddleware <AddCorrelationIdToLogContextMiddleware>()

                .UseMiddleware <AddHttpSecurityHeadersMiddleware>(
                    startupUseOptions.Server.ServerName,
                    startupUseOptions.Server.PoweredByName,
                    startupUseOptions.Server.FrameOptionsDirective)

                .UseMiddleware <AddRemoteIpAddressMiddleware>(startupUseOptions.Api.RemoteIpAddressClaimName)

                .UseMiddleware <AddVersionHeaderMiddleware>(startupUseOptions.Server.VersionHeaderName);
                startupUseOptions.MiddlewareHooks.AfterMiddleware?.Invoke(builder);

                builder
                .UseMiddleware <DefaultResponseCompressionQualityMiddleware>(new Dictionary <string, double>
                {
                    { "br", 1.0 },
                    { "gzip", 0.9 }
                })
                .UseResponseCompression();
                startupUseOptions.MiddlewareHooks.AfterResponseCompression?.Invoke(builder);

                var requestLocalizationOptions = startupUseOptions
                                                 .Common
                                                 .ServiceProvider
                                                 .GetRequiredService <IOptions <RequestLocalizationOptions> >()
                                                 .Value;

                builder.UseRequestLocalization(requestLocalizationOptions);
                startupUseOptions.MiddlewareHooks.AfterRequestLocalization?.Invoke(builder);

                builder
                .Run(async context =>
                {
                    context.Response.StatusCode  = StatusCodes.Status500InternalServerError;
                    context.Response.ContentType = MediaTypeNames.Application.Json;

                    var error     = context.Features.Get <IExceptionHandlerFeature>();
                    var exception = error?.Error;

                    // Errors happening in the Apply() stuff result in an InvocationException due to the dynamic stuff.
                    if (exception is TargetInvocationException && exception.InnerException != null)
                    {
                        exception = exception.InnerException;
                    }

                    await exceptionHandler.HandleException(exception, context);
                });
            });

            return(app);
        }
Пример #16
0
 public ValidationExceptionHandler(ProblemDetailsHelper problemDetailsHelper)
 => _problemDetailsHelper = problemDetailsHelper;
Пример #17
0
 public ApiExceptionHandler(ProblemDetailsHelper problemDetailsHelper)
 => _problemDetailsHelper = problemDetailsHelper;
 public abstract ProblemDetails MapException(ApiProblemDetailsException exception, ProblemDetailsHelper problemDetailsHelper);