Collection for handle express error response
npm install @qnx/errors@qnx/errors@qnx/errors provides a set of custom error classes to help you simplify and standardize error handling in your JavaScript/TypeScript applications.
- Predefined error classes for common scenarios
- Consistent structure for error responses
- Easy integration with APIs and validation logic
You can install via your preferred package manager:
``bashnpm
npm install @qnx/errors
π Usage
$3
`javascript
import {
ApiError,
ValidationError,
InvalidValueError,
UnauthenticatedUserError,
ServerError
} from '@qnx/errors'
`π Error Class Examples
$3
`ts
const apiError = new ApiError('Something went wrong', 500, {
errRes: {
message: 'Server failed to respond',
errors: {
generic: ['Unexpected error occurred']
}
}
})console.log(apiError.message) // "Something went wrong"
console.log(apiError.getCode()) // 500
console.log(apiError.getErrorResponse()) // { message: "...", errors: ... }
`$3
`ts
const validationError = new ValidationError('Validation failed', {
errRes: {
message: 'Validation errors found',
errors: {
email: ['Email is invalid'],
password: ['Password must be at least 8 characters']
}
}
})console.log(validationError.getCode()) // errorCodes.VALIDATION_ERROR_CODE
console.log(validationError.getErrorResponse()) // Detailed error response
`$3
`ts
const error = new InvalidValueError('Username cannot contain spaces', {
key: 'username'
})console.log(error.getCode()) // errorCodes.VALIDATION_ERROR_CODE
console.log(error.getErrorResponse()) // { errors: { username: ['Username cannot contain spaces'] } }
`$3
`ts
const authError = new UnauthenticatedUserError('User not authenticated')console.log(authError.getCode()) // errorCodes.UNAUTHENTICATED_USER_ERROR_CODE
console.log(authError.message) // "User not authenticated"
`$3
`ts
const serverError = new ServerError('Internal server error')console.log(serverError.getCode()) // errorCodes.SERVER_ERROR_CODE
console.log(serverError.message) // "Internal server error"
`π Summary Table
| Error Class | Purpose | Default Code | Use When... |
| -------------------------- | --------------------------- | ------------ | ------------------------------------------- |
|
ApiError | Generic base error | Custom | You need a customizable error |
| ValidationError | Multi-field form validation | 422 | Input or schema validation fails |
| InvalidValueError | Single field validation | 422 | One field like "email" or "username is bad |UnauthenticatedUserError | Auth failure | 401 | User not logged in or token missing |ServerError | Internal system failure | 500 | Something broke that user canβt fix |Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
MIT License Β© 2023-PRESENT Yatendra Kushwaha