Skip to content

ADR: Error handling

ADR: Error handling, one shape everywhere

Status
accepted
Date
2026-09-02
Shipped as
1.0.0.29

Steve's ask, in the same message as the telemetry: "hook up error handling and log every API call and error, and every React error." The README also promised this at the start of the build and it was still open.

In plain words

Every error the site returns, from any endpoint, has the same shape (the RFC 9457 standard) with a plain message and a trace id, and a crash in one part of the page no longer blanks the whole screen. Every error, from the server or the browser, shows up on the Admin tab.

What that is worth: a developer handles errors one way everywhere and can find any one from its trace id, and the organization hears about a problem from its own dashboard before a customer reports it.

Context

Three gaps, all of them honest ones the README already listed:

  1. An unhandled exception was a shapeless 500. The error middleware recorded it for the Admin tab and rethrew, and the framework answered with an empty body. A caller learned nothing, and the response looked the same as a proxy failure.
  2. Two different 400 bodies. A rejected query answered { "error": "..." } and a rejected bid answered { "reason": "..." }. The browser had to know which endpoint it had called to read the message, and a new endpoint had a coin flip to make.
  3. A render crash was a white page. React unmounts the whole tree when a render throws. There was no boundary, so a bug in one component took the site with it, and nothing about it ever reached the Admin tab.

Decision

One failure shape: RFC 9457 ProblemDetails. AddProblemDetails plus UseExceptionHandler turn every unhandled exception into application/problem+json with a status, a title and a trace identifier. Every deliberate 400 uses Results.Problem(...) with the human-readable message in detail, so a query rejection and a bid rejection read the same way:

{ "type": "...", "title": "The bid was rejected", "status": 400,
  "detail": "Your bid must be at least $23,300.", "traceId": "00-a1b2..." }

The 404 for an unknown vehicle stays an empty 404: there is nothing to say that the status code does not. (That sentence stopped being true without a test noticing, and the addendum at the end of this record says when and what replaced it.)

The browser reads detail first. src/lib/data.ts prefers detail, falls back to the old reason and error keys so nothing breaks mid-deploy, and then to a generic sentence. One helper, used by every call.

Every request is logged, as structured JSON. AddHttpLogging records the method, path, status and duration for every API call; the console formatter writes JSON so a log line is machine-readable wherever it lands (ADR: Telemetry, when it ships, sends the same events to Application Insights). Static files and the SPA fallback are excluded, or the log is mostly bundle chunks.

A React error boundary at the root. ErrorBoundary wraps <App /> in app/mount.tsx. A render crash shows what happened plus two ways out (reload, or back to the inventory with the query string dropped) instead of a blank page. Errors that never reach a boundary, thrown in an event handler or an unhandled promise rejection, are caught by window.onerror and window.onunhandledrejection.

Browser errors land where server errors already do. All three paths POST to /api/errors/client, which records into the same ring buffer the Admin tab reads, tagged with the page the visitor was on. The Admin tab's Recent errors card now shows both sides of the app.

In the code

The handler and the logging, in api/TheYard.Api/Program.cs:

// Every failure answers RFC 9457 ProblemDetails (ADR-023): one shape for a
// rejected query, a rejected bid and an unhandled exception alike, so a caller
// reads one field, `detail`, for the message. The trace identifier ties the
// response to the request's log line.
// A failure nothing planned (the sign-in challenge, an address that maps to nothing) arrives
// here with a title and no sentence; it gets the sentence its status means, so every
// failure carries a `detail` a person can read.
builder.Services.AddProblemDetails(options =>
    options.CustomizeProblemDetails = context =>
    {
        context.ProblemDetails.Extensions["traceId"] = context.HttpContext.TraceIdentifier;
        context.ProblemDetails.Detail ??= context.ProblemDetails.Status switch
        {
            StatusCodes.Status401Unauthorized => "Sign in to do that: this needs an account.",
            StatusCodes.Status403Forbidden => "This account may not do that.",
            StatusCodes.Status404NotFound => "Nothing answers at that address.",
            StatusCodes.Status405MethodNotAllowed => "That address does not answer that method.",
            _ => "The request was not answered.",
        };
    });
