What is make-error?
The make-error package is a simple library for creating custom error types in JavaScript. It is useful for defining new error constructors that work similarly to native Error types, allowing for better error handling and debugging in applications.
What are make-error's main functionalities?
Creating custom error types
This feature allows developers to create their own error types that can be used throughout their codebase. The custom error types will inherit from the base Error class, making them compatible with standard error handling mechanisms.
const makeError = require('make-error');
const CustomError = makeError('CustomError');
throw new CustomError('This is a custom error message.');
Extending custom error types
Developers can also extend their custom error types to create a hierarchy of errors. This is useful for creating more specific error types that still share common behavior with their parent error types.
const makeError = require('make-error');
const BaseError = makeError('BaseError');
const SpecificError = makeError(BaseError);
throw new SpecificError('This is a more specific error message.');
Other packages similar to make-error
http-errors
The http-errors package allows for the creation of HTTP error objects. It is similar to make-error but is specifically tailored for HTTP server use, providing a list of constructors for various HTTP status codes.
verror
Verror is a library for richer JavaScript errors, which can include multiple error causes, formatted error messages, and other metadata. It offers more features compared to make-error, which is more minimalistic.
extendable-error
Extendable-error is a simple base class for creating extendable custom error classes in JavaScript. It is similar to make-error in its simplicity and focus on creating custom error types.
make-error
Make your own error types!
Features
- Compatible Node & browsers
instanceof
supporterror.name
& error.stack
support- compatible with CSP (i.e. no
eval()
)
Installation
Installation of the npm package:
> npm install --save make-error
Then require the package:
var makeError = require("make-error");
Browser
You can directly use the build provided at unpkg.com:
<script src="https://unpkg.com/make-error@1/dist/make-error.js"></script>
Usage
Basic named error
var CustomError = makeError("CustomError");
throw new CustomError("a message");
Advanced error class
function CustomError(customValue) {
CustomError.super.call(this, "custom error message");
this.customValue = customValue;
}
makeError(CustomError);
CustomError.prototype.myMethod = function CustomError$myMethod() {
console.log("CustomError.myMethod (%s, %s)", this.code, this.message);
};
try {
throw new CustomError(42);
} catch (error) {
error.myMethod();
}
Specialized error
var SpecializedError = makeError("SpecializedError", CustomError);
throw new SpecializedError(42);
Inheritance
Best for ES2015+.
import { BaseError } from "make-error";
class CustomError extends BaseError {
constructor() {
super("custom error message");
}
}
Related
Contributions
Contributions are very welcomed, either on the documentation or on
the code.
You may:
- report any issue
you've encountered;
- fork and create a pull request.
License
ISC © Julien Fontanet