Exemple #1
0
        public static IApplicationBuilder UseStatusCodePagesConfiguration(this IApplicationBuilder app)
        {
            app.UseStatusCodePages(async context =>
            {
                var req = context.HttpContext.Request;
                var res = context.HttpContext.Response;

                res.ContentType = "application/json";

                switch (res.StatusCode)
                {
                case (int)HttpStatusCode.Unauthorized:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultUnauthorizedResponse()));
                    break;

                case (int)HttpStatusCode.Forbidden:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultForbiddenResponse()));
                    break;

                case (int)HttpStatusCode.NotFound:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultNotFoundResponse()));
                    break;

                case (int)HttpStatusCode.BadRequest:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultBadRequestResponse()));
                    break;

                case (int)HttpStatusCode.MethodNotAllowed:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultMethodNotAllowedResponse()));
                    break;

                case (int)HttpStatusCode.RequestTimeout:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultRequestTimeoutResponse()));
                    break;

                case (int)HttpStatusCode.InternalServerError:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultInternalServerErrorResponse()));
                    break;

                default:
                    await res.WriteAsync(SerializationHelper.SerializeToJson(ErrorDto.DefaultInternalServerErrorResponse()));
                    break;
                }
            });

            return(app);
        }
Exemple #2
0
        private Task HandleExceptionAsync(HttpContext httpContext, Exception exception)
        {
            var response = default(ErrorDto);

            httpContext.Response.ContentType = "application/json";

            if (exception is ApiException)
            {
                var ex = (exception as ApiException);
                httpContext.Response.StatusCode = ex.Code;
                response = new ErrorDto(ex.Name, ex.Message, ex.Code);
            }
            else if (exception is ArgumentNullException || exception is ArgumentException)
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                response = ErrorDto.DefaultBadRequestResponse();
            }
            else if (exception is ValidationException)
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;

                response         = ErrorDto.DefaultBadRequestResponse();
                response.Details = (exception as ValidationException).Errors
                                   .Select(x => new ErrorFieldDto(field: x.PropertyName, value: x.ErrorMessage));
            }
            else
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                response = ErrorDto.DefaultInternalServerErrorResponse();
            }

            if (!(exception is ApiException))
            {
                if (!response.Details.Any() && exception.GetInnerExceptions().Any())
                {
                    response.Details = exception.GetInnerExceptions()
                                       .Select(x => new ErrorFieldDto(field: null, value: x.Message))
                                       .ToList();
                }
            }

            _logger.LogError(exception, response.Message);
            return(httpContext.Response.WriteAsync(SerializationHelper.SerializeToJson(response)));
        }