예제 #1
0
        public static IApplicationBuilder UseGlobalExceptionHandler(this IApplicationBuilder builder,
                                                                    Action <GlobalExceptionOptions> configureOptions)
        {
            var options = new GlobalExceptionOptions();

            configureOptions(options);

            return(builder.UseMiddleware <GlobalExceptionHandler>(options));
        }
예제 #2
0
 public GlobalExceptionHandler(
     GlobalExceptionOptions options,
     RequestDelegate next,
     ILogger <GlobalExceptionHandler> logger)
 {
     _next    = next;
     _logger  = logger;
     _options = options;
 }
예제 #3
0
        private Task HandleExceptionAsync(HttpContext context, Exception exception, GlobalExceptionOptions opts)
        {
            var error = new PublicErrorDetails
            {
                Id     = Guid.NewGuid().ToString(),
                Status = (short)HttpStatusCode.InternalServerError,
                Code   = "",
                Title  = "Some kind of error occurred in the API.  Please use the error Id and contact our support team if the problem persists.",
                Detail = ""
            };

            opts.AddResponseDetails?.Invoke(context, exception, error);

            var innerException = GetInnermostException(exception);

            _logger.LogError(exception, innerException.Message + "|{httpStatusCode}|{errorId}", error.Status, error.Id);

            var result = JsonConvert.SerializeObject(error);

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)HttpStatusCode.InternalServerError;
            return(context.Response.WriteAsync(result));
        }