public ForbiddenResponseExamples( IHttpContextAccessor httpContextAccessor, ProblemDetailsHelper problemDetailsHelper) { _httpContextAccessor = httpContextAccessor; _problemDetailsHelper = problemDetailsHelper; }
public InternalServerErrorResponseExamples( IHttpContextAccessor httpContextAccessor, ProblemDetailsHelper problemDetailsHelper) { _httpContextAccessor = httpContextAccessor; _problemDetailsHelper = problemDetailsHelper; }
public PreconditionFailedResponseExamples( IHttpContextAccessor httpContextAccessor, ProblemDetailsHelper problemDetailsHelper) { _httpContextAccessor = httpContextAccessor; _problemDetailsHelper = problemDetailsHelper; }
public NotAcceptableResponseExamples( IHttpContextAccessor httpContextAccessor, ProblemDetailsHelper problemDetailsHelper) { _httpContextAccessor = httpContextAccessor; _problemDetailsHelper = problemDetailsHelper; }
public BadRequestResponseExamples( IHttpContextAccessor httpContextAccessor, ProblemDetailsHelper problemDetailsHelper) { _httpContextAccessor = httpContextAccessor; _problemDetailsHelper = problemDetailsHelper; }
public UnauthorizedResponseExamples( IHttpContextAccessor httpContextAccessor, ProblemDetailsHelper problemDetailsHelper) { _httpContextAccessor = httpContextAccessor; _problemDetailsHelper = problemDetailsHelper; }
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)); }
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); }
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), };
public ProblemDetailsSetup( IHostingEnvironment environment, IHttpContextAccessor httpContextAccessor, IOptions <ApiBehaviorOptions> apiOptions, ProblemDetailsHelper problemDetailsHelper) { ProblemDetailsHelper = problemDetailsHelper; Environment = environment; HttpContextAccessor = httpContextAccessor; ApiOptions = apiOptions.Value; }
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)); }
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) };
public static void SetTraceId(this ProblemDetails details, ProblemDetailsHelper problemDetailsHelper, HttpContext httpContext) => details.ProblemInstanceUri = problemDetailsHelper.GetInstanceUri(httpContext);
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); }
public ValidationExceptionHandler(ProblemDetailsHelper problemDetailsHelper) => _problemDetailsHelper = problemDetailsHelper;
public ApiExceptionHandler(ProblemDetailsHelper problemDetailsHelper) => _problemDetailsHelper = problemDetailsHelper;
public abstract ProblemDetails MapException(ApiProblemDetailsException exception, ProblemDetailsHelper problemDetailsHelper);