@uniresp/errors
v0.1.7
Published
Typed error classes (AppError & subclasses).
Maintainers
Readme
@uniresp/errors
VN (tóm tắt): Tập hợp các error classes đã chuẩn hoá (
AppError& các lớp con) để ném lỗi cócode,message,details, vàstatusnhất quán.
Install
npm i @uniresp/errors
# or
pnpm add @uniresp/errorsWhat you get
AppError: base class →new AppError(code, message, details?, status = 400)UnauthorizedError→AUTH.UNAUTHORIZED, HTTP 401NotFoundError→RESOURCE.NOT_FOUND, HTTP 404ValidationError→INPUT.VALIDATION, HTTP 422SystemError→SYS.UNKNOWN, HTTP 500
All of these work seamlessly with
@uniresp/server-express’serrorHandlerto emit the standardizedApiErrorJSON.
Usage
import { ValidationError, NotFoundError, AppError } from '@uniresp/errors';
// throw from services/handlers
function createUser(input: any) {
if (!input?.email)
throw new ValidationError({ field: 'email' }, 'Email is required');
// ...
}
async function getUser(id: string) {
const user = await db.user.findById(id);
if (!user) throw new NotFoundError('User not found');
return user;
}
// customizing
throw new AppError(
'PAYMENT.REJECTED',
'Card declined',
{ reason: 'insufficient_funds' },
402
);Error shape produced by adapters
When used with @uniresp/server-express, thrown AppError (or subclasses) are converted to:
{
"ok": false,
"error": {
"code": "INPUT.VALIDATION",
"message": "Invalid input",
"details": { "...": "..." },
"traceId": "req-123"
}
}Notes
- Prefer stable
codes (e.g.USER.NOT_FOUND) over free-text parsing. detailsis optional and can carry validation errors, raw provider payloads, etc.- You can extend
AppErrorto add domain-specific errors while keeping consistency.
License
MIT © 2025-present
