コード例 #1
0
        private void Handle(ExceptionHandlerContext context)
        {
            IExceptionQualifier efilter = context.RequestContext.Configuration.DependencyResolver.GetService(typeof(IExceptionQualifier)) as IExceptionQualifier;

            if (efilter == null)
            {
                return;
            }

            string message = efilter.DisplayExceptionDetails(context.Exception) ? context.Exception.Message : efilter.GenericErrorMessage;

            context.Result = new TextPlainErrorResult
            {
                Request = context.ExceptionContext.Request,
                Content = message
            };
        }
コード例 #2
0
        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, "An unhandled exception has occurred: " + ex.Message);
                if (context.Response.HasStarted)
                {
                    _logger.LogWarning("The response has already started, the error handler will not be executed.");
                    throw;
                }
                PathString originalPath = context.Request.Path;
                try
                {
                    var info = new LuccaExceptionBuilderInfo(ex,
                                                             _filter?.StatusCode(ex) ?? HttpStatusCode.InternalServerError,
                                                             _filter?.DisplayExceptionDetails(ex) ?? false,
                                                             _filter?.GenericErrorMessage,
                                                             _filter?.PreferedResponseType(context.Request.Path));

                    await GenerateErrorResponseAsync(context, info);
                }
                catch (Exception ex2)
                {
                    _logger.LogError(0, ex2, "An exception was thrown attempting to execute the error handler.");
                    throw;
                }
                finally
                {
                    context.Request.Path = originalPath;
                }
            }
        }