@grafctory/http - v0.9.0
    Preparing search index...

    Function resolve

    • Creates a middleware answering failed exchanges with responses.

      Decorates a Fetch implementation with one that never rejects but for a caller abort, answering a request that yields no response as an intermediary would on the origin's behalf:

      • Exchange succeeds with a response, whatever its status: the response is relayed unchanged
      • Request states a malformed URL (only a raw string may): answered without being sent, with a 400 response stating the http_request_error proxy error
      • Exchange fails with an Error, as when the resource cannot be reached: answered with a response stating the proxy error type the failure is classified under, as tabled below
      • Exchange fails with a Problem stating the status of an origin response, as reject raises for an unsuccessful one: answered with a response restating it under that status, with no proxy error
      • Exchange fails with any other value, including a problem stating no origin status: answered as an unavailable destination, stating the detail or the title of a problem, or else the value itself, as the reason
      • Exchange is aborted by the caller: relayed as the rejection the caller asked for, as the platform fetch does

      Failures are answered whether the layers below reject or throw outright.

      A synthetic response states the target URL as its url, on clones too, and the reason the exchange failed as its statusText. Its application/problem+json body describes the failure, with an instance naming the target URL. A response answering a transport failure also states a Proxy-Status field, naming the client, the RFC 9209 error type and the reason. It takes the status RFC 9209 recommends for that error type:

      Failure Error type Status
      Malformed URL http_request_error 400
      DNS name not found dns_error 502
      DNS timeout dns_timeout 504
      Connection refused connection_refused 502
      Connection timeout connection_timeout 504
      Connection reset connection_terminated 502
      TLS certificate failure tls_certificate_error 502
      Other TLS failure tls_protocol_error 502
      Any other failure destination_unavailable 502
      Note

      Failures are told apart by the code the platform states on the cause of the error, as Node.js and Deno do. Browsers state none, deliberately hiding why an exchange failed, so every failure there is answered as an unavailable destination. The reason phrase still states the message the browser raised.

      The middleware is best declared late in a chain, right above the transport, so that the layers above it see every failure as a response. throttle then retries the transient ones, such as timeouts and connection resets, and relays permanent ones, such as certificate failures, as they stand. Likewise, monitor logs each failure with the reason stated by the response answering it.

      Tip

      Restating a problem rebuilds a response from what the problem retains, so header fields the origin stated, such as Retry-After, are lost. reject is thus best declared above this middleware rather than below it, so that it raises problems for the responses this middleware answers rather than the converse.

      Returns Middleware

      A Middleware wrapping a Fetch implementation with one answering every failed exchange with a synthetic response rather than a rejection, but for a caller abort