Shared API error classes.
npm install @wisemen/api-errorShared API error classes.
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
``typescript
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)`
}
}
- ApiError - Base class for all API errors with automatic JSON API conversionCompositeApiError
- - For grouping multiple errors together
- BadRequestApiError - 400 errorsUnauthorizedError
- - 401 errorsForbiddenApiError
- - 403 errorsNotFoundApiError
- - 404 errorsConflictApiError
- - 409 errorsInternalServerApiError
- - 500 errorsServiceUnavailableApiError
- - 503 errors
All errors automatically convert to JSON API format via the toJsonApiError() method:
`typescript`
{
status: 404,
errors: [{
code: "user_not_found",
detail: "User with id 123 not found",
status: "404"
}]
}
- @ApiErrorCode(code: string) - Set the error code@ApiErrorStatus(status: HttpStatus)
- - Set the HTTP status@ApiErrorMeta()
- - Define metadata structure@ApiNotFoundErrorResponse()
- - Swagger documentation helpers
- @nestjs/common - NestJS common utilities@nestjs/swagger
- - OpenAPI/Swagger decorators@sentry/nestjs
- - Error tracking integrationclass-transformer` - Type transformation
-