error-extender-1.0.2
Simplifies creation of custom Error
classes for Node.js!
...which then produces stack
with appended stacks of supplied cause
(very much like in Java)!
const extendError = ; const CustomError = ; const rootCause = 'the root cause'; console;
Shall output:
CustomError: An error has occurred.
at Object.<anonymous> (/opt/app/index.js:7:13)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:240:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:564:3)
Caused by: Error: the root cause
at Object.<anonymous> (/opt/app/index.js:5:19)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:240:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:564:3)
100% Code Coverage
Oh, by the way, 100% test coverage. See for yourself (via npm test
)!
Features
"Extending" Errors
It's quite simple! See below:
const extendError = ; const AppError = ; // extends `Error` (default)
Or... A bit more complex using the second argument (options):
const extendError = ; const AppError = ; const ServiceError = ; const DatabaseError = ; ;// no error
Yes, defaultData
merges!
error-extender
Arguments
error-extender
accepts a single object literal as second argument.
The options (object literal keys) are as follows:
key | expected type |
---|---|
parent |
Error.prototype or one that extends it |
defaultMessage |
string |
defaultData |
any |
"Extended Errors"
- Creates prototype-based
Error
classes (child/subclass) : "Extended Errors". - Those "Extended Errors", accepts
cause
(Error
); very much like how it is with JavaException
. - Appends stack of
cause
to the bottom of instantiated "Extended Errors" stack. - "Extended Errors" constructor & argument (w/ optional
new
):new ExtendedError(options)
ExtendedError(options)
Yes, much like JavaScript's native Error
, "Extended Errors" can be written/used "factory-like" (without the new
keyword).
"Extended Errors" Arguments (constructor)
"Extended Errors" accepts a single object literal as argument:
const extendError = ;const ServiceError = ;try // ... something throws something catch error throw message: 'An error has occurred' data: ref: '7e9f876ca116' cause: error ;
The options (object literal keys) are as follows:
key | alias | expected type |
---|---|---|
message |
m |
string |
data |
d |
any |
cause |
c |
instancedof Error |
Given the alias, you may construct extended errors by:
const extendError = ;const ServiceError = ;try // ... something throws something catch error throw m: 'An error has occurred' d: ref: '7e9f876ca116' c: error ;
Note: Aliases are evaluated first; hence if you have both m
and message
, if m
's value is truthy, then m
's value will be used.
Instance Properties
As with Error
, "Extended Errors" would have the following properties:
name
message
stack
... "Extended Errors" shall have the following additiona properties:
data
- (as set in constructor args)cause
- (as set in constructor args)
data
merging w/ defaultData
Yes, you heard right, instance data
merges with defaultData
!!!
See example below:
const extendError = ; const AppError = ; const appError = d: status: 401 ; ;// no error
bluebird
!):
The inspiration (thanks const Promise = ;// ...const extendError = ;// ...const ServiceError = ;const ServiceStateError = ;// ... { return { // ... multiple things that may throw your // custom "expected" errors } ;}
With JavaScript, I felt quite stifled when I was limited to:
- Do selective/custom handling based on matching messages from
throw new Error('..')
. - Return/propagate JSend-like responses to function "callers"/"users".
- ... or whatever error possible passing/handling could be done, throughout functions and callers/users.
With error-extender
with help from syntactic-sugar from bluebird
, you can improve (or even standardize) your way of propagating/handling errors throughout your application.
callers.
License
MIT License
Copyright (c) 2018 Joseph Baking
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.