// What goes in that shape when nothing planned the failure: which exceptions
// are the caller's fault and may say so, what a 500 is allowed to reveal, and
// the log line that makes the returned trace id worth having (ADR-030).
builder.Services.AddExceptionHandler<ProblemHandler>();
// A body that will not parse answers a bare 400 with nothing in it by default,
// because the framework would rather not spend an exception on a bad request.
// That left one kind of failure on this API with no sentence in it. One shape
// for every failure is worth an exception on a request that was already wrong
// (ADR-030).
builder.Services.Configure<RouteHandlerOptions>(options => options.ThrowOnBadRequest = true);

// No HTTP logging middleware. AddHttpLogging and UseHttpLogging stood here from
// the first commit to the pipeline lane, and never wrote a line: the logging
// configuration has always held Microsoft.AspNetCore at Warning, and the
// middleware logs at Information, so it checked, found itself filtered, and
// passed every request on. Every request is already recorded twice, by the
// timing ring in RequestPipeline.cs and by OpenTelemetry's request telemetry to
// Application Insights, whose daily cap a line per request would spend by noon
// (ADR: The order of the request pipeline).
builder.Logging.AddJsonConsole(options => options.IncludeScopes = false);

Live from api/TheYard.Api/Shared/Wiring/ApiRegistration.cs, region problem-details, read from this build at 99df7a8.

The endpoint browser errors report to:

// Browser errors land where server errors already do (ADR-023): a render
// crash caught by the boundary, or an unhandled rejection, POSTs here and
// shows up on the Admin tab's Recent errors card tagged with the page the
// visitor was on. Status 0 marks the entry as coming from the browser.
app.MapPost("/api/errors/client", BrowserReport)
    .WithName("ReportBrowserError")
    .WithTags("Errors")
    .WithSummary("Report an error the browser caught")
    .WithDescription("Anonymous by design, so a crash in the page reaches the same list a crash in the server does. "
        + "The message and the stack are bounded and any address in them is masked before they are kept.")
    .ProducesProblem(StatusCodes.Status400BadRequest);

Live from api/TheYard.Api/Features/Health/ErrorEndpoints.cs, region client-errors, read from this build at 99df7a8.

A deliberate 400, in the same shape:

// All filters, sorting, and paging are optional GET parameters, applied
// server-side. The default page is the top 100 by auction time (live and
// ending soonest first). Responses are an envelope: { total, vehicles },
// with each vehicle carrying the server-derived auction facts.
// e.g. /api/vehicles?make=Ford&status=live&sort=price-asc&limit=100&offset=100
private static async Task<Results<Ok<VehiclePage>, ProblemHttpResult>> List(
    SearchVehiclesHandler handler,
    [AsParameters] VehicleQueryParams query,
    CancellationToken cancellation)
{
    if (!query.TryBuildFilter(out var filter, out var clock, out var sort, out var error))
    {
        // One failure shape for the whole API: the message a person can act on goes in
        // `detail`, never in a key only this endpoint uses.
        return TypedResults.Problem(detail: error, statusCode: 400, title: "The query could not be read");
    }
    // One handler answers the page (ADR: The onion cut into vertical slices, rule 7); this
    // file reads the query and writes the wire.
    var result = await handler.HandleAsync(new SearchVehiclesQuery(filter, clock, sort, query.EffectiveLimit, query.EffectiveOffset), cancellation);
    return TypedResults.Ok(new VehiclePage(result.Total, [.. result.Vehicles.Select(v => VehicleWire.ToWire(v, clock))]));
}

Live from api/TheYard.Api/Features/Vehicles/VehicleEndpoints.cs, region inventory-endpoint, read from this build at 99df7a8.

