Example #1
0
 public override int SaveChanges()
 {
     try
     {
         return(base.SaveChanges());
     }
     catch (Exception ex)
     {
         throw _exceptionMapper.Map(ex);
     }
 }
        /// <summary>
        ///     Handle a thrown exception.
        /// </summary>
        /// <param name="edi">The thrown exception info.</param>
        /// <param name="context">The http context.</param>
        protected virtual async Task HandleExceptionAsync(ExceptionDispatchInfo edi, HttpContext context)
        {
            // We're only interested in the root cause of the exception.
            // Unpack the base exception in case of an aggregate exception.
            if (edi.SourceException is AggregateException)
            {
                edi = ExceptionDispatchInfo.Capture((edi.SourceException as AggregateException).GetBaseException());
            }

            // If the exception is not of type TException, throw it upwards.
            if (edi.SourceException is not TException)
            {
                edi.Throw();
            }

            // We can't do anything if the response has already started, just abort.
            // This means headers have already been sent to the client.
            if (context.Response.HasStarted)
            {
                _logger.LogError(edi.SourceException, "Could not do anything, response has already started");

                edi.Throw();
            }

            // Save original context path for later restoration in case we can't handle the exception.
            var originalPath = context.Request.Path;

            try
            {
                // Prepare the context for being passed back up the chain.
                ClearHttpContext(context);

                // Add a generated error message to the http context.
                context.Features.Set(_mapper.Map(edi.SourceException as TException));

                // Pass back up the chain to reach the error handling controller.
                context.Request.Path = _options.ErrorControllerPath ?? throw new InvalidOperationException("No error handler path specified");
                await _next(context);

                return;
            }
            catch (Exception e)
            {
                // Only log, we re-throw later.
                _logger.LogError(e, "Could not handle exception, re-throwing original");
            }
            finally
            {
                // Set the context path to what it was.
                context.Request.Path = originalPath;
            }

            // If we reach this we couldn't handle the exception.
            // Re-throw it upwards.
            edi.Throw();
        }
Example #3
0
 public void Intercept(IInvocation invocation)
 {
     try
     {
         invocation.Proceed();
     }
     catch (Exception e)
     {
         throw _exceptionMapper.Map(e);
     }
 }
 public void Intercept(IInvocation invocation)
 {
     try
     {
         invocation.Proceed();
     }
     catch (Exception e)
     {
         // .NET 4.5 feature: Capture an exception and re-throw it without changing the stack trace
         ExceptionDispatchInfo.Capture(_exceptionMapper.Map(e)).Throw();
     }
 }
Example #5
0
        public IActionResult Error()
        {
            var context   = HttpContext.Features.Get <IExceptionHandlerFeature>();
            var exception = context?.Error;

            if (exception is null)
            {
                return(NotFound());
            }

            IErrorData error = _exceptionMapper.Map(exception);

            _logger.LogError(exception, error.Message);

            var model = new ErrorVM
            {
                Message = error.Message,
                Data    = error.Data
            };

            return(StatusCode(error.StatusCode, model));
        }