make-error
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The "make-error" npm package is a handy tool designed to assist developers in creating their own unique error types to use in their JavaScript code. It provides essential features, including compatibility with Node.js and browsers, instanceof
support, error.name
and error.stack
support, and complies with the Content Security Policy (CSP), ensuring it avoids utilizing eval()
. This package is a highly beneficial addition to any codebase requiring a higher level of error customization and targeted error debugging.
"Make-error" is a user-friendly package and can be used effortlessly. Start by installing the npm package using the command npm install --save make-error
. Then require the package in your JavaScript file using var makeError = require("make-error")
.
You can create your basic named error with the following code:
var CustomError = makeError("CustomError");
throw new CustomError("a message");
For a more advanced error class, you can follow this pattern:
function CustomError(customValue) {
CustomError.super.call(this, "custom error message");
this.customValue = customValue;
}
makeError(CustomError);
// You can extend the prototype
CustomError.prototype.myMethod = function CustomError$myMethod() {
console.log("CustomError.myMethod (%s, %s)", this.code, this.message);
};
// Throwing your Custom error
try {
throw new CustomError(42);
} catch (error) {
error.myMethod();
}
For specialized errors use:
var SpecializedError = makeError("SpecializedError", CustomError);
throw new SpecializedError(42);
To utilize inheritance with ES2015+:
import { BaseError } from "make-error";
class CustomError extends BaseError {
constructor() {
super("custom error message");
}
}
The entire documentation for the "make-error" npm package can be found within the README file on the official GitHub repository, which is git://github.com/JsCommunity/make-error.git. Here, you can find comprehensive details regarding the various features, installation instructions, usage examples, and more. It's your one-stop spot for everything you need to know about the "make-error" package.