Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
The error-ex npm package is designed to create more customizable and informative error objects in JavaScript. It allows developers to extend the native Error type with additional properties and behaviors, making error handling and debugging more efficient.
Creating custom error types
This feature allows developers to create custom error types by extending the native Error object. The custom error can then be thrown with a specific message, making it easier to identify and handle specific kinds of errors in the code.
const ErrorEx = require('error-ex');
const MyError = ErrorEx('MyError');
throw new MyError('Something went wrong');
Adding properties to errors
This functionality enables the addition of custom properties to error objects. In this example, a 'code' property is added to a custom error type, which could be used to store HTTP status codes or other relevant error information.
const ErrorEx = require('error-ex');
const MyError = ErrorEx('MyError');
MyError.prototype.code = 404;
const errorInstance = new MyError('Resource not found');
console.log(errorInstance.code); // 404
VError is a library for richer JavaScript errors. It allows chaining of errors, providing a way to wrap lower-level errors without losing the original context. Compared to error-ex, VError focuses more on error wrapping and context preservation.
http-errors is a package for creating HTTP error objects. It is specifically tailored for use in web applications, providing a straightforward way to create errors with HTTP status codes. Unlike error-ex, http-errors is more specialized for HTTP applications.
Easily subclass and customize new Error types
To include in your project:
var errorEx = require('error-ex');
To create an error message type with a specific name (note, that ErrorFn.name
will not reflect this):
var JSONError = errorEx('JSONError');
var err = new JSONError('error');
err.name; //-> JSONError
throw err; //-> JSONError: error
To add a stack line:
var JSONError = errorEx('JSONError', {fileName: errorEx.line('in %s')});
var err = new JSONError('error')
err.fileName = '/a/b/c/foo.json';
throw err; //-> (line 2)-> in /a/b/c/foo.json
To append to the error message:
var JSONError = errorEx('JSONError', {fileName: errorEx.append('in %s')});
var err = new JSONError('error');
err.fileName = '/a/b/c/foo.json';
throw err; //-> JSONError: error in /a/b/c/foo.json
errorEx([name], [properties])
Creates a new ErrorEx error type
name
: the name of the new type (appears in the error message upon throw;
defaults to Error.name
)properties
: if supplied, used as a key/value dictionary of properties to
use when building up the stack message. Keys are property names that are
looked up on the error message, and then passed to function values.
line
: if specified and is a function, return value is added as a stack
entry (error-ex will indent for you). Passed the property value given
the key.stack
: if specified and is a function, passed the value of the property
using the key, and the raw stack lines as a second argument. Takes no
return value (but the stack can be modified directly).message
: if specified and is a function, return value is used as new
.message
value upon get. Passed the property value of the property named
by key, and the existing message is passed as the second argument as an
array of lines (suitable for multi-line messages).Returns a constructor (Function) that can be used just like the regular Error constructor.
var errorEx = require('error-ex');
var BasicError = errorEx();
var NamedError = errorEx('NamedError');
// --
var AdvancedError = errorEx('AdvancedError', {
foo: {
line: function (value, stack) {
if (value) {
return 'bar ' + value;
}
return null;
}
}
}
var err = new AdvancedError('hello, world');
err.foo = 'baz';
throw err;
/*
AdvancedError: hello, world
bar baz
at tryReadme() (readme.js:20:1)
*/
errorEx.line(str)
Creates a stack line using a delimiter
This is a helper function. It is to be used in lieu of writing a value object for
properties
values.
str
: The string to create
%s
to specify where in the string the value should govar errorEx = require('error-ex');
var FileError = errorEx('FileError', {fileName: errorEx.line('in %s')});
var err = new FileError('problem reading file');
err.fileName = '/a/b/c/d/foo.js';
throw err;
/*
FileError: problem reading file
in /a/b/c/d/foo.js
at tryReadme() (readme.js:7:1)
*/
errorEx.append(str)
Appends to the error.message
string
This is a helper function. It is to be used in lieu of writing a value object for
properties
values.
str
: The string to append
%s
to specify where in the string the value should govar errorEx = require('error-ex');
var SyntaxError = errorEx('SyntaxError', {fileName: errorEx.append('in %s')});
var err = new SyntaxError('improper indentation');
err.fileName = '/a/b/c/d/foo.js';
throw err;
/*
SyntaxError: improper indentation in /a/b/c/d/foo.js
at tryReadme() (readme.js:7:1)
*/
Licensed under the MIT License. You can find a copy of it in LICENSE.
FAQs
Easy error subclassing and stack customization
The npm package error-ex receives a total of 24,532,564 weekly downloads. As such, error-ex popularity was classified as popular.
We found that error-ex demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.