public override async Task OnExceptionAsync(ExceptionContext context)
            {
                var ex = context.Exception;

                if (ex == null)
                {
                    return;
                }

                var error = ApiErrorFactory.Build(context.HttpContext, ex, _mapper, _logger, _env.IsDevelopment(), _exceptionListeners);
                await JsonSerializer.SerializeAsync(context.HttpContext.Response.Body, error, _serializerOptions);

                context.ExceptionHandled = true;
            }
Пример #2
0
            public override async Task OnExceptionAsync(ExceptionContext context)
            {
                var ex = context.Exception;

                if (ex == null)
                {
                    return;
                }

                var error = ApiErrorFactory.Build(context.HttpContext, ex, _mapper, _logger, _env.IsDevelopment(), _exceptionListeners);

                using (var writer = new StreamWriter(context.HttpContext.Response.Body))
                {
                    _serializer.Serialize(writer, error);
                    await writer.FlushAsync().ConfigureAwait(false);
                }

                context.ExceptionHandled = true;
            }
Пример #3
0
        /// <summary>
        /// Handles the exception asynchronously.
        /// </summary>
        /// <param name="context">The exception handler context</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests</param>
        /// <returns>A Task representing the asynchronous exception handling operation</returns>
        public override Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
            // First of all, check if the exception should be handled.
            if (!ShouldHandle(context))
            {
                return(Task.FromResult(0));
            }

            // Create the API Problem Details model.
            var problemDetails = new ApiProblemDetails();

            if (context.Exception is UnauthorizedAccessException)
            {
                // Unauthorised.
                problemDetails = ApiErrorFactory.Create(new ApiException(HttpStatusCode.Unauthorized));
            }
            else if (context.Exception is ApiException)
            {
                // Custom API error.
                problemDetails = ApiErrorFactory.Create(context.Exception as ApiException);
            }
            else
            {
                // An unhandled exception occurred.
                problemDetails = ApiErrorFactory.Create(new ApiException(HttpStatusCode.InternalServerError));
            }

            // Log the error.
            Trace.TraceError(problemDetails.Instance);

            // Create and return the error response.
            var response = context.Request.CreateResponse((HttpStatusCode)problemDetails.Status, problemDetails);

            context.Result = new ResponseMessageResult(response);

            return(Task.FromResult(0));
        }
 public ExceptionMiddleware(RequestDelegate next, ApiErrorFactory apiErrorFactory)
 {
     fNext            = next;
     fApiErrorFactory = apiErrorFactory;
 }