Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ublic static class WebApplicationExtensions
- {
- public static IServiceCollection AddServices(this IServiceCollection services, IConfiguration configuration)
- {
- /*Some code*/
- Log.Logger.Information("WebApplicationExtensions->AddServices->AddProblemDetails");
- services.AddProblemDetails(configuration);
- return services;
- }
- private static void AddProblemDetails(this IServiceCollection services, IConfiguration configuration)
- {
- services.AddProblemDetails(options =>
- {
- options.ValidationProblemStatusCode = StatusCodes.Status400BadRequest;
- options.Map<ProblemControllerException>(exception => new DetailedProblem(exception.error.Code, exception.error.Message)
- {
- Detail = exception.HResult.ToString(),
- Title = exception.Message,
- Type = $"https://httpstatuses.io/{exception.statusCode}",
- Status = exception.statusCode
- });
- options.Map<ProblemServiceException>(exception => new DetailedProblem(exception.error.Code, exception.error.Message)
- {
- Detail = exception.HResult.ToString(),
- Title = exception.Message,
- Type = $"https://httpstatuses.io/{exception.statusCode}",
- Status = exception.statusCode
- });
- options.Map<ProblemFluentValidationException>(exception => new DetailedProblem(exception.errors)
- {
- Detail = exception.HResult.ToString(),
- Title = string.IsNullOrEmpty(exception.Message) ? "" : exception.Message,
- Type = $"https://httpstatuses.io/{exception.statusCode}",
- Status = exception.statusCode
- });
- options.Map<System.Exception>(exception => new DetailedProblem(ErrorsType.EMUN5000006.GetDisplayName(), "Internal server error")
- {
- Detail = exception.Message,
- Title = "Internal server error",
- Type = exception.GetType().Name,
- Status = (int)HttpStatusCode.InternalServerError
- });
- options.OnBeforeWriteDetails = (ctx, problem) =>
- {
- var id = ctx.Request.GetHttpHeadersKey(configuration["ID_HEADER_KEY"]);
- if (problem is ProblemDetailsBase pdb)
- {
- pdb.Id = id;
- }
- problem.Instance = ctx.Request.Path;
- };
- options.IncludeExceptionDetails = (ctx, ex) =>
- {
- return false;
- };
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment