private async Task GenerateErrorResponseAsync(HttpContext context, LuccaExceptionBuilderInfo info)
        {
            // Prepares the response request
            ClearHttpResponseResponse(context, info.StatusCode);

            // Extracts the Accept header
            string acceptable = NegociateAcceptableContentType(context.Request);

            if (acceptable != null)
            {
                if (await TryRenderErrorOnContentTypeAsync(acceptable, context, info))
                {
                    return;
                }
            }

            // If explicit content type (Content-Type header) match, we render
            if (!string.IsNullOrEmpty(context.Request.ContentType) && await TryRenderErrorOnContentTypeAsync(context.Request.ContentType, context, info))
            {
                return;
            }

            // fallback on PreferedResponseType
            if (!string.IsNullOrWhiteSpace(info.PreferedResponseType))
            {
                if (await TryRenderErrorOnContentTypeAsync(info.PreferedResponseType, context, info))
                {
                    return;
                }
            }

            // fallback on generic text/plain error
            context.Response.ContentType = _textPlain;
            await context.Response.WriteAsync(info.GenericErrorMessage);
        }
        private async Task JsonReport(HttpContext httpContext, LuccaExceptionBuilderInfo info)
        {
            string data = await _options.Value.JsonResponse(info);

            httpContext.Response.ContentType = _appJson;
            await httpContext.Response.WriteAsync(data);
        }
        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;
                }
            }
        }
        private async Task <bool> TryRenderErrorOnContentTypeAsync(string contentType, HttpContext context, LuccaExceptionBuilderInfo info)
        {
            try
            {
                switch (contentType)
                {
                case _textPlain:
                    await TextPlainReport(context, info);

                    return(true);

                case _appJson:
                    await JsonReport(context, info);

                    return(true);

                case _textHtml:
                    await HtmlReport(context, info);

                    return(true);
                }
            }
            catch (Exception e)
            {
                _logger.LogError(0, e, "An exception was thrown attempting to render the error with content type " + contentType);
            }
            return(false);
        }
Пример #5
0
 public Task <String> DefaultHandler(LuccaExceptionBuilderInfo builderInfo) => Task.FromResult($"Error code: {builderInfo.StatusCode}");