Skip to content

Latest commit

 

History

History
110 lines (79 loc) · 6.1 KB

README.md

File metadata and controls

110 lines (79 loc) · 6.1 KB

@belgattitude/http-exception

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.

npm size node browserslist dist ci codecov techdebt maintainability license

Highlights

Documentation

👉 See full documentation on https://belgattitude.github.io/http-exception. 👈

Install

npm install @belgattitude/http-exception  # via npm
yarn add @belgattitude/http-exception     # via yarn

Quick start

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

Support

Don't hesitate and open an issue.

Contributors

Contributors are warmly welcomed. Have a look to the CONTRIBUTING document.

Sponsors ♥

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 🙏

License

MIT