ADR: The exception handler, and the endpoint that proves it
- Status
- accepted
- Date
- 2026-09-03
- Shipped as
- 1.0.0.40
This extends
ADR: Error handling rather than replacing it: that record decided the shape,
this one decides what goes in it when nobody wrote the failure.
In plain words
This page decides what the API says when code fails in a way nobody planned for (an unhandled exception). The caller gets one fixed sentence and a trace id, while the full details go to one log line with the same id. A deliberate failure endpoint proves the whole path works in production.
What that is worth: a developer can find any crash from the id a reader quotes, and the organization's error messages never draw a map of the inside of the process for whoever asks.
Context
ADR: Error handling shipped AddProblemDetails, UseExceptionHandler() and a
traceId on every response, and the suite proves the shape on a rejected
query, a rejected bid, a rejected browser report and a 404. Every one of those
is a failure an endpoint returns on purpose.
The failure nobody wrote code for was never tested, and there is a reason it
was easy to miss: the built-in handler does produce a correctly shaped 500, so
reading the code gives no sign anything is missing. Running it does. Three
things were wrong.
The developer exception page was answering, not the handler. WebApplication
inserts it automatically in Development, in front of everything the app
registers. So the response a developer sees when something throws, and the
response a caller gets in production, were never the same response, and only
one of them had ever been looked at.
The bare 500 says nothing at all. No title, no detail. A caller gets a
status code and a trace id and no sentence, which is worse than it sounds: the
front end renders detail for every other failure, so a crash was the one case
that produced an empty message box.
Nothing decided what a 500 may reveal. Nothing leaked, but nothing was
stopping it either. The next person to add Detail = ex.Message to make
debugging easier would have been making a security decision without knowing it.
Decision
A custom IExceptionHandler, because three questions need an application's
answer and the framework cannot have one.
Which failures are the caller's fault. BadHttpRequestException is the
framework saying the request was malformed, which is a different claim from
"this server broke". It carries its own status code and its message describes
the caller's own input, so it is answered at its status, with its message, and
logged as a warning rather than an error.
Minimal APIs do not raise that exception by default. A body that will not parse
answers a bare 400 with nothing in it, on the reasoning that a bad request
should not cost an exception. That left exactly one kind of failure on this API
without a sentence in it, so RouteHandlerOptions.ThrowOnBadRequest is on and
binding failures join everything else. The test that proves this is the test
that found it: it was written to assert one shape for every 400, and it failed
on the first run against the API it was describing.
What a server failure may reveal. One fixed sentence, ProblemHandler.ServerDetail,
and the trace id. The exception message, the type and the stack stay inside.
An error message that varies with the exception is a map of the inside of the
process, drawn for whoever asks.
What makes the trace id worth returning. One structured log line, written
before the response, carrying the exception, the method, the path, the status
and the same trace id. The response can afford to say nothing precisely because
everything it withholds is in that line.
There is a fourth case worth naming: a caller who hangs up mid-response
produces an OperationCanceledException on an aborted request. There is no
socket left to answer on, so nothing is written, and it does not belong in the
Admin tab's error list as a server failure.
The first version of this logged it at Debug and returned without touching the
status, which leaves a 200 on a request that was never answered. That is wrong
in the one situation it matters: the opening shape of a real outage is a slow
server, clients timing out, and every abandoned request landing here. A version
that logs at Debug and reports 200 makes exactly that invisible in the request
log and in telemetry at once. It logs at Information now and sets 499, which is
nginx's convention for a client that left and is understood widely enough to
read as what it is. That correction came from the staff review of the same
night's diff, not from a failure in production.
A deliberate failure endpoint, /api/admin/selftest/exception. Every other
endpoint here is written not to throw, which is why the exception path had
never once run in production: the middleware's catch, the ring buffer's
record, the handler's log line and the Application Insights exceptions table
the Admin tab reads were all assumed to work end to end and none of them had
been asked. This endpoint asks. It throws a sentence chosen to be searchable,
and the answer it produces is the answer any real bug would produce.
It is public, like every other endpoint on this site, and cheaper to serve than
the inventory query. DELETE /api/bids, which resets the auction for everyone,
has been public since the first deploy.
The exposure it does add is worth naming rather than leaving for someone to
find: the recent-errors buffer holds fifty entries, so fifty anonymous requests
to this endpoint would push every real error out of the Admin tab, and each one
also sends an exception to Application Insights, which is billed by ingestion.
On a demo with a public reset endpoint that is an acceptable trade. On anything
with users it would need a rate limit or an authenticated route, and the reason
it does not have one here is the deployment, not the design.
The tests run against Production. ProductionApi is a factory that sets
the environment, because asking what a caller sees means asking the environment
a caller reaches. This is the point the whole record turns on: a test against
the default environment would have passed while asserting the developer
exception page's HTML.
In the code
The handler (api/TheYard.Api/Shared/Observability/ProblemHandler.cs):
public const int ClientClosedRequest = 499;
public const string ServerDetail =
"Something went wrong on the server. Quote the trace id below and the "
+ "request can be found in the logs.";
public static bool CallerLeft(HttpContext context, Exception exception) =>
exception is OperationCanceledException cancelled
&& (context.RequestAborted.IsCancellationRequested
|| (context.RequestAborted.CanBeCanceled && cancelled.CancellationToken == context.RequestAborted));
public async ValueTask<bool> TryHandleAsync(
HttpContext context,
Exception exception,
CancellationToken cancellationToken)
{
if (CallerLeft(context, exception))
{
if (!context.Response.HasStarted)
{
context.Response.StatusCode = ClientClosedRequest;
}
log.LogInformation(
"{Method} {Path} was abandoned by the caller; nothing was written",
context.Request.Method,
context.Request.Path);
return true;
}
bool callersFault = exception is BadHttpRequestException;
int status = callersFault
? ((BadHttpRequestException)exception).StatusCode
: StatusCodes.Status500InternalServerError;
log.Log(
callersFault ? LogLevel.Warning : LogLevel.Error,
exception,
"{Exception} on {Method} {Path} answered {Status}, trace {TraceId}",
exception.GetType().Name,
context.Request.Method,
context.Request.Path,
status,
context.TraceIdentifier);
context.Response.StatusCode = status;
bool written = await problems.TryWriteAsync(new ProblemDetailsContext
{
HttpContext = context,
Exception = exception,
ProblemDetails = new ProblemDetails
{
Status = status,
Title = callersFault
? "The request could not be read"
: "The request could not be completed",
Detail = callersFault ? exception.Message : ServerDetail,
},
});
if (!written)
{
log.LogWarning(
"Nothing was written for {Exception} on {Method} {Path}: no problem details writer accepted it, so the request will be reset",
exception.GetType().Name,
context.Request.Method,
context.Request.Path);
}
return written;
}
Live from api/TheYard.Api/Shared/Observability/ProblemHandler.cs, region exception-handler, read from this build at 99df7a8.
The five tests (api/TheYard.Tests/ExceptionHandlerTests.cs):
[Fact]
public async Task An_unhandled_exception_answers_problem_details_carrying_a_trace_id()
{
var response = await (await Buyers.SignedIn(factory)).GetAsync(SelfTest);
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
using var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
Assert.Equal(500, json.RootElement.GetProperty("status").GetInt32());
Assert.Equal("The request could not be completed", json.RootElement.GetProperty("title").GetString());
Assert.Equal(ProblemHandler.ServerDetail, json.RootElement.GetProperty("detail").GetString());
Assert.False(string.IsNullOrWhiteSpace(json.RootElement.GetProperty("traceId").GetString()));
}
[Fact]
public async Task The_exception_never_reaches_the_caller()
{
string body = await (await (await Buyers.SignedIn(factory)).GetAsync(SelfTest)).Content.ReadAsStringAsync();
Assert.DoesNotContain("self-test", body, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(nameof(InvalidOperationException), body, StringComparison.Ordinal);
Assert.DoesNotContain("at TheYard", body, StringComparison.Ordinal);
}
[Fact]
public async Task A_thrown_request_reaches_the_list_the_admin_tab_reads()
{
await (await Buyers.SignedIn(factory)).GetAsync(SelfTest);
string errors = await _client.GetStringAsync("/api/errors");
Assert.Contains(SelfTest, errors);
}
[Fact]
public async Task The_list_carries_the_frames_and_still_refuses_the_message()
{
await (await Buyers.SignedIn(factory)).GetAsync(SelfTest);
var entries = await _client.GetFromJsonAsync<JsonElement>("/api/errors");
var thrown = entries.EnumerateArray()
.First(entry => entry.GetProperty("path").GetString() == SelfTest
&& entry.GetProperty("status").GetInt32() == 500);
var frames = thrown.GetProperty("frames").EnumerateArray().Select(frame => frame.GetString() ?? "").ToList();
Assert.NotEmpty(frames);
Assert.Contains(frames, frame => frame.Contains("TheYard", StringComparison.Ordinal));
Assert.True(frames.Count <= StackFrames.Most, $"{frames.Count} frames is more than a reader gets through");
Assert.DoesNotContain("self-test", string.Join(" ", frames), StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task A_malformed_body_is_the_callers_fault_and_says_which_part()
{
var response = await _client.PostAsync(
"/api/errors/client",
new StringContent("{ this is not json", Encoding.UTF8, "application/json"));
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
Assert.Equal("application/problem+json", response.Content.Headers.ContentType?.MediaType);
using var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
Assert.Equal(400, json.RootElement.GetProperty("status").GetInt32());
Assert.Equal("The request could not be read", json.RootElement.GetProperty("title").GetString());
Assert.NotEqual(ProblemHandler.ServerDetail, json.RootElement.GetProperty("detail").GetString());
}
[Fact]
public async Task The_shape_of_a_crash_and_the_shape_of_a_rejected_query_are_the_same()
{
using var crash = JsonDocument.Parse(
await (await (await Buyers.SignedIn(factory)).GetAsync(SelfTest)).Content.ReadAsStringAsync());
using var rejected = JsonDocument.Parse(
await (await _client.GetAsync("/api/vehicles?sort=alphabetical")).Content.ReadAsStringAsync());
var crashFields = crash.RootElement.EnumerateObject().Select(p => p.Name).Order().ToArray();
var rejectedFields = rejected.RootElement.EnumerateObject().Select(p => p.Name).Order().ToArray();
Assert.NotEmpty(rejectedFields);
Assert.Equal(rejectedFields, crashFields);
}
Live from api/TheYard.Tests/ExceptionHandlerTests.cs, region exception-tests, read from this build at 99df7a8.
Consequences
- A crash now renders a sentence in the browser instead of an empty message
box, and the sentence tells the reader what to quote.
- The shape of a crash and the shape of a rejected query are asserted to have
the same fields, so a client needs one parser and not two. That test is what
will fail if someone later adds a field to one path only.
- The exception path can be exercised against the live container, which is how
the Admin tab's Application Insights card gets checked against a real
exception rather than an absence of them.
/api/admin/selftest/exception will appear in the Admin tab's recent-errors
list every time it is used, which is correct: it is a 500 and the list is the
list of 500s.
- One thing is deliberately not done: known domain exceptions are not mapped to
statuses, because this application does not throw any. Every rule that can
reject something returns a result instead. If that changes, the mapping goes
here.
Where it sits
ProblemHandler is an IExceptionHandler in the host Api, registered in Composition/ApiRegistration.cs, and it is the one place that decides what an unplanned failure may say. Open/closed applies directly: the framework's exception pipeline stays untouched while the application adds its answer by implementing the interface, keeping the fixed sentence beside the log line in that single class. The cost is a public self-test endpoint that fifty anonymous calls could use to flush the error ring and add Application Insights ingestion. A site with real users would put that endpoint behind authentication or a rate limit, and would map domain exceptions here if the rules ever started throwing them.
Addendum, 2026-10-21 (1.0.4.24): a sentence for the failures nothing planned
A 401 from the sign-in challenge and a 404 from an address that maps to nothing arrived as ProblemDetails with a title and no detail, so the one field a caller reads was empty on exactly the failures nobody wrote. From 1.0.4.24 CustomizeProblemDetails fills an empty detail with the sentence its status means (401 "Sign in to do that: this needs an account.", 403, 404 "Nothing answers at that address.", 405, and a general one), and every 400, 401 and 404 the status tests assert goes through Problem.Is: the problem content type, a title, a sentence in detail, and no public cache header.
Enforced by
One rule in the rulebook cites this record, each held by a test on every release.