/// <summary> /// UseExceptionHandlerWithProblemDetails /// </summary> /// <param name="app"></param> /// <returns></returns> private static void UseExceptionHandlerWithProblemDetails(IApplicationBuilder app, ILoggerFactory loggerFactory) { app.UseExceptionHandler(builder => { builder.Run(async context => { var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>(); if (exceptionHandlerFeature != null) { var exception = exceptionHandlerFeature.Error; if (loggerFactory != null) { var logger = loggerFactory.CreateLogger("GlobalExceptionHandler"); logger.LogError($"Unexpected error: {exception}"); } var problemDetails = new ProblemDetailsFactory(context.Request.Path, context.TraceIdentifier).GetInternalServerError(exception); context.Response.StatusCode = problemDetails.Status.Value; context.Response.ContentType = Constants.CONTENT_TYPE_JSON; var json = JsonConvert.SerializeObject(problemDetails); await context.Response.WriteAsync(json); } }); }); }
/// <summary> /// UseProblemDetailsFor405 /// </summary> /// <param name="app"></param> /// <returns></returns> private static void UseProblemDetailsFor40X(IApplicationBuilder app) { app.Use(async(context, next) => { await next(); // Verificar no futuro: https://github.com/Homely/Homely.AspNetCore.Mvc.Helpers#problemdetails-for-all-4xx5xx-http-errors var listCustom = new List <int>() { (int)HttpStatusCode.MethodNotAllowed, (int)HttpStatusCode.Unauthorized, (int)HttpStatusCode.Forbidden, }; if (listCustom.Contains(context.Response.StatusCode)) { var pd = new ProblemDetails(); var pdf = new ProblemDetailsFactory(context.Request.Path, context.TraceIdentifier); pdf.SetProblemDetails(pd, context.Response.StatusCode); context.Response.ContentType = Constants.CONTENT_TYPE_JSON; var json = JsonConvert.SerializeObject(pd); await context.Response.WriteAsync(json); } }); }
public ModelStateBuilder <T> SetFieldError(string fieldName, ProblemDetailsFieldType errorType, string message = null, Func <ProblemDetailsFieldType, bool> addOnly = null) { if (addOnly == null || addOnly(errorType)) { this._controller.ModelState.TryAddModelError(fieldName, ProblemDetailsFactory.GetComposeTypeAndErrorMessage(errorType, message)); } return(this); }
public void OnActionExecuting(ActionExecutingContext context) { if (context.Result == null && !context.ModelState.IsValid) { var problemDetails = new ProblemDetailsFactory ( context.HttpContext.Request.Path, context.HttpContext.TraceIdentifier ).GetClientError(context.ModelState, StatusCodes.Status400BadRequest); context.Result = ModelStateInvalidExtensions.GetObjectResult(problemDetails); } }
public void OnResultExecuting(ResultExecutingContext context) { if (!(context.Result is IClientErrorActionResult clientError)) { return; } // We do not have an upper bound on the allowed status code. This allows this filter to be used // for 5xx and later status codes. if (clientError.StatusCode < 400) { return; } var problemDetails = new ProblemDetailsFactory ( context.HttpContext.Request.Path, context.HttpContext.TraceIdentifier ).GetClientError(context.ModelState, clientError.StatusCode.Value); context.Result = ModelStateInvalidExtensions.GetObjectResult(problemDetails); }
public static IRuleBuilderOptions <T, TProperty> WithMessage <T, TProperty>(this IRuleBuilderOptions <T, TProperty> rule, ProblemDetailsFieldType type, string errorMessage) { rule.WithMessage(ProblemDetailsFactory.GetComposeTypeAndErrorMessage(type, errorMessage)); return(rule); }