@philiprehberger/http-error
v0.1.2
Published
Typed HTTP error classes with status codes, factory methods, and serialization
Downloads
310
Readme
@philiprehberger/http-error
Typed HTTP error classes with status codes, factory methods, and serialization.
Installation
npm install @philiprehberger/http-errorUsage
import { HttpError, isHttpError } from '@philiprehberger/http-error';
// Factory methods
throw HttpError.notFound('User not found');
throw HttpError.badRequest('Invalid email');
throw HttpError.internal();
// Constructor
throw new HttpError(409, 'Already exists', { code: 'DUPLICATE' });
// Type guard
try {
await fetchUser(id);
} catch (err) {
if (isHttpError(err, 404)) {
// handle not found
}
}
// Serialization
const err = HttpError.badRequest('Invalid input');
console.log(JSON.stringify(err));
// {"status":400,"message":"Invalid input","code":"ERR_HTTP_400"}API
new HttpError(status, message?, options?)
Creates an HTTP error instance.
| Param | Type | Description |
| ------------------ | --------- | ------------------------------------ |
| status | number | HTTP status code |
| message | string | Error message (defaults by status) |
| options.code | string | Machine-readable code |
| options.cause | unknown | Underlying cause |
Factory Methods
badRequest, unauthorized, forbidden, notFound, conflict, gone, unprocessable, tooMany, internal, notImplemented, badGateway, unavailable
isHttpError(err): err is HttpError
isHttpError(err, status): err is HttpError
Type guard that checks if a value is an HttpError, optionally matching a specific status code.
toJSON(): { status, message, code }
Serializes the error for API responses.
Development
npm install
npm run build
npm testSupport
If you find this project useful:
