Repository navigation
Answer storefront errors with 500 instead of a redirect to the error page - #1175
Merged
Merged
Conversation
…page Unhandled exceptions in web requests redirected to /errorpage.htm, so the browser saw 302 then 200 and HTTP metrics and load balancers saw no 5xx. The handler now writes wwwroot/errorpage.htm itself with status 500 and Cache-Control: no-store, without MVC or the database. A MongoDB connection error or server-selection timeout answers 503 with Retry-After. API requests keep the ProblemDetails response.
| private HttpContext CreateContext() | ||
| { | ||
| var env = new Mock<IWebHostEnvironment>(); | ||
| env.SetupProperty(e => e.WebRootFileProvider, new PhysicalFileProvider(_webRoot)); |
| private static string ReadBody(HttpContext context) | ||
| { | ||
| context.Response.Body.Position = 0; | ||
| return new StreamReader(context.Response.Body).ReadToEnd(); |
| DataSettingsManager.Initialize(Path.Join(Path.GetTempPath(), "grand-exception-handler-tests-settings.json")); | ||
| DataSettingsManager.Instance.LoadDataSettings(new DataSettings { ConnectionString = "mongodb://localhost" }); | ||
|
|
||
| _webRoot = Path.Combine(Path.GetTempPath(), "errorpage-" + Guid.NewGuid().ToString("N")); |
|
|
||
| _webRoot = Path.Combine(Path.GetTempPath(), "errorpage-" + Guid.NewGuid().ToString("N")); | ||
| Directory.CreateDirectory(_webRoot); | ||
| File.WriteAllText(Path.Combine(_webRoot, "errorpage.htm"), "<h1>INTERNAL ERROR</h1>"); |
errorpage.htm is now self-contained: inline styles with a dark variant, no stylesheet request, no script. It offers Try again (reloads the original URL, which now stays in the address bar) and a link to the home page, and asks crawlers not to index it. The text no longer claims that staff was notified. assets/errorpage/error.css is removed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type: bugfix
Issue
GrandExceptionHandleranswered an unhandled exception in a storefront (non-API) request withResponse.Redirect("/errorpage.htm"). The browser saw a 302 and then a 200 for the static page, so request metrics, access logs and load balancers never saw a 5xx for a failing storefront. A storefront failing on every request looked healthy from the outside.Solution
For web requests the handler now writes
wwwroot/errorpage.htmitself, read throughIWebHostEnvironment.WebRootFileProvider:Content-Type: text/html; charset=utf-8,Cache-Control: no-store;/assets/errorpage/error.cssis a static file);MongoConnectionExceptionor aTimeoutException(what the driver throws when server selection times out), status 503 withRetry-After: 30instead. This is a single type check, not an exception taxonomy;The API branch (Bearer requests, ProblemDetails 500) is unchanged.
errorpage.htmis redesigned and self-contained: inline styles with a dark variant (prefers-color-scheme), no stylesheet request and no script, so it renders whatever else is failing. It offers Try again (a plain link that reloads the original URL, which now stays in the address bar) and Go to the home page, carriesnoindex, and no longer claims that staff was notified.assets/errorpage/error.cssis removed.Heads-up for operators: dashboards and alerts on the 5xx rate will start counting storefront failures that were previously hidden behind the redirect.
Breaking changes
None in code. The URL in the address bar no longer changes to
/errorpage.htmon an error. A customisederrorpage.htmthat links/assets/errorpage/error.cssloses its styles, because that file is removed.Testing
dotnet test src/Tests/Grand.Web.Common.Tests --filter GrandExceptionHandlerTests(new): web request gets 500 + the static page +no-storeand noLocation;TimeoutExceptionandMongoConnectionExceptionget 503 +Retry-After; an API request still gets a 500 ProblemDetails with noRetry-After; a response that has already started returnsfalse.dotnet test GrandNode.slnxpasses (23 test projects).