public static void SetCustomExceptionHandler(this IApplicationBuilder application, bool isDevelopment) { application.UseExceptionHandler(errorApp => { errorApp.Run(async context => { var exceptionHandlerPathFeature = context.Features.Get <IExceptionHandlerPathFeature>(); var exception = exceptionHandlerPathFeature.Error; var result = new ExceptionResponse { StatusCode = context.Response.StatusCode, Message = exception.Message }; if (isDevelopment) { result.StackTrace = exception.StackTrace; } context.Response.ContentType = "application/json"; await context.Response.WriteAsync(result.ToJson()); }); }); }