universal-api-errors

fetch adapter

parseFetchError() — turn a failed Response or network rejection into a UniversalError.

Install

npm install @harshilrajput/universal-api-errors-fetch

The core package comes along automatically as a regular dependency — no separate install needed.

Usage

fetch never throws for HTTP error statuses (404, 500, ...) — it only rejects for genuine network failures (offline, DNS, an aborted request). parseFetchError handles both cases, which is why, unlike every other parser in this ecosystem, it's async: reading a Response body requires an await.

import { parseFetchError } from "@harshilrajput/universal-api-errors-fetch";

// 1. HTTP error status
const response = await fetch("/api/user");
if (!response.ok) {
  const error = await parseFetchError(response);
  if (error.isUnauthorized) redirectToLogin();
}

// 2. Network failure / abort
try {
  await fetch("/api/user");
} catch (err) {
  const error = await parseFetchError(err);
  if (error.isOffline) showOfflineBanner();
}

Reading the response body uses response.clone(), so calling parseFetchError(response) never consumes the stream — you can still read response.json() yourself afterwards if you need the body for something else.

For the non-Response branch (network failures, AbortError), this delegates straight to the core package's parseError(), which already classifies those from the error's code/message — keeping that heuristic in one place instead of two copies drifting apart.

Everything from core, too

This package re-exports the entire core API, so parseError, createUniversalError, the UniversalError class, retry, normalizeValidation, and every type are all available from this one import:

import { parseFetchError, parseError, retry, UniversalError } from "@harshilrajput/universal-api-errors-fetch";

On this page