What is makeerror?
The makeerror npm package provides a simple way to create custom error types in JavaScript. It allows developers to define new error types with specific properties and methods, enhancing error handling in applications by making it more structured and informative.
What are makeerror's main functionalities?
Creating custom error types
This feature allows developers to create new error types. The example shows how to define a custom error type named 'MyError' and create an instance of it with a specific message.
const makeError = require('makeerror');
const MyError = makeError('MyError');
const errorInstance = new MyError('Something went wrong!');
console.log(errorInstance.name); // 'MyError'
console.log(errorInstance.message); // 'Something went wrong!'
Custom error with additional properties
This feature demonstrates how to add additional properties to a custom error type. In the example, a 'statusCode' property is added to the 'MyError' type, which can be useful for HTTP error handling.
const makeError = require('makeerror');
const MyError = makeError('MyError', { statusCode: 404 });
const errorInstance = new MyError('Not found');
console.log(errorInstance.statusCode); // 404
Other packages similar to makeerror
verror
The verror package provides a way to create rich JavaScript errors, which can include multiple error causes, formatted messages, and other metadata. It is more feature-rich compared to makeerror, offering advanced error chaining and customization options.
http-errors
This package is specifically designed to generate HTTP errors for Node.js applications. Unlike makeerror, which is general-purpose, http-errors focuses on HTTP status codes and messages, making it ideal for web server development.
makeerror 
A library to make errors.
Basics
Makes an Error constructor function with the signature below. All arguments are
optional, and if the first argument is not a String
, it will be assumed to be
data
:
function(message, data)
You'll typically do something like:
var makeError = require('makeerror')
var UnknownFileTypeError = makeError(
'UnknownFileTypeError',
'The specified type is not known.'
)
var er = UnknownFileTypeError()
er
will have a prototype chain that ensures:
er instanceof UnknownFileTypeError
er instanceof Error
Templatized Error Messages
There is support for simple string substitutions like:
var makeError = require('makeerror')
var UnknownFileTypeError = makeError(
'UnknownFileTypeError',
'The specified type "{type}" is not known.'
)
var er = UnknownFileTypeError({ type: 'bmp' })
Now er.message
or er.toString()
will return 'The specified type "bmp" is not known.'
.
Prototype Hierarchies
You can create simple hierarchies as well using the prototype
chain:
var makeError = require('makeerror')
var ParentError = makeError('ParentError')
var ChildError = makeError(
'ChildError',
'The child error.',
{ proto: ParentError() }
)
var er = ChildError()
er
will have a prototype chain that ensures:
er instanceof ChildError
er instanceof ParentError
er instanceof Error