diff --git a/src/Lumina.Presentation.Web/Common/Middlewares/ExceptionHandlingMiddleware.cs b/src/Lumina.Presentation.Web/Common/Middlewares/ExceptionHandlingMiddleware.cs
index a66e1484..f7842e57 100644
--- a/src/Lumina.Presentation.Web/Common/Middlewares/ExceptionHandlingMiddleware.cs
+++ b/src/Lumina.Presentation.Web/Common/Middlewares/ExceptionHandlingMiddleware.cs
@@ -81,8 +81,19 @@ public class ExceptionHandlingMiddleware
serverUnreachable = IsApiOutage(ex),
errorMessage = _stringLocalizer["InternalServerError"].Value
});
- else if (IsNavigationRequest(context))
+ else if (IsExplicitNavigationRequest(context))
await WriteHtmlErrorResponseAsync(context, (int)HttpStatusCode.InternalServerError, _stringLocalizer["InternalServerError"].Value).ConfigureAwait(false);
+ else if (IsNavigationRequest(context))
+ {
+ // A caller that states no preference is not necessarily a browser, so it keeps the historical JSON envelope instead of
+ // receiving an HTML page it cannot consume.
+ await WriteJsonResponseAsync(context, new
+ {
+ success = false,
+ serverUnreachable = IsApiOutage(ex),
+ errorMessage = _stringLocalizer["InternalServerError"].Value
+ });
+ }
else
await WriteStatusOnlyResponseAsync(context, (int)HttpStatusCode.InternalServerError).ConfigureAwait(false);
}
@@ -348,6 +359,22 @@ public class ExceptionHandlingMiddleware
|| string.Equals(context.Request.Headers["Sec-Fetch-Dest"].ToString(), "document", StringComparison.OrdinalIgnoreCase);
}
+ ///
+ /// Checks if the current request explicitly identifies itself as a browser navigation, rather than merely stating no preference.
+ ///
+ ///
+ /// A caller that sends no Accept header at all may be a script, a probe, or an API client, so it is not safe to answer it with
+ /// an HTML page. Only a request that asks for HTML or carries the browser navigation headers is treated as a page here.
+ ///
+ /// The for the current request.
+ /// when the request is explicitly a browser navigation, otherwise.
+ private static bool IsExplicitNavigationRequest(HttpContext context)
+ {
+ return context.Request.Headers.Accept.Any(accept => accept?.Contains("text/html", StringComparison.OrdinalIgnoreCase) == true)
+ || string.Equals(context.Request.Headers["Sec-Fetch-Mode"].ToString(), "navigate", StringComparison.OrdinalIgnoreCase)
+ || string.Equals(context.Request.Headers["Sec-Fetch-Dest"].ToString(), "document", StringComparison.OrdinalIgnoreCase);
+ }
+
///
/// Writes a JSON response to the client.
///
diff --git a/tests/UnitTests/Lumina.Presentation.Web.UnitTests/Common/Middlewares/ExceptionHandlingMiddlewareTests.cs b/tests/UnitTests/Lumina.Presentation.Web.UnitTests/Common/Middlewares/ExceptionHandlingMiddlewareTests.cs
index 4263737b..fb6baeb5 100644
--- a/tests/UnitTests/Lumina.Presentation.Web.UnitTests/Common/Middlewares/ExceptionHandlingMiddlewareTests.cs
+++ b/tests/UnitTests/Lumina.Presentation.Web.UnitTests/Common/Middlewares/ExceptionHandlingMiddlewareTests.cs
@@ -80,7 +80,8 @@ public class ExceptionHandlingMiddlewareTests
string body = await ReadResponseBodyAsync(httpContext);
using JsonDocument jsonDocument = JsonDocument.Parse(body);
Assert.False(jsonDocument.RootElement.GetProperty("success").GetBoolean());
- Assert.True(jsonDocument.RootElement.GetProperty("serverUnreachable").GetBoolean());
+ // An ordinary bug is not an unreachable server, so the client must not switch to the outage path for it.
+ Assert.False(jsonDocument.RootElement.GetProperty("serverUnreachable").GetBoolean());
Assert.Equal("localized:InternalServerError", jsonDocument.RootElement.GetProperty("errorMessage").GetString());
_mockLogger.Received(1).Log(
LogLevel.Error,
@@ -95,6 +96,7 @@ public class ExceptionHandlingMiddlewareTests
{
// Arrange
DefaultHttpContext httpContext = CreateHttpContext(culture: "de-DE");
+ httpContext.Request.Headers.Accept = "text/html";
_nextDelegate = _ => throw new InvalidOperationException("Unexpected failure");
// Act
@@ -112,6 +114,26 @@ public class ExceptionHandlingMiddlewareTests
Arg.Any>());
}
+ [Fact]
+ public async Task InvokeAsync_WhenNextThrowsNonApiExceptionAndRequestStatesNoPreference_ShouldWriteFailureJson()
+ {
+ // Arrange
+ // A caller that sends no Accept header is not proven to be a browser, so it keeps the historical JSON envelope.
+ DefaultHttpContext httpContext = CreateHttpContext();
+ _nextDelegate = _ => throw new InvalidOperationException("Unexpected failure");
+
+ // Act
+ await _sut.InvokeAsync(httpContext);
+
+ // Assert
+ Assert.Contains("application/json", httpContext.Response.ContentType);
+ string body = await ReadResponseBodyAsync(httpContext);
+ using JsonDocument jsonDocument = JsonDocument.Parse(body);
+ Assert.False(jsonDocument.RootElement.GetProperty("success").GetBoolean());
+ Assert.False(jsonDocument.RootElement.GetProperty("serverUnreachable").GetBoolean());
+ Assert.Equal("localized:InternalServerError", jsonDocument.RootElement.GetProperty("errorMessage").GetString());
+ }
+
[Fact]
public async Task InvokeAsync_WhenNextThrowsNonApiExceptionAndRawRequest_ShouldWriteStatusCodeWithoutBody()
{