Creates a middleware admitting only 2xx responses, rejecting the others as problems.
Decorates a Fetch implementation with one rejecting every unsuccessful response as a structured exception,
so that callers await a usable response rather than inspecting response.ok at each call site:
Response is ok (2xx status): the promise resolves with the response unchanged
Response is not ok (non-2xx status): the promise rejects with a Problem carrying the response
status and its statusText as detail, plus what the body states when the Content-Type allows it to be read:
application/problem+json stating a Problem: the problem itself, its members taking precedence over the
status and detail of the response, so that a problem resolve or the origin server
described is raised as described
text/plain: the body as text, carried as report
other JSON-based media types, for example application/json or application/ld+json, or a problem body stating
no Problem: the parsed JSON payload, carried as report
other media types, or a body that fails to parse: no report
The wrapped implementation rejects, whatever with: the rejection is relayed unchanged, so that a failure the
platform or a middleware raised, a bug or a caller abort included, is never mistaken for a problem
Problems raised by this middleware are immutable, so a handler may pass one on without guarding against
later mutation.
Tip
With resolve declared below this middleware, transport failures are rejected as problems
as well. They reach this middleware as responses, which it rejects like any other unsuccessful one.
Note
The middleware is idempotent, so applying it more than once to the same chain is harmless.
Creates a middleware admitting only 2xx responses, rejecting the others as problems.
Decorates a Fetch implementation with one rejecting every unsuccessful response as a structured exception, so that callers await a usable response rather than inspecting
response.okat each call site:ok(2xxstatus): the promise resolves with the response unchangedok(non-2xxstatus): the promise rejects with a Problem carrying the responsestatusand itsstatusTextasdetail, plus what the body states when theContent-Typeallows it to be read:application/problem+jsonstating a Problem: the problem itself, its members taking precedence over thestatusanddetailof the response, so that a problem resolve or the origin server described is raised as describedtext/plain: the body as text, carried asreportapplication/jsonorapplication/ld+json, or a problem body stating no Problem: the parsed JSON payload, carried asreportreportProblems raised by this middleware are immutable, so a handler may pass one on without guarding against later mutation.
With resolve declared below this middleware, transport failures are rejected as problems as well. They reach this middleware as responses, which it rejects like any other unsuccessful one.
The middleware is idempotent, so applying it more than once to the same chain is harmless.