universal-api-errors

Axios adapter

parseAxiosError() — turn any AxiosError into a UniversalError.

Install

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

axios itself is an optional peer dependency — install it if you don't already have it. The core package comes along automatically as a regular dependency.

Usage

import axios from "axios";
import { parseAxiosError } from "@harshilrajput/universal-api-errors-axios";

try {
  await axios.get("/api/user");
} catch (err) {
  const error = parseAxiosError(err);

  if (error.isUnauthorized) return redirectToLogin();
  if (error.isValidation) return showFieldErrors(error.validation);
  if (error.shouldRetry()) return retry();

  showToast(error.message);
}

Why a dedicated adapter

The core package's generic parseError() already recognizes Axios's { response: { status, data } } error shape via duck-typing, so this adapter is intentionally thin. It only adds what the generic parser can't infer on its own:

  • Telling "the server responded with an error status" (error.response present) apart from "the request was sent but nothing came back" (error.request present, error.response absent — a network failure, timeout, or DNS issue) apart from "the request never left the browser" (neither present — a setup error).
  • Tagging source: "axios" unconditionally, rather than the best-effort isAxiosError sniffing the generic parser falls back to.

It works even without axios installed at runtime — the error shape it checks for is a structural type, not an import from axios — so this package has no hard dependency on the client it adapts.

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 { parseAxiosError, parseError, retry, UniversalError } from "@harshilrajput/universal-api-errors-axios";

On this page