Example #1
0
        public static IApplicationBuilder UseApiExceptionHandler(this IApplicationBuilder builder, Action <ApiExceptionOptions> configureOptions)
        {
            var options = new ApiExceptionOptions();

            configureOptions(options);

            return(builder.UseMiddleware <ApiExceptionMiddleware>(options));
        }
        private Task HandleExceptionAsync(HttpContext context, Exception ex, ApiExceptionOptions options)
        {
            var error = new ApiError
            {
                Id     = Guid.NewGuid().ToString(),
                Status = (short)HttpStatusCode.InternalServerError,
                Title  = "Some kind of error occurred in the API. Please use the id and contact our support team if the problem persists."
            };

            options.AddResponseDetails?.Invoke(context, ex, error);

            var innerExMessage = GetInnerMostExceptionMessage(ex);

            logger.LogError(ex, "BADNESS!!! " + innerExMessage + "-- {errorId}.", error.Id);

            var result = JsonConvert.SerializeObject(error);

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)HttpStatusCode.InternalServerError;
            return(context.Response.WriteAsync(result));
        }
 public ApiExceptionMiddleware(ApiExceptionOptions options, RequestDelegate next, ILogger <ApiExceptionMiddleware> logger)
 {
     this.options = options;
     this.next    = next;
     this.logger  = logger;
 }