@belgattitude/http-exception
Version:
Warning: has been moved to @httpx/exception. Please update.
110 lines (78 loc) • 6.25 kB
Markdown
# @belgattitude/http-exception
> **Warning**
> ⚠️ This package have been 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 and error logging in mind.
[](https://www.npmjs.com/package/@belgattitude/http-exception)
[](https://bundlephobia.com/package/@belgattitude/http-exception@latest)
[](https://browserslist.dev/?q=PjAuMjUlLCBub3QgZGVhZA%3D%3D)
[](https://browserslist.dev/?q=PjAuMjUlLCBub3QgZGVhZA%3D%3D)
[](https://github.com/belgattitude/http-exception/blob/main/packages/http-exception/.size-limit.cjs)
[](https://github.com/belgattitude/http-exception/actions?query=branch%3Amain)
[](https://codecov.io/gh/belgattitude/http-exception)
[](https://codeclimate.com/github/belgattitude/http-exception)
[](https://codeclimate.com/github/belgattitude/http-exception)
[](https://github.com/belgattitude/http-exception/blob/main/LICENSE)
## Highlights
- 🚀 Dead simple: [explicit named imports](https://belgattitude.github.io/http-exception/#/?id=named-exceptions) and/or [status code](https://belgattitude.github.io/http-exception/#/?id=factories).
- 📡 Works everywhere: node, browsers, edge... framework agnostic, no deps.
- 🎥 Logger friendly with [contextual](https://belgattitude.github.io/http-exception/#/?id=about-context) info. Less guessing games.
- 🐎 [Serializable](https://belgattitude.github.io/http-exception/#/?id=serializer) to cover Server-Side-Rendering use-cases (nextjs, superjson,...).
- 🎯 Up to standards. [extends](https://belgattitude.github.io/http-exception/#/?id=uml-class-diagram) Error class with [stack](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack) and [Error.cause](https://belgattitude.github.io/http-exception/#/?id=about-errorcause) support.
- 🍃 [Lightweight](https://bundlephobia.com/package/@belgattitude/http-exception@latest) - [treeshakable](https://github.com/belgattitude/http-exception/blob/main/packages/http-exception/.size-limit.cjs) - wide [browser coverage](https://browserslist.dev/?q=PjAuMjUlLCBub3QgZGVhZA%3D%3D) (trade-off).
- ✨ Default statusText as [error message](https://belgattitude.github.io/http-exception/#/?id=about-default-message). Less chars, divergence...
- 🧙 IDE friendly. Typescript - typedoc with links to mdn and description.
- 🥃 [Docs](https://belgattitude.github.io/http-exception) & [changelogs](https://github.com/belgattitude/http-exception/releases) - Well tested and maintained - [Contributors](https://github.com/belgattitude/http-exception/blob/main/CONTRIBUTING.md) welcome.
## Documentation
**👉 See full documentation on [https://belgattitude.github.io/http-exception](https://belgattitude.github.io/http-exception). 👈**
## Install
```bash
npm install @belgattitude/http-exception # via npm
yarn add @belgattitude/http-exception # via yarn
```
## Quick start
Simple named exceptions:
```typescript
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
```typescript
import { createHttpException } from "@belgattitude/http-exception";
const e404 = createHttpException(404);
const e500 = createHttpException(500, { message: "Server error" });
```
Serialization
```typescript
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](https://belgattitude.github.io/http-exception)
## Support
Don't hesitate and open [an issue](https://github.com/belgattitude/http-exception/issues).
## Contributors
Contributors are warmly welcomed. Have a look to the [CONTRIBUTING](https://github.com/belgattitude/http-exception/blob/main/CONTRIBUTING.md) document.
## Sponsors ♥
If you are enjoying some of my OSS guides or libs for your company, I'd really appreciate a [sponsorship](https://github.com/sponsors/belgattitude), a [coffee](https://ko-fi.com/belgattitude) or a dropped star. That gives me a tasty morning boost and help me to make some of my ideas come true 🙏
## License
MIT