The boundary, and the reporter every path uses:

/**
 * The last line of defense for a render crash (ADR-023). React unmounts the
 * whole tree when a render throws, so without a boundary the visitor gets a
 * blank white page and no way back. This catches the throw, shows what
 * happened, and offers the two moves that actually help: reload, or go back
 * to the inventory with the failing query string dropped.
 *
 * It must be a class: `componentDidCatch` and `getDerivedStateFromError` have
 * no hook equivalent, and this is the one place in the app that needs them.
 * Errors thrown in event handlers and promises never reach a boundary, which
 * is why app/reportUncaughtErrors.ts also reports those (ADR: Error handling).
 */
export class ErrorBoundary extends Component<
  { children: ReactNode },
  { error: Error | null; reloading: boolean }
> {
  state: { error: Error | null; reloading: boolean } = { error: null, reloading: false };

  /**
   * Runs during the failed render: the only place state may be set from an error.
   * A stale chunk is drawn as the new version loading from this first commit, so
   * the error card never flashes before the reload; componentDidCatch takes that
   * back when it does not reload.
   */
  static getDerivedStateFromError(error: Error) {
    return { error, reloading: isStaleChunk(error) };
  }

  /**
   * Runs after: side effects belong here, never in the method above. A chunk
   * the page names that a deploy has since replaced is not a crash: the page
   * loads again onto the new version, once (src/lib/staleChunk.ts), and says
   * so for the moment the reload takes. Anything else is reported and shown.
   */
  componentDidCatch(error: Error, info: ErrorInfo) {
    const reloaded =
      isStaleChunk(error) &&
      reloadOnce(tabStorage(), () => window.location.reload(), { online: navigator.onLine });
    if (reloaded) return;
    if (this.state.reloading) this.setState({ reloading: false });
    reportClientError(error, info.componentStack ?? undefined);
  }

  render() {
    if (!this.state.error) {
      return this.props.children;
    }
    if (this.state.reloading) {
      return (
        <div
          className={`${styles.wrap} op-glass op-solid`}
          role="status"
          data-testid="stale-reload"
        >
          <p className={styles.body}>The site was just updated. Loading the new version.</p>
        </div>
      );
    }
    return (
      <div className={`${styles.wrap} op-glass op-solid`} role="alert" data-testid="render-error">
        <h1 className={styles.title}>Something went wrong on this page.</h1>
        <p className={styles.body}>
          The inventory is still running. Reload, or go back to the full list and try again.
        </p>
        <p className={styles.detail}>{this.state.error.message}</p>
        <div className={styles.actions}>
          <button type="button" className={styles.primary} onClick={() => window.location.reload()}>
            Reload the page
          </button>
          <button
            type="button"
            className={styles.secondary}
            onClick={() => {
              window.location.href = window.location.pathname;
            }}
          >
            Back to the inventory
          </button>
        </div>
      </div>
    );
  }
}

Live from src/components/shared/ErrorBoundary/ErrorBoundary.tsx, region boundary, read from this build at 99df7a8.

/**
 * Reports a browser-side error to the API, which records it in the same ring
 * buffer the Admin tab reads (ADR: Observability). Fire and forget on purpose:
 * a failed report must never replace the error the visitor is already seeing.
 */
export function reportClientError(error: unknown, componentStack?: string): void {
  const message = error instanceof Error ? error.message : String(error);
  const stack = error instanceof Error ? error.stack : undefined;
  try {
    void request(
      '/api/errors/client',
      {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        // keepalive lets the report survive a navigation away from the page.
        keepalive: true,
        body: JSON.stringify({
          message,
          stack: (componentStack ?? stack ?? '').slice(0, 2000),
          path: window.location.pathname + window.location.search,
        }),
      },
      { background: true }
    ).catch(() => {});
  } catch {
    // A browser that refuses the request tells us nothing useful; stay quiet.
  }
}

