Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
The es6-error package provides a simple and effective way to create custom error classes in JavaScript using ES6 syntax. It extends the native Error class, making it easier to create and manage custom error types with proper stack traces.
Creating Custom Error Classes
This feature allows you to create custom error classes by extending the ExtendableError class provided by es6-error. The custom error class can then be used to throw and catch specific error types, with proper stack traces.
class CustomError extends ExtendableError {
constructor(message) {
super(message);
this.name = 'CustomError';
}
}
try {
throw new CustomError('This is a custom error');
} catch (err) {
console.error(err.name); // CustomError
console.error(err.message); // This is a custom error
console.error(err.stack); // Stack trace
}
Handling Custom Errors
This feature demonstrates how to handle custom errors by creating a specific error class (NotFoundError) and using it in a function. When the error is thrown, it can be caught and handled specifically based on its type.
class NotFoundError extends ExtendableError {
constructor(resource) {
super(`${resource} not found`);
this.name = 'NotFoundError';
}
}
function findResource(resource) {
if (!resource) {
throw new NotFoundError('Resource');
}
return resource;
}
try {
findResource(null);
} catch (err) {
if (err instanceof NotFoundError) {
console.error('Specific handling for NotFoundError');
} else {
console.error('General error handling');
}
}
The custom-error-generator package provides a way to create custom error classes with additional properties and methods. It is similar to es6-error but offers more flexibility in defining custom error properties and methods.
The extendable-error-class package allows you to create custom error classes by extending the native Error class. It is similar to es6-error but focuses on providing a minimalistic approach to creating custom error classes.
The create-error-class package provides a simple way to create custom error classes with additional properties. It is similar to es6-error but offers a more concise syntax for defining custom error classes.
An easily-extendable error class for use with ES6 classes (or ES5, if you so choose).
Tested in Node 4.0, Chrome, and Firefox.
I made this because I wanted to be able to extend Error for inheritance and type
checking, but can never remember to add
Error.captureStackTrace(this, this.constructor.name)
to the constructor or how
to get the proper name to print from console.log
.
import ExtendableError from 'es6-error';
class MyError extends ExtendableError {
// constructor is optional; you should omit it if you just want a custom error
// type for inheritance and type checking
constructor(message = 'Default message') {
super(message);
}
}
export default MyError;
var util = require('util');
var ExtendableError = require('es6-error');
function MyError(message) {
message = message || 'Default message';
ExtendableError.call(this, message);
}
util.inherits(MyError, ExtendableError);
module.exports = MyError;
[v4.0.1] - 2017-01-04
babel-plugin-transform-builtin-extend
(#27)FAQs
Easily-extendable error for use with ES6 classes
The npm package es6-error receives a total of 4,562,038 weekly downloads. As such, es6-error popularity was classified as popular.
We found that es6-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.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.