qfetch
    Preparing search index...

    Function withResponseError

    • Middleware that throws errors for HTTP responses based on their status codes.

      By default, throws a ResponseError for any response with status code >= 400. Provides flexible error customization through status-specific mappers and a default fallback, allowing consumers to standardize error handling across their application.

      Parameters

      Returns MiddlewareExecutor

      Error mappers can be sync or async, allowing you to read the response body when creating custom errors.

      // Zero-config usage - throws ResponseError for status >= 400
      const qfetch = withResponseError()(fetch);

      // Custom error mapping
      const qfetch = withResponseError({
      statusMap: new Map([
      [404, (res) => new NotFoundError(res.url)],
      [401, async (res) => new AuthError(await res.json())]
      ]),
      throwOnStatusCode: (code) => code >= 400
      })(fetch);