Skip to content

Answer storefront errors with 500 instead of a redirect to the error page - #1175

Merged
grandnode-dev merged 2 commits into
developfrom
fix/error-page-status-code
Dec 3, 2026
Merged

grandnode-dev merged 2 commits into
developfrom
fix/error-page-status-code

Conversation

@grandnode-dev

@grandnode-dev grandnode-dev commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Type: bugfix

Issue

GrandExceptionHandler answered an unhandled exception in a storefront (non-API) request with Response.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.htm itself, read through IWebHostEnvironment.WebRootFileProvider:

  • status 500, Content-Type: text/html; charset=utf-8, Cache-Control: no-store;
  • no MVC and no database access, so the page still renders while MongoDB is down (its stylesheet /assets/errorpage/error.css is a static file);
  • when the exception is a MongoConnectionException or a TimeoutException (what the driver throws when server selection times out), status 503 with Retry-After: 30 instead. This is a single type check, not an exception taxonomy;
  • if the file is missing, a short plain-text body.

The API branch (Bearer requests, ProblemDetails 500) is unchanged.

errorpage.htm is 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, carries noindex, and no longer claims that staff was notified. assets/errorpage/error.css is 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.htm on an error. A customised errorpage.htm that links /assets/errorpage/error.css loses its styles, because that file is removed.

Testing

  1. dotnet test src/Tests/Grand.Web.Common.Tests --filter GrandExceptionHandlerTests (new): web request gets 500 + the static page + no-store and no Location; TimeoutException and MongoConnectionException get 503 + Retry-After; an API request still gets a 500 ProblemDetails with no Retry-After; a response that has already started returns false.
  2. Full suite: dotnet test GrandNode.slnx passes (23 test projects).
  3. The new page checked in Chromium at desktop width (light and dark scheme) and at 375 px.
  4. Not checked against a running storefront with MongoDB stopped.

…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.
Copilot AI balanced review requested due to automatic review settings December 2, 2026 20:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.
@grandnode-dev
grandnode-dev merged commit 2da64b0 into develop Dec 3, 2026
9 checks passed
@grandnode-dev
grandnode-dev deleted the fix/error-page-status-code branch December 3, 2026 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants