ErrorJS
Extendable javascript errors
About
After extending javascript Error
class in every single project and copying
allways different bad snippets from stackoverflow, i wanted to have a good
error base that i can reuse in different javascript/typescript projects.
This library tries to provide good simple base for javascript errors.
Install
npm install --save errorjs
Features
Sane predefined errors
ErrorJS has a list of predefined error defined, that you can reuse while devloping a service.
; throw 'some_feature_not_implemented';throw 'something_internally';throw 'something_was_not_found';throw 'access_was_unauthroized';throw 'error_with_connection_to_server';throw 'error_validating_resource';throw 'some_conflict';throw 'some_logic_error';
Enforced error codes
Every error has to have error code provided, which should uniquely identify an error
; throw ; // <-- invalid since no error code is providedthrow 'some_error_code'; // <-- good, since error code is provided
Support for error context
Additional context can be passed when creating an error
Simple error context
; throw 'some_error_code' some: 'context';
Predefined error context
const ConflictWithContext = ConflictError; throw 'some_error_code' some: 'othercontext';
Error factory with predefined error context
; const contextErrors = errors; const error = 'user_exists' errorId: '<some_error_id>'; errorcontext // {userId: '<some_user_id>', errorId: '<some_error_id>'}
Support for base Errors
Base errors can be passed to an error to encapsulate child error
; const baseError = 'some base error';const baseError2 = 'some other error'; throw 'some_error_code' baseError baseError2;
Variable arguments
Error constructor accepts variable number of arguments. This enable to pass multiple context objects to error.
; const baseError = 'some error has happend'; throw 'some_error_code' baseError some: 'context' some: 'other_context';
TypeScript/flowtype support
ErrorJS is tightly integrated with typescript and provides type information.
; errorsNotImplemented // <--- autocomplete here /* autocomplete here */ ;
Extendable error factory and base error
Error factory can be extended with new errors classes and base error can be redefined.
; BaseError = BaseError isVeryBadError = true; { return thiscontextuserID; } ; MyError = this; const factory = ;const error = factory; errorisVeryBadError // <-- extended functionality here throw 'some_error_code';
Examples
TypeScript
; BaseError = BaseError scope = 'global'; { return thiscontextuserId; } ; TransactionError = this; const errorFactory = ; const userErrors = factory; throw 'transaction_not_found' 'transaction was not found' id: '<transaction_id>';
JavaScript
const ExtendedErrorFactory = ; { super...args; thisBaseError = BaseError scope = 'global'; { return thiscontextuserId; } ; thisTransactionError = this; } const errorFactory = ; const userErrors = factory; throw 'transaction_not_found' 'transaction was not found' id: '<transaction_id>';
Development
npm t
: Run test suitenpm start
: Runsnpm run build
in watch modenpm run test:watch
: Run test suite in interactive watch modenpm run test:prod
: Run linting and generate coveragenpm run build
: Generage bundles and typings, create docsnpm run lint
: Lints codenpm run commit
: Commit using conventional commit style (husky will tell you to use it if you haven't 😉)
Credits
Made with ❤️ by @offlinehacker