https://github.com/mrcaidev/http-errors
Utility classes for HTTP errors.
https://github.com/mrcaidev/http-errors
error http typescript utility vite
Last synced: about 1 year ago
JSON representation
Utility classes for HTTP errors.
- Host: GitHub
- URL: https://github.com/mrcaidev/http-errors
- Owner: mrcaidev
- License: mit
- Created: 2023-08-09T16:09:25.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-05-30T04:44:39.000Z (almost 2 years ago)
- Last Synced: 2024-05-31T15:28:15.454Z (almost 2 years ago)
- Topics: error, http, typescript, utility, vite
- Language: TypeScript
- Homepage:
- Size: 356 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @mrcaidev/http-errors
Utility classes for HTTP errors.
## 📦 Installation
```sh
npm add @mrcaidev/http-errors # using npm
yarn add @mrcaidev/http-errors # using yarn
pnpm add @mrcaidev/http-errors # using pnpm
```
## 🚀 Usage
Simplest:
```ts
import { BadRequestError } from "@mrcaidev/http-errors";
throw new BadRequestError(); // status: 400, message: "Bad Request"
```
Custom error message:
```ts
import { BadRequestError } from "@mrcaidev/http-errors";
throw new BadRequestError("Malformed data"); // status: 400, message: "Malformed data"
```
Dynamic status code:
```ts
import { HttpError } from "@mrcaidev/http-errors";
throw new HttpError(400); // status: 400, message: "Bad Request"
```
Dynamic status code and custom error message:
```ts
import { HttpError } from "@mrcaidev/http-errors";
throw new HttpError(400, "Malformed data"); // status: 400, message: "Malformed data"
```
Dynamic non-standard status code:
```ts
import { HttpError } from "@mrcaidev/http-errors";
throw new HttpError(499); // status: 499, message: "Unknown"
```
Dynamic non-standard status code and custom error message:
```ts
import { HttpError } from "@mrcaidev/http-errors";
throw new HttpError(499, "Custom error"); // status: 400, message: "Custom error"
```
Every error class extends `HttpError`, which further extends `Error`.
```ts
import { BadRequestError, HttpError } from "@mrcaidev/http-errors";
new BadRequestError() instanceof HttpError; // true
new HttpError(400) instanceof Error; // true
```
Hover over an error class in the editor to see its definition in RFC.

## 🔎 References
- [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110)
- [RFC 8470](https://www.rfc-editor.org/rfc/rfc8470)
- [RFC 7725](https://www.rfc-editor.org/rfc/rfc7725)
- [RFC 6585](https://www.rfc-editor.org/rfc/rfc6585)
- [RFC 5842](https://www.rfc-editor.org/rfc/rfc5842)
- [RFC 4918](https://www.rfc-editor.org/rfc/rfc4918)
- [RFC 2774](https://www.rfc-editor.org/rfc/rfc2774)
- [RFC 2295](https://www.rfc-editor.org/rfc/rfc2295)
## 📜 License
[MIT](LICENSE)