
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Using custom errors is tricky in JS. Unfortunately, it is not enough to just inherit your class from Error. You need to do really strange things to provide stack trace with your error objects.
nerr fixes this problem by providing ErrorBase class. By inheriting it you are getting correct error implementation with stack trace included.
ErrorBase features:
Object.defineProperties
is not available, you will need to use functions instead of stack
and message
properties, see belowmessage
is getter, not just string property, you can override getMessage() to construct message you need using any of your error object propertiesNote, that ErrorBase hasn't message
argument that Error has. However, you can provide it in your error class yourself, as well as any other arguments and properties you may need. But be careful with property names. For example, inaccurate overriding of message
property (which is a getter) can break functionality.
Defining custom error class:
var inherits = require('util').inherits;
var ErrorBase = require('nerr').ErrorBase;
var MyError = function (code) {
ErrorBase.call(this);
this.code = code;
};
inherits(MyError, ErrorBase);
MyError.prototype.name = 'MyError';
MyError.prototype.getMessage = function () {
return 'Error code: ' + this.code;
};
Using it:
try {
throw new MyError(500);
}
catch(err) {
// prints error name, message and stack trace
console.log('Stack trace:\n', err.stack);
console.log();
// prints error name and message
console.log('Error:', err.toString());
console.log();
console.log('Error name:', err.name);
console.log('Error message:', err.message);
}
ErrorBase constructor calls Error.captureStackTrace(obj, func)
which provides an object instance with a stack
getter (func
is used to appropriately truncate captured stack trace, excluding error construction function).
stack
getter returns concatenation of obj.toString() and a captured stack trace. Obtained value is cached, so sequential getter calls will return the same value even if obj.toString() result is changed.
Error.captureStackTrace()
is undocumented feature of V8 (at least), so it's behavior can be changed.
If captureStackTrace()
is not available, ErrorBase uses new Error()
to get stack trace. In this case it cannot exclude error construction part of trace.
If Object.defineProperties
is not available, message
and stack
getters will not be exposed, use getMessage()
and getStackTrace
functions instead.
MIT
FAQs
Properly constructed base class for error objects
The npm package nerr receives a total of 142 weekly downloads. As such, nerr popularity was classified as not popular.
We found that nerr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.