Live from src/components/shared/ErrorBoundary/ErrorBoundary.tsx, region report, read from this build at 99df7a8.

The two window-level handlers, in src/app/reportUncaughtErrors.ts:

/**
 * Listens on the window for errors and rejected promises and sends each one to the API, so the
 * Admin tab shows the browser's failures beside the server's (ADR: Error handling).
 */
export function reportUncaughtErrors(): void {
  // A throw inside an event handler, a timer or a script that is not React rendering.
  window.addEventListener('error', (event) => reportClientError(event.error ?? event.message));
  // A promise that failed with nothing waiting on it.
  window.addEventListener('unhandledrejection', (event) => reportClientError(event.reason));
}

Live from src/app/reportUncaughtErrors.ts, region window-handlers, read from this build at 99df7a8.

How the browser reads a failure:

/**
 * Reads the message out of a failed response. The API answers RFC 9457
 * ProblemDetails, so `detail` is the field to trust (ADR: Error handling);
 * `reason` and `error` are the two shapes it used to answer with, kept here
 * so a browser holding an old bundle still shows a real message during a roll.
 */
async function problemDetail(response: Response, fallback: string): Promise<string> {
  // A failure can arrive with no JSON body (an edge error page, an empty 502): anything
  // unreadable falls back to the caller's sentence rather than throwing over the first error.
  let body: { detail?: string; reason?: string; error?: string } | null = null;
  try {
    body = await response.json();
  } catch {
    body = null;
  }
  return body?.detail ?? body?.reason ?? body?.error ?? fallback;
}

Live from src/lib/data.ts, region problem-detail, read from this build at 99df7a8.

Consequences

  • A caller can read one field, detail, for the message on any failure from this API.
  • traceId on every problem response ties a visitor's report to a log line, and to a request in Application Insights once telemetry ships.
  • The ring buffer holds fifty entries, browser and server together, and is in memory: it is a demo's observability, not an audit log. That limitation is recorded in ADR: Observability and is the reason the telemetry record exists.
  • The boundary catches render crashes only. Event handlers and promises are covered by the window handlers, which is why both exist.
  • Reporting is best effort. keepalive lets a report survive the navigation away, and a failed report is swallowed rather than replacing the error the visitor already sees.

Addendum, 2026-09-15: the empty 404 was not empty

UseStatusCodePages was added to the pipeline after this record was written, and with the problem-details service registered it fills any bare status code with a ProblemDetails body on the way out. So the "empty 404" above had been answering application/problem+json with a generic title for some time, and nothing said so. When the API started describing itself (ADR: The API describes itself), every declared response had to match the wire, and a declaration of "empty" would have been the one lie in the document. The endpoints that answer 404 and 401 now say so themselves, with a title and a one-sentence detail in the shape every other failure here uses. The status codes did not change; the body names what was not found instead of the middleware naming nothing.

Addendum, 2026-09-19: the frames, and still not the message

Steve, on a 500 he found on his own Admin tab: recent errors should be a table, and they should show the stack trace with line numbers. Both, and the second one needs saying carefully, because this record's own rule is that the list at /api/errors carries an exception's type and never its message: a message is where a framework writes a filesystem path, a connection detail or the value that broke a constraint, and the list is public.

The frames are a different thing from the message. A frame is a method, the file it is in and the line it is on, which is source this repository already publishes in full, and it is the whole distance between "something threw on /api/admin/machines" and "line 160 of Machines.cs reads a decimal into a double". So the ring carries up to twelve frames per entry, the endpoint serves them, the card shows them behind a disclosure in a table with the time, the status and the path, and the message stays out. A test throws on the self-test endpoint and asserts both halves: the frames are there, and the sentence the self-test throws with is not.

Where it sits

