Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
create-error
Advanced tools
The create-error npm package is a utility for creating custom error types in JavaScript. It simplifies the process of defining and using custom error classes, making it easier to handle specific error conditions in your applications.
Creating a Basic Custom Error
This feature allows you to create a basic custom error type with a specific name. The custom error can then be thrown and caught like any standard error.
const createError = require('create-error');
const MyCustomError = createError('MyCustomError');
try {
throw new MyCustomError('Something went wrong!');
} catch (err) {
console.error(err.name); // MyCustomError
console.error(err.message); // Something went wrong!
}
Creating a Custom Error with Additional Properties
This feature allows you to create a custom error type with additional properties. In this example, a `statusCode` property is added to the custom error.
const createError = require('create-error');
const DetailedError = createError('DetailedError', { statusCode: 400 });
try {
throw new DetailedError('Invalid input!');
} catch (err) {
console.error(err.name); // DetailedError
console.error(err.message); // Invalid input!
console.error(err.statusCode); // 400
}
Inheriting from Custom Errors
This feature allows you to create custom error types that inherit from other custom error types. This is useful for creating a hierarchy of error types in your application.
const createError = require('create-error');
const AppError = createError('AppError');
const NotFoundError = createError('NotFoundError', AppError);
try {
throw new NotFoundError('Resource not found!');
} catch (err) {
console.error(err.name); // NotFoundError
console.error(err.message); // Resource not found!
console.error(err instanceof AppError); // true
}
The http-errors package is used to create HTTP errors for use in Express and other web frameworks. It provides a set of predefined HTTP error classes and allows for the creation of custom HTTP errors. Compared to create-error, http-errors is more focused on HTTP-specific error handling.
The custom-error-generator package provides a simple way to create custom error classes with additional properties. It is similar to create-error in terms of functionality but offers a different API for defining custom errors.
The extendable-error-class package allows you to create custom error classes that can be extended. It provides a base class for creating custom errors and is similar to create-error in terms of creating hierarchical error types.
A simple helper for creating subclassed errors in Javascript.
$ npm install create-error
$ bower install create-error
var createError = require('create-error');
var MyCustomError = createError('MyCustomError');
var SubCustomError = createError(MyCustomError, 'CoolSubError', {messages: []});
var sub = new SubCustomError('My Message', {someVal: 'value'});
sub instanceof SubCustomError // true
sub instanceof MyCustomError // true
sub instanceof Error // true
assert.deepEqual(sub.messages, []) // true
assert.equal(sub.someVal, 'value') // true
Creates a new error by specifying the name of the error to be created, taking an optional hash of properties to be attached to the error class upon creation.
Create a new error by specifying the Target
error class we wish to inherit from,
along with an optional name and properties for the error. If the name
is omitted,
it will have the same name as the parent error.
In the browser, the function will be assigned to window.createError
,
and createError.noConflict()
will restore the original window.createError
if overwritten.
MIT
FAQs
Simple helper for sub-classing the Error object
The npm package create-error receives a total of 197,510 weekly downloads. As such, create-error popularity was classified as popular.
We found that create-error 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.