@philiprehberger/ts-api-error-kit
v0.1.5
Published
Standardized typed HTTP error classes for APIs
Readme
@philiprehberger/ts-api-error-kit
Standardized typed HTTP error classes for APIs
Installation
npm install @philiprehberger/ts-api-error-kitUsage
Throwing Errors
import { ApiError, NotFoundError, BadRequestError } from '@philiprehberger/ts-api-error-kit';
// Generic error with status code
throw new ApiError(404, 'User not found', { code: 'USER_NOT_FOUND' });
// Convenience subclasses
throw new NotFoundError('User not found');
throw new BadRequestError('Invalid email', { code: 'INVALID_EMAIL', details: { field: 'email' } });Serializing Errors
import { toErrorResponse } from '@philiprehberger/ts-api-error-kit';
app.use((err, req, res, next) => {
const response = toErrorResponse(err);
res.status(response.status).json(response);
});
// ApiError → { status: 404, message: 'User not found', code: 'USER_NOT_FOUND' }
// Unknown → { status: 500, message: 'Internal server error' }Type Guard
import { isApiError } from '@philiprehberger/ts-api-error-kit';
if (isApiError(err)) {
console.log(err.statusCode); // typed
}Error Classes
| Class | Status Code |
|-------|------------|
| ApiError | Any (first argument) |
| BadRequestError | 400 |
| UnauthorizedError | 401 |
| ForbiddenError | 403 |
| NotFoundError | 404 |
| ConflictError | 409 |
| UnprocessableError | 422 |
| InternalError | 500 |
API
| Export | Description |
|--------|-------------|
| ApiError | Base error class with statusCode, code, details |
| isApiError(err) | Type guard — returns true if err is an ApiError |
| toErrorResponse(err) | Serializes any error to ErrorResponse object |
Development
npm install
npm run build
npm testLicense
MIT
