Example #1
0
        /// <summary>
        /// Executes the Exceptional-wrapped middleware.
        /// </summary>
        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next.Invoke(context);
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, "An unhandled exception has occurred, logging to Exceptional");
                var error = await ex.LogAsync(context);

                // If options say to do so, show the exception page to the user
                if (Settings.Current.UseExceptionalPageOnThrow && error != null)
                {
                    var response = context.Response;
                    if (context.Response.HasStarted)
                    {
                        _logger.LogWarning("The response has already started, the Exceptional error page will not be displayed.");
                        throw;
                    }

                    try
                    {
                        response.HttpContext.Features.Get <IHttpResponseFeature>().ReasonPhrase = null;
                        response.Headers.Clear();
                        if (response.Body.CanSeek)
                        {
                            response.Body.SetLength(0);
                        }
                        response.StatusCode = 500;

                        var page = new ErrorDetailPage(error, Settings.Current.DefaultStore, "", error.GUID)
                        {
                            HeaderTitle     = "An error was thrown during this request.",
                            PageTitle       = "An error was thrown during this request.",
                            InlineCSS       = true,
                            IncludeJS       = false,
                            ShowActionLinks = false
                        };
                        response.ContentType = "text/html";
                        await response.WriteAsync(page.Render());

                        return;
                    }
                    catch (Exception pex)
                    {
                        _logger.LogError(0, pex, "An exception was thrown attempting to display the Exceptional page.");
                    }
                }
                throw;
            }
        }
Example #2
0
        public void RendersNullCustomData()
        {
            var settings = new ExceptionalSettings();
            var ex       = new Exception("My exception");
            var error    = new Error(ex, settings, initialCustomData: new Dictionary <string, string>
            {
                ["MyData"] = null
            });

            var errorPage = new ErrorDetailPage(error, settings, settings.DefaultStore, "/", error.GUID);

            Assert.NotNull(errorPage);

            var rendered = errorPage.Render();

            Assert.NotNull(rendered);
            Assert.Contains("var Exception = ", rendered);
            Assert.Contains("MyData", rendered);
        }