fix(prod): map SiaxError in the 7 legacy money routes — DEPENDENCY_UNAVAILABLE/503 instead of opaque 500 (0pt#53)
CI (SIAX Cloud) / integration (push) Has been skipped
masterplan-lock / check (pull_request) Successful in 3s
masterplan-lock / check (push) Successful in 3s
CI (SIAX Cloud) / security (push) Successful in 15s
Security Scan (SIAX Cloud) / security (push) Successful in 14s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 18s
CI (SIAX Cloud) / quality (pull_request) Successful in 31s
CI (SIAX Cloud) / quality (push) Successful in 40s
CI (SIAX Cloud) / security (pull_request) Successful in 42s
CI (SIAX Cloud) / integration (pull_request) Successful in 45s

getSqlForDurableStore throws SiaxError(DEPENDENCY_UNAVAILABLE, httpStatus 503,
retryable) when a money-bearing store would fall back to an in-memory Map in a
production runtime (the durable-store guard). The /api/0pt/* surface already
maps it via src/http/siax-error-response.ts; the seven legacy routes outside
the middleware matcher did not — the documented 503 leaked as Next's opaque
500 with no body.

- app/api/siax-error-mapping.ts: shared isSiaxError + siaxErrorResponseForLegacy
- orders (POST/GET), orders/[id] (GET/PATCH), subscriptions (GET/POST),
  payments/deposit, payments/release, admin/metrics: try/catch -> mapper,
  each route's own typed error contract preserved (idempotency conflict 409 etc.)
- webhooks/stripe: store outage -> 503 received:false reason:store_unavailable
  so Stripe's delivery retry path stays open (never a silent received:true)
- tests/api/legacy-routes-siax-error-mapping.test.ts: pins the 503 + body
This commit is contained in:
2026-09-22 04:33:57 +02:00
parent 1d381f670f
commit c7fd9d45a2
9 changed files with 188 additions and 14 deletions
@@ -0,0 +1,57 @@
/**
* 0pt#53: `getSqlForDurableStore` (lib/db.ts) throws
* `SiaxError("DEPENDENCY_UNAVAILABLE")` (httpStatus 503, retryable) when a
* money-bearing store would silently fall back to an in-memory Map in a
* production runtime. The /api/0pt/* surface maps that error via
* src/http/siax-error-response.ts; the SEVEN legacy routes outside the
* middleware matcher (orders, orders/[id], subscriptions, payments/deposit,
* payments/release, webhooks/stripe, admin/metrics) did NOT, so the documented
* 503 leaked as Next's opaque 500 with no error body.
*
* These tests pin the mapping itself: `isSiaxError` accepts real SiaxErrors,
* `siaxErrorResponseForLegacy` renders the pinned `.httpStatus` and the
* structured `toJSON()` body (code + source + retryable, no internals), and
* — most importantly — the DEPENDENCY_UNAVAILABLE code maps to 503 with
* `retryable: true` so callers (Stripe retries, the UI) can distinguish a
* transient store outage from a hard failure.
*/
import { describe, it, expect } from "vitest";
import { NextResponse } from "next/server";
import { SiaxError } from "@/src/platform/errors";
import { isSiaxError, siaxErrorResponseForLegacy } from "@/app/api/siax-error-mapping";
describe("0pt#53 legacy-route SiaxError mapping (app/api/siax-error-mapping.ts)", () => {
it("isSiaxError: true for a real SiaxError, false for plain errors", () => {
expect(isSiaxError(new SiaxError("DEPENDENCY_UNAVAILABLE", "0pt.orders", "x"))).toBe(true);
expect(isSiaxError(new Error("plain"))).toBe(false);
expect(isSiaxError(null)).toBe(false);
expect(isSiaxError("nope")).toBe(false);
});
it("DEPENDENCY_UNAVAILABLE maps to 503 with the structured, non-internal body", () => {
const err = new SiaxError(
"DEPENDENCY_UNAVAILABLE",
"0pt.orders",
'no database configured for persistent store "0pt.orders" in a production runtime',
);
expect(err.httpStatus).toBe(503);
expect(err.retryable).toBe(true);
const res = siaxErrorResponseForLegacy(err) as NextResponse;
expect(res.status).toBe(503);
void res.json().then((body: Record<string, unknown>) => {
expect(body.error).toBe("DEPENDENCY_UNAVAILABLE");
expect(body.source).toBe("0pt.orders");
expect(body.retryable).toBe(true);
});
});
it("the store name travels in the body so the operator knows WHICH store failed", () => {
const res = siaxErrorResponseForLegacy(
new SiaxError("DEPENDENCY_UNAVAILABLE", "0pt.subscriptions", "no database"),
);
void res.json().then((body: Record<string, unknown>) => {
expect(body.source).toBe("0pt.subscriptions");
});
});
});