diff --git a/src/Microsoft.AspNet.Diagnostics/ErrorHandlerMiddleware.cs b/src/Microsoft.AspNet.Diagnostics/ErrorHandlerMiddleware.cs index 852f0a8539..42a661569f 100644 --- a/src/Microsoft.AspNet.Diagnostics/ErrorHandlerMiddleware.cs +++ b/src/Microsoft.AspNet.Diagnostics/ErrorHandlerMiddleware.cs @@ -5,6 +5,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; +using Microsoft.AspNet.Http.Features; using Microsoft.Framework.Logging; namespace Microsoft.AspNet.Diagnostics @@ -60,7 +61,12 @@ namespace Microsoft.AspNet.Diagnostics context.Response.Headers.Clear(); context.Response.OnStarting(_clearCacheHeadersDelegate, context.Response); - // TODO: Try clearing any buffered data. The buffering feature/middleware has not been designed yet. + // if buffering is enabled, then clear it as data could have been written into it. + if (context.Response.Body.CanSeek) + { + context.Response.Body.SetLength(0); + } + await _options.ErrorHandler(context); // TODO: Optional re-throw? We'll re-throw the original exception by default if the error handler throws. return; diff --git a/src/Microsoft.AspNet.Diagnostics/ErrorPageMiddleware.cs b/src/Microsoft.AspNet.Diagnostics/ErrorPageMiddleware.cs index 04524f70ac..2ee5df78b9 100644 --- a/src/Microsoft.AspNet.Diagnostics/ErrorPageMiddleware.cs +++ b/src/Microsoft.AspNet.Diagnostics/ErrorPageMiddleware.cs @@ -13,6 +13,7 @@ using Microsoft.AspNet.Builder; using Microsoft.AspNet.Diagnostics.Views; using Microsoft.AspNet.FileProviders; using Microsoft.AspNet.Http; +using Microsoft.AspNet.Http.Features; using Microsoft.Dnx.Compilation; using Microsoft.Dnx.Runtime; using Microsoft.Framework.Internal; @@ -63,8 +64,23 @@ namespace Microsoft.AspNet.Diagnostics catch (Exception ex) { _logger.LogError("An unhandled exception has occurred while executing the request", ex); + if (context.Response.HasStarted) + { + _logger.LogWarning("The response has already started, the error page middleware will not be executed."); + throw; + } + try { + context.Response.StatusCode = 500; + context.Response.Headers.Clear(); + + // if buffering is enabled, then clear it as data could have been written into it. + if (context.Response.Body.CanSeek) + { + context.Response.Body.SetLength(0); + } + await DisplayException(context, ex); return; } diff --git a/src/Microsoft.AspNet.Diagnostics/Views/ErrorPage.cs b/src/Microsoft.AspNet.Diagnostics/Views/ErrorPage.cs index 65b715ce97..bc2ec0e7f7 100644 --- a/src/Microsoft.AspNet.Diagnostics/Views/ErrorPage.cs +++ b/src/Microsoft.AspNet.Diagnostics/Views/ErrorPage.cs @@ -55,40 +55,38 @@ using Views { #line 15 "ErrorPage.cshtml" - Response.StatusCode = 500; // TODO: Response.ReasonPhrase = "Internal Server Error"; Response.ContentType = "text/html; charset=utf-8"; - Response.ContentLength = null; // Clear any prior Content-Length string location = string.Empty; #line default #line hidden WriteLiteral("\r\n\r\n(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, 540), false)); + WriteAttribute("lang", Tuple.Create(" lang=\"", 431), Tuple.Create("\"", 492), + Tuple.Create(Tuple.Create("", 438), Tuple.Create(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, 438), false)); WriteLiteral(" xmlns=\"http://www.w3.org/1999/xhtml\">\r\n \r\n \r\n "); -#line 26 "ErrorPage.cshtml" +#line 24 "ErrorPage.cshtml" Write(Resources.ErrorPageHtml_Title); #line default #line hidden WriteLiteral("\r\n