unified-errors
v1.0.1
Published
A library to handle declarative error handling in Node.js
Maintainers
Readme
unified-errors
A TypeScript library for declarative error handling in Node.js applications.
Installation
npm install --save-dev unified-errorsQuick Start
1. Create Error Declarations
Create a .errors.js file in your project root:
module.exports = {
ApplicationError: {
code: "application",
children: {
ValidationError: {
code: "validation",
children: {
InvalidEmail: {
code: "invalid_email",
}
}
}
}
}
};2. Generate Error Classes
npx unified-errors generate3. Use Generated Errors
import { InvalidEmail } from './errors';
// Use the generated error classes
throw new InvalidEmail('Invalid email format', {
userId: session.userId
});CLI Usage
# Generate with default options
npx unified-errors generate
# Generate with custom options
npx unified-errors generate --config .errors.js --output ./src/errorsAPI
Generated Files
errors.ts: Generated error classeserror-types.ts: TypeScript type definitionserror-helpers.ts: Helper functionsindex.ts: Main export file
Error Declaration Format
{
ErrorName: {
code: "error_code",
children: {
// Nested errors
},
}
}License
MIT
