Handle HTTP errors like a pro with these ready-to-use exception classes and utilities. Perfect for building REST APIs that follow best practices!
import { NotFoundException, HttpException } from '@medishn/toolkit';
// Throw a 404 error
throw new NotFoundException({
detail: 'User 123 not found',
instance: '/users/123',
});
// Custom error with extra info
throw new HttpException(422, {
type: 'https://api.com/errors/invalid-email',
title: 'Invalid Email Format',
extensions: {
suggestedFormat: 'user@domain.com',
},
});All common HTTP status codes ready to use:
// 400 Bad Request
throw new BadRequestException();
// 401 Unauthorized
throw new UnauthorizedException();
// 404 Not Found
throw new NotFoundException({ detail: 'Post not found' });
// 500 Internal Error
throw new InternalServerErrorException();Standard error format out-of-the-box:
{
"type": "about:blank",
"title": "Not Found",
"status": 404,
"detail": "User 123 not found",
"instance": "/users/123",
"traceId": "abc123"
}Add extra context to errors:
throw new ForbiddenException({
type: 'https://api.com/errors/insufficient-credits',
title: 'Insufficient Credits',
extensions: {
currentBalance: 5,
requiredBalance: 10,
},
});All status codes with descriptions:
import { HTTP_ERRORS } from '@medishn/toolkit';
console.log(HTTP_ERRORS.NOT_FOUND);
// {
// status: 404,
// message: 'Not Found',
// description: 'Requested resource could not be found'
// }Pick your status code:
| Status | Class Name | Typical Use |
|---|---|---|
| 400 | BadRequestException |
Invalid user input |
| 401 | UnauthorizedException |
Missing/invalid auth |
| 403 | ForbiddenException |
No permission |
| 404 | NotFoundException |
Missing resource |
| 429 | TooManyRequestsException |
Rate limiting |
| 500 | InternalServerErrorException |
Server oopsie |
For custom status codes:
// Custom 418 I'm a teapot error
throw new HttpException(418, {
title: 'Teapot Mode Activated',
detail: 'Cannot brew coffee, I am a teapot',
});throw new ConflictException({
detail: 'Email already registered',
extensions: {
conflictingField: 'email',
existingUser: 'user@example.com',
},
});throw new BadRequestException({
type: 'https://api.com/errors/invalid-input',
title: 'Validation Failed',
});const error = new InternalServerErrorException();
const response = error.getProblemDetails('req-123');All exceptions return this standardized format:
{
"type": "error-category",
"title": "Short error summary",
"status": 404,
"detail": "Detailed explanation",
"instance": "/path/where-error-happened",
"traceId": "request-correlation-id",
"customField": "Additional info"
}Tip: Use the
getProblemDetails()method to format errors for API responses! 🚀