@nitrotool/errors
v1.0.5
Published
Reusable error utilities for H3-based applications. This package provides a collection of typed error helpers that create `H3Error` instances with standardized HTTP status codes and messages.
Readme
@nitrotool/errors
Reusable error utilities for H3-based applications.
This package provides a collection of typed error helpers that create H3Error instances with standardized HTTP status
codes and messages.
Installation
pnpm install @nitrotool/errorsnpm install @nitrotool/errorsUsage
import {
UnauthenticatedError,
UnauthorizedError,
NotFoundError,
EntityAlreadyExistsError,
ServerError,
ClientError,
UnprocessableEntityError,
BadRequestError,
} from '@nitrotool/errors';
// Example in an API handler
export default defineEventHandler((event) => {
const authHeader = getHeader(event, 'authorization');
if (!authHeader) {
throw UnauthenticatedError('Missing auth token');
}
if (!isUserAllowed(authHeader)) {
throw UnauthorizedError('You do not have access to this resource');
}
// ...
});API
| Function | Status Code | Status Message |
|------------------------------|-------------|---------------------------|
| UnauthenticatedError | 401 | Unauthenticated |
| UnauthorizedError | 403 | Unauthorized |
| NotFoundError | 404 | Not found |
| EntityAlreadyExistsError | 409 | Entity Already Exists |
| ClientError | 400 | Client Error |
| BadRequestError | 400 | Bad Request |
| UnprocessableEntityError | 422 | Unprocessable Entity |
| ServerError | 500 | Internal Server Error |
Development
Build the package:
pnpm buildThe package uses unbuild for generating dist/ with both index.mjs and type
definitions.
License
MIT