The server half sits in the host Api: the problem-details registration in Composition/ApiRegistration.cs, the browser-report endpoint in Endpoints/ErrorEndpoints.cs and the error ring in Observability.cs. The browser half is front end: ErrorBoundary is a shared component, the detail reader is in src/lib/data.ts, and no inner ring changed, which suits a decision about how HTTP failures look. Single responsibility, a class with one reason to change, shows in the split: one helper in src/lib reads every failure body, and the boundary handles render crashes while the window handlers take everything else. The cost is a fifty-entry ring that empties on every roll, and a product with an audit requirement would send these errors to a durable store first.

Files

Addendum, 2026-09-25 (1.0.3.27): a page left open across a deploy

Steve, on his iPhone, on the Admin tab after 1.0.3.26 rolled: every card he opened showed this boundary with "Importing a module script failed". His page was 1.0.3.25's. It asks for each card's chunk by the hashed name that build gave it, and the roll had replaced those files with 1.0.3.26's. Nothing was broken on the server, and a reload fixed it; the boundary was the wrong answer to a stale page. From 1.0.3.27 an error that is a chunk the server no longer has (each engine words it differently; src/lib/staleChunk.ts knows the three, and Vite's own preload error) loads the page again onto the new version, once, and says "The site was just updated" for the moment that takes, from the boundary's first frame, so the error card never flashes first. A page that is offline is not reloaded, since the same words come from a chunk that had no network and a reload would land on the browser's offline page. A second such failure inside a minute is not a stale page, so it is reported and shown, never looped. admin.spec stands in for a deploy by refusing the Errors card's chunk: refused once, the page reloads and the card opens; refused every time, the page reloads once and then shows the error.

Addendum, 2026-10-07 (1.0.3.88): the boundary's place

The boundary still wraps <App /> at the root, now in src/app/TheYard.tsx, the one tree that both the browser and the build draw (ADR: The landing page rendered at build time, server rendering as the goal); app/mount.tsx puts that tree on the page.

Addendum, 2026-10-07 (1.0.3.92): the request log that never wrote

"Every request is logged, as structured JSON" above was not true on any day of this project. AddHttpLogging and UseHttpLogging were wired from the first commit, but the logging configuration has always held Microsoft.AspNetCore at Warning and the HTTP logger writes at Information, so it found itself filtered on every request and wrote nothing. Both are removed rather than switched on: every request is recorded already, by the timing ring on the Admin tab and by the request telemetry sent to Application Insights, and a line per request through the logger would spend that service's daily cap, which is already reached most afternoons. The error record moved up beside the exception handler in the same version, so a failure while reading the session now reaches the Admin tab (ADR: The order of the request pipeline).

Addendum, 2026-10-29: the ring both sides record into has its own file

The error ring named above is now api/TheYard.Api/Shared/Observability/ErrorRing.cs rather than Observability.cs, split off by job when the file came within four lines of the three-hundred cap (ADR: Observability, the addendum of 2026-10-29). Nothing it records, keeps or withholds changed: the exception's message is still not in it.

Enforced by

Not machine-checked. Held by review.

Store

The Yard, home

Steven StoutStaff .NET engineerRemote

Production .NET that stays up. Built with AI, measured on every version.

Steven Stout

Hi, I'm Steve. I build the systems that make delivery boring in the best way, then coach people inside them.

  • 200+

    production releases, none took the platform down

    Conway Investment Research, 2019 to 2024. Sole engineer for the first three years on a live financial advisory platform.

    See how
  • 50%

    shorter release cycle

    Storee Construction, 2024 to 2026. Four ASP.NET applications onto one .NET 10 platform on Azure, with a four-person team I hired and mentored.

    See how

01Verified

Steven Stout

A real person, and the work is mine.

  • Identity

    Checked by LinkedIn, not by me.

    LinkedIn verified my government ID.

  • Running system

    You are looking at it.

    Version 1.0.5.273 is live. Nothing ships unless it passes the automated gates first.

That's who I am. Here's how I work.

02Strengths

