error extension for graceful exception handling
npm install @aqo/exceptionjs
function Exception(messageOrError, assign = {})
`
messageOrError: can be String or Error or Exception
assign: optional, if provided must be Object
- if messageOrError is String, sets the message, and stack is set from point code was called
- if messageOrError is Error or Exception, sets the message and stack from that Error/Exception
- if assign is provided, all of its keys are assigned to the Exception being created
bonus "hidden" global configuration options that affect all Exceptions:
`js
Exception.toRemoveNodeInternals = true;
`
If you set this to true, all Exceptions will remove Node.js internals from stack traces.
This makes stacks a lot more compact and easier to read, at the price of potentially missing
an actual error in Node.js's implementation if one happens.
`js
Exception.trimPath = require('path').resolve(__dirname, '..');
`
If you set trimPath to a non-empty string, this path will be stripped from all paths
in both the trace and the error message, making it more compact and easier to read,
at the price of potentially missing relevant information.
Examples
`js
const Exception = require('@aqo/exception');
// throw an error (this is native JavaScript, no external modules)
try {
throw Error('message');
} catch(error) {
console.log(
'\nerror instanceof Error:', error instanceof Error, // true
'\nerror instanceof Exception:', error instanceof Exception, // false
);
}
// throw an exception (this is using this package)
try {
throw Exception('message');
} catch(error) {
console.log(
'\nerror instanceof Error:', error instanceof Error, // true
'\nerror instanceof Exception:', error instanceof Exception, // true
);
}
// throw an exception with extra properties
try {
throw Exception('human friendly message as long as needed for debugging', { // message is unreliable for testing, i.e. can be dynamically generated, etc
code: 'ERR_MACHINE_FRIENDLY_VALUE', // reliable for testing, should be final once decided on
data: 'blob of information that can be useful for debugging', // actual data depends on code
});
} catch(error) {
switch(error.code) {
case 'ERR_MACHINE_FRIENDLY_VALUE':
console.error('known exception type happened:', error.message, error.data);
break;
default:
console.error('unknown error happened:', error);
}
}
// convert errors into exceptions
try {
JSON.parse('not-json-data');
} catch(error) {
console.error(Exception(error, { // keeps error's original message, stack, and other keys
code: 'ERR_JSON_PARSE', // if error already had a .code key, this overwrites it
}));
}
// also works with existing exceptions
console.log(Exception(Exception('multi-level error handling', {
foo: 'inner level',
bar: 'inner level',
}), {
foo: 'outer level',
})); // should have foo: 'outer level', bar: 'inner level'
// you can also use new to create Exceptions, but it's recommended to avoid it
const myException = new Exception('foo');
// same result as const myException = Exception('foo');
``