@wisemen/api-error
v0.0.10
Published
Shared API error classes.
Maintainers
Keywords
Readme
@repo/api-errors
Shared API error classes.
Overview
This package provides a standardized error handling system for API applications. It includes:
- Base error classes (
ApiError,CompositeApiError) - Specific error types (NotFound, BadRequest, Forbidden, etc.)
- JSON API error formatting
- Error decorators for metadata, codes, and status
- Swagger/OpenAPI documentation support
Usage
import { NotFoundApiError, ApiErrorCode } from '@repo/api-errors'
export class UserNotFoundError extends NotFoundApiError {
@ApiErrorCode('user_not_found')
readonly code = 'user_not_found'
meta: never
constructor (userId: string) {
super(`User with id ${userId} not found`)
}
}Features
Base Classes
ApiError- Base class for all API errors with automatic JSON API conversionCompositeApiError- For grouping multiple errors together
Error Types
BadRequestApiError- 400 errorsUnauthorizedError- 401 errorsForbiddenApiError- 403 errorsNotFoundApiError- 404 errorsConflictApiError- 409 errorsInternalServerApiError- 500 errorsServiceUnavailableApiError- 503 errors
JSON API Format
All errors automatically convert to JSON API format via the toJsonApiError() method:
{
status: 404,
errors: [{
code: "user_not_found",
detail: "User with id 123 not found",
status: "404"
}]
}Decorators
@ApiErrorCode(code: string)- Set the error code@ApiErrorStatus(status: HttpStatus)- Set the HTTP status@ApiErrorMeta()- Define metadata structure@ApiNotFoundErrorResponse()- Swagger documentation helpers
Dependencies
@nestjs/common- NestJS common utilities@nestjs/swagger- OpenAPI/Swagger decorators@sentry/nestjs- Error tracking integrationclass-transformer- Type transformation
