Page MenuHomePhabricator

REST API vNext: [SPIKE] Error format versioning — explicit declaration vs. schema-derived
Closed, ResolvedPublic5 Estimated Story PointsSpike

Description

Goals:

  • Evaluate whether vNext modules should declare their error response format explicitly in the module definition file, or derive it implicitly from the module's schema version (e.g. 1.1 for mwapi-1.1.json).
  • Produce a short written recommendation covering trade-offs: discoverability, consistency across modules, long-term maintenance cost.
  • Include a proposed default behavior plus migration guidance for modules that predate the decision.
  • Circulate for review with Daniel Kinzler before implementation begins.

Context: this is the main open decision currently blocking the error handling workstream (see Router.php / specs.v0.json experiment)

Details

Other Assignee
KineticPelagic

Event Timeline

HCoplin-WMF renamed this task from REST API vNext: Error format versioning — explicit declaration vs. schema-derived to REST API vNext: [SPIKE] Error format versioning — explicit declaration vs. schema-derived.Jul 22 2026, 2:10 PM
HCoplin-WMF added a project: Spike.
Restricted Application changed the subtype of this task from "Task" to "Spike". · View Herald TranscriptJul 22 2026, 2:10 PM
AGhirelli-WMF set the point value for this task to 5.Jul 23 2026, 4:28 PM
AGhirelli-WMF changed the task status from Open to In Progress.Jul 29 2026, 3:51 PM
AGhirelli-WMF updated Other Assignee, added: KineticPelagic.

Spike report

Modules declare their error response format explicitly, via a new responseSchemaVer field in the module definition file (sibling of mwapi). Modules that omit the field are treated as legacy — no module has to be touched for this to be safe to ship.

json
{
  "mwapi": "1.1.0",
  "moduleId": "specs/v0",
  "responseSchemaVer": "0.1",
  ...
}

This has been prototyped end-to-end (code + manual verification, see below) under T432868, so this recommendation isn't purely theoretical — it reflects a mechanism that already runs correctly against two real core modules.


Why explicit over schema-derived

The alternative considered was deriving the error format implicitly from the module's mwapi version (e.g. "mwapi >= 2.0 implies the new error shape").

Explicit (responseSchemaVer)Derived from mwapi
DiscoverabilityThe format is visible directly in the module spec — no need to cross-reference a version-to-shape mapping maintained elsewhere.Requires knowing (or looking up) which mwapi ranges imply which error shape; not visible at the point of declaration.
ConsistencyA module's error format and its request/response schema version can evolve independently — a module can adopt the new error shape without bumping unrelated mwapi fields.Couples two concerns that don't necessarily change together: mwapi versions the *whole* module contract, error shape is one narrow part of it.
Maintenance costOne new field, one lookup table (responseSchemaVer → formatter class) in the router. Adding a format is additive.Requires a version_compare()-style threshold that has to be kept in sync with mwapi version history; gets harder to reason about as more thresholds accumulate.

Explicit declaration was preferred on all three axes considered by this task.


Default behavior (migration guidance)

No existing module declares responseSchemaVer today — it does not exist anywhere in the current module files. The router treats absence of the field as the migration signal:

  • responseSchemaVer absent → legacy error shape (httpCode/httpReason/messageTranslations/...), the shape in production today. No action required from existing module authors.
  • responseSchemaVer present, recognized → routed to the formatter registered for that version.
  • responseSchemaVer present, not recognized → fails fast with a ModuleConfigurationException at module load time, rather than silently falling back. A typo or a reference to a not-yet-implemented format is caught immediately instead of producing a wrong response shape at runtime.

Mechanism (brief)

  • ErrorFormatter — concrete base class, holds the logic shared by every shape (translating messages via the module's text formatters, gathering data from exceptions). Not itself a "shape."
  • LegacyErrorFormatter extends ErrorFormatter — today's production shape; redefines nothing, inherits ErrorFormatter::formatErrorBody() as-is.
  • A future formatter (e.g. for the shape T432797 will finalize) is a new subclass overriding only formatErrorBody( int $statusCode, array $bodyData ): array — the single method that decides which keys go into the response body. Everything else (exception handling, message translation) is shared and untouched.
  • Router holds a small registry, responseSchemaVer → formatter class, and picks the right ResponseFactory/formatter pair per module when it's instantiated:
private const ERROR_FORMATTERS = [
    '0.1' => HybridErrorFormatter::class,
    // 'X.Y' => SomeOtherErrorFormatter::class,
];

Adding a new format is: write the formatter class (override one method), add one line to this map. No changes required to EntryPoint, ResponseFactory, or any existing formatter.

  • Extra per-error data (e.g. request URI) that a new shape might need reaches the formatter through the existing $extraData parameter already threaded from Module::execute() down to formatErrorBody() — no signature changes needed for that class of addition.

Verification performed

Manually verified against a running local wiki, using two core modules that exercise both branches of the mechanism:

  • specs/v0 (declares responseSchemaVer: "0.1") → routed to the mapped formatter. Confirmed by temporarily adding marker fields to that formatter's formatErrorBody() override and observing them in the live HTTP error response (GET /w/rest.php/specs/v0/module/does-not-exist → 403, marker fields present in the JSON body). Reverted after verification.
  • site/v1 (no responseSchemaVer) → routed to LegacyErrorFormatter. Confirmed the equivalent request (GET /w/rest.php/site/v1/this-path-does-not-exist → 404) returns the unmodified legacy shape, with no marker fields — proving the two branches are correctly isolated.

Next steps

  • Once T432797 finalizes the new shape, implement it as a new ErrorFormatter subclass per the mechanism above (T432798).