Warning
⚠️ MOVED TO https://github.com/belgattitude/httpx⚠️ No breaking changes, just update from
@belgattitude/http-exception
to@httpx/exception
.
Delightful http exceptions. Crafted with node, browsers, ssr frameworks (serialization) and error logging in mind.
- 🚀 Dead simple: explicit named imports and/or status code.
- 📡 Works everywhere: node, browsers, edge... framework agnostic, no deps.
- 🎥 Logger friendly with contextual info. Less guessing games.
- 🐎 Serializable to cover Server-Side-Rendering use-cases (nextjs, superjson,...).
- 🎯 Up to standards. extends Error class with stack and Error.cause support.
- 🍃 Lightweight - treeshakable - wide browser coverage (trade-off).
- ✨ Default statusText as error message. Less chars, divergence...
- 🧙 IDE friendly. Typescript - typedoc with links to mdn and description.
- 🥃 Docs & changelogs - Well tested and maintained - Contributors welcome.
👉 See full documentation on https://belgattitude.github.io/http-exception. 👈
npm install @belgattitude/http-exception # via npm
yarn add @belgattitude/http-exception # via yarn
Simple named exceptions:
import {
HttpGatewayTimeout,
HttpInternalServerError,
HttpNotFound,
HttpServiceUnavailable,
} from "@belgattitude/http-exception";
throw new HttpNotFound(); // message = 'Not found', statusCode = 404
// Custom message
throw new HttpServiceUnavailable("Service temporarily unavailable");
// Custom context
throw new HttpInternalServerError({
message: "Oups, this is on our side.",
url: "https://api.dev/gateway",
code: "EXTERNAL_SERVICE_TIMEOUT",
cause: new HttpGatewayTimeout({
code: "This Serverless Function has timed out",
errorId: "cdg1::h99k2-1664884491087-b41a2832f559",
}),
});
By status code
import { createHttpException } from "@belgattitude/http-exception";
const e404 = createHttpException(404);
const e500 = createHttpException(500, { message: "Server error" });
Serialization
import { fromJson, toJson } from "@belgattitude/http-exception/serializer";
const e = new HttpForbidden();
const json = toJson(e);
const deserialized = fromJson(json);
More in the docs: https://belgattitude.github.io/http-exception
Don't hesitate and open an issue.
Contributors are warmly welcomed. Have a look to the CONTRIBUTING document.
If you are enjoying some of my OSS guides or libs for your company, I'd really appreciate a sponsorship, a coffee or a dropped star. That gives me a tasty morning boost and help me to make some of my ideas come true 🙏
MIT