@harshilrajput/universal-api-errors-axios
v0.1.3
Published
Axios adapter for universal-api-errors: turns any AxiosError into a UniversalError.
Maintainers
Readme
@harshilrajput/universal-api-errors-axios
Axios adapter for universal-api-errors.
Turns any AxiosError — or anything Axios-shaped — into a UniversalError.
Install
npm install @harshilrajput/universal-api-errors-axios
# or: pnpm add @harshilrajput/universal-api-errors-axios
# or: yarn add @harshilrajput/universal-api-errors-axiosaxios itself is an optional peer dependency you almost certainly already have. @harshilrajput/universal-api-errors-core
comes along automatically as a regular dependency — no separate install needed.
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);
}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" apart from "the request was sent but nothing came back" (network failure,
timeout, DNS), and tagging source: "axios" unconditionally rather than guessing from an
isAxiosError flag.
It works even without axios installed at runtime — AxiosErrorLike 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
@harshilrajput/universal-api-errors-core
public API, so you don't need a separate import (or a separate npm install) to reach the generic
surface — parseError, createUniversalError, the UniversalError class, retry, normalizeValidation,
configureLogger, and every type, all from this one package:
import { parseAxiosError, parseError, retry, UniversalError } from '@harshilrajput/universal-api-errors-axios';License
MIT