Ten strengths, each with the proof beside it. The numbers match my resume and LinkedIn, and the live ones are read from this site as you look.

  • 01

    Keeps production alive

    200+ releases on a live financial platform, and none took it down. Sole engineer for the first three years.

    See the Conway work

  • 02

    Simplifies messy systems

    Four ASP.NET applications onto one .NET 10 platform on Azure, and a release cycle 50% shorter.

    See the Storee work

  • 03

    Marries dev and infrastructure

    Blue-green releases at Storee that flipped in about 5 seconds with nobody signed out, and Azure Front Door at two employers.

    See this site's cloud

  • 04

    Designs data that holds up

    White-label clients on one shared database, the isolation built into the data, and no cross-client issues.

    See the Conway work

  • 05

    Keeps AI-written code honest

    15 quality numbers that may only improve: 273 releases in a row with none worse.

    See the quality numbers

  • 06

    Resourceful

    $30,000+ cut from Azure and vendor spend at Conway. This site's Azure bill is public, resource by resource.

    See the bill

  • 07

    Builds people up

    Hired and mentored a four-person team. 13 colleagues recommend me by name.

    Read the recommendations

  • 08

    Takes ideas to production

    This site: designed, built and run solo, with 352 versions measured through one gate.

    How it was built

  • 09

    Writes clearly

    Posts on real engineering problems, in plain words.

    Read the blog

  • 10

    Owns decisions

    153 decision records, each with its tradeoff and the call I made.

    See the decision records

Built with AI. Gated so quality never slides.

Steven Stout’s showcase: a used-vehicle auction on .NET and React, built and run by one engineer, live on Azure, with its decisions written down.

No quality number has gotten worse at any release since 1.0.5.1 on 10 November: 273 releases in a row.

One engineer, an AI agent and a gate that checks 15 quality numbers on every release. 352 versions measured, with every result public, the worst days included.

82 releases in the last 7 days, each through the same gate.

Async methods with no CancellationToken, may only fall

11091.0.3.1391.0.5.2731.0.4.115, the ratchet
Every number, on the Quality page

TheYard, right nowLive

Answering in

unread

02505007501,000

server time at p95, the usual five minutes of the last day.

Azure this month

unread

the bill, by resource, read from Azure. The Admin tab shows every line.

Since the last roll

unread

 

rolled in place by the pipeline, behind one gate.

People who have visited

unread

30 days agotoday

Scanners, my own tools' and my own reads left out.

03Start here

04Where teams are heading

How I see engineering with AI, and the setup on this site that backs it.

AI-augmented teams · Architecture and ownership

AI writes more of the code. Someone still has to own what ships.

Teams are going to be heavily AI-augmented. That moves the senior job toward architecture and technical ownership: deciding what to build in pieces small enough to verify, and owning what ships.

  • Task tracking will get automated too. Judgment stays human: seeing a design fail at scale before it does.
  • On this site the AI's first design held the whole catalogue in memory. I caught it when the App Service plan hit 82 per cent and moved the work to the store.

What the AI wrote, what I decided

07Tech stack

  •    
  •    
  •    

Read from the build, never typed by hand

What TheYard runs on

What we use, where it runs, its AWS twin, the version as built, and the file that proves it.

Cloud patterns, not one vendor: every Azure piece in this stack has a named AWS twin. Mapped, not yet run on AWS.

  • .NET 1114 projects
  • React 19.3TypeScript 7.0
  • Azure9 Bicep files
  • 11GitHub Actions workflows
The whole stack, with the file that proves each line

The last release the gate stopped was 1.0.5.258 on 2 December, at its browser 1 of 2 job.

08How it is built

09How it is run

10Who and why

Next step

11What has to work, where is it today, and what does success look like?

Book 20 minutes opens an email to me with those three questions in it, and I answer with a time.

Book 20 minutes

Your email app should have opened with this message. If it did not, copy it and send it from any mailbox.

Or message me on LinkedIn