Ejemplo n.º 1
0
        public static IApplicationBuilder ConfigureExceptionHandler(this IApplicationBuilder app, ILogger logger)
        {
            app.UseExceptionHandler(appBuilder =>
            {
                appBuilder.Run(async httpContext =>
                {
                    httpContext.Response.StatusCode  = (int)HttpStatusCode.InternalServerError;
                    httpContext.Response.ContentType = "application/json";

                    var path = string.Empty;
                    var exceptionHandlerFeature = httpContext.Features.Get <IExceptionHandlerPathFeature>();
                    if (exceptionHandlerFeature != null)
                    {
                        logger.LogError($"Path: {exceptionHandlerFeature.Path} | Error: {exceptionHandlerFeature.Error}");
                        path = exceptionHandlerFeature.Path;
                    }

                    var errorDetails = new ErrorDetails
                    {
                        StatusCode = httpContext.Response.StatusCode,
                        Message    = "Internal Server Error.",
                        Path       = path
                    };

                    await httpContext.Response.WriteAsync(errorDetails.ToJsonString());
                });
            });

            return(app);
        }