Exemplo n.º 1
0
        private Task HandleExceptionAsync(HttpContext context, Exception exception,
                                          ApiExceptionOptions options)
        {
            var error = new ApiError
            {
                Id     = Guid.NewGuid().ToString(),
                Status = (short)HttpStatusCode.InternalServerError,
                Title  = "Some kind of error occured in API. Please use the id and contact our " +
                         "support team if the problem persists."
            };

            options.AddResponseDetails(context, exception, error);

            var innerExMessage = GetInnermostExceptionMessage(exception);

            var level = options.DetermineLogLevel?.Invoke(exception) ?? LogLevel.Critical;

            _logger.Log(level, exception, $"BADNESS!!! {innerExMessage} " +
                        $" -- {error.Id}.");

            var result = JsonConvert.SerializeObject(error);

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)HttpStatusCode.InternalServerError;
            return(context.Response.WriteAsync(result));
        }
Exemplo n.º 2
0
 public ApiExceptionMiddleware(ApiExceptionOptions options,
                               RequestDelegate next, ILogger <ApiExceptionMiddleware> logger)
 {
     _next    = next;
     _logger  = logger;
     _options = options;
 }
Exemplo n.º 3
0
        public static IApplicationBuilder UseApiExceptionHandler(this IApplicationBuilder builder,
                                                                 Action <ApiExceptionOptions> configureOptions)
        {
            var options = new ApiExceptionOptions();

            configureOptions(options);

            return(builder.UseMiddleware <ApiExceptionMiddleware>(options));
        }