
Security News
Node.js Homepage Adds Paid Support Link, Prompting Contributor Pushback
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
common-errors
Advanced tools
Common error classes and utility functions
Applicable when a resource is already in use, for example unique key constraints like a username.
new AlreadyInUseError(entityName, arg1, [arg2, arg3, arg4, ...])
Arguments
entityName
- the entity that owns the protected resourceargs
- the fields or attributes that are already in use// Example
throw new errors.AlreadyInUseError('user', 'username');
### ArgumentError
Applicable when there's a generic problem with an argument received by a function call.
new ArgumentError(argumentName[, inner_error])
Arguments
argumentName
- the name of the argument that has a probleminner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.ArgumentError('username', err);
### ArgumentNullError
Applicable when an argument received by a function call is null/undefined or empty.
new ArgumentNullError(argumentName[, inner_error])
Arguments
argumentName
- the name of the argument that is nullinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.ArgumentNullError('username', err);
### AuthenticationRequiredError
Applicable when an operation requires authentication
new AuthenticationRequiredError(message, [inner_error])
Arguments
message
- any messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.AuthenticationRequiredError("Please provide authentication.", err)
### Error
This is roughly the same as the native Error class. It additionally supports an inner_error attribute.
new Error(message, [inner_error])
Arguments
message
- any messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.Error("Please provide authentication.", err)
### HttpStatusError
Represents a message and a HTTP status code.
new HttpStatusError(status_code[, message])
Arguments
status_code
- any HTTP status code integermessage
- any message// Example
throw new errors.HttpStatusError(404, "Not Found");
new HttpStatusError(err[, req])
Figure out a proper status code and message from a given error.
To change the mappings, modify HttpStatusError.message_map
and HttpStatusError.code_map
Arguments
err
- any instanceof Errorreq
- the request object// Example
throw new errors.HttpStatusError(err, req);
### IOError
Base class for Errors while accessing information using streams, files and directories.
new IOError(message[, inner_error])
Arguments
message
- any messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.io.IOError("Could not open file", err)
### DirectoryNotFoundError
Applicable when part of a file or directory cannot be found.
new DirectoryNotFoundError(message[, inner_error])
Arguments
message
- any messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.io.DirectoryNotFoundError("/var/log", err)
### DriveNotFoundError
Applicable when trying to access a drive or share that is not available.
new DriveNotFoundError(message[, inner_error])
Arguments
message
- any messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.io.DriveNotFoundError("c", err)
### EndOfStreamError
Applicable when reading is attempted past the end of a stream.
new EndOfStreamError(message[, inner_error])
Arguments
message
- any messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.io.EndOfStreamError("EOS while reading header", err)
### FileLoadError
Applicable when a file is found and read but cannot be loaded.
new FileLoadError(message[, inner_error])
Arguments
file_name
- any messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.io.FileLoadError("./package.json", err)
### FileNotFoundError
Applicable when an attempt to access a file that does not exist on disk fails.
new FileNotFoundError(message[, inner_error])
Arguments
file_name
- any messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.io.FileNotFoundError("./package.json", err)
### NotImplementedError
Applicable when a requested method or operation is not implemented.
new NotImplementedError(message[, inner_error])
Arguments
message
- any messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.NotImplementedError("Method is not yet implemented.", err)
### NotPermittedError
Applicable when an operation is not permitted
new NotPermittedError(message[, inner_error])
Arguments
message
- any messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.NotPermittedError("username cannot be changed once set.", err)
### NotSupportedError
Applicable when a certain condition is not supported by your application.
new NotSupportedError(message[, inner_error])
Arguments
message
- a messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.NotSupportedError('Zero values', err);
### OutOfMemoryError
Applicable when there is not enough memory to continue the execution of a program.
new OutOfMemoryError(message[, inner_error])
Arguments
message
- a messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.OutOfMemoryError('Maximum mem size exceeded.', err);
### RangeError
Represents an error that occurs when a numeric variable or parameter is outside of its valid range. This is roughly the same as the native RangeError class. It additionally supports an inner_error attribute.
new RangeError(message[, inner_error])
Arguments
message
- a messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.RangeError("Value must be between " + MIN + " and " + MAX, err);
### ReferenceError
Represents an error when a non-existent variable is referenced. This is roughly the same as the native ReferenceError class. It additionally supports an inner_error attribute.
new ReferenceError(message[, inner_error])
Arguments
message
- a messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.ReferenceError("x is not defined", err);
### StackOverflowError
Applicable when the execution stack overflows because it contains too many nested method calls.
new StackOverflowError(message[, inner_error])
Arguments
message
- a messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.StackOverflowError('Stack overflow detected.', err);
### SyntaxError
Represents an error when trying to interpret syntactically invalid code. This is roughly the same as the native SyntaxError class. It additionally supports an inner_error attribute.
new SyntaxError(message[, inner_error])
Arguments
message
- a messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.SyntaxError("Unexpected token a", err);
### TypeError
Represents an error when a value is not of the expected type. This is roughly the same as the native TypeError class. It additionally supports an inner_error attribute.
new TypeError(message[, inner_error])
Arguments
message
- a messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.TypeError("number is not a function", err);
### URIError
Represents an error when a value is not of the expected type. This is roughly the same as the native URIError class. It additionally supports an inner_error attribute.
new URIError(message[, inner_error])
Arguments
message
- a messageinner_error
- the Error instance that caused the current error. Stack trace will be appended.// Example
throw new errors.URIError("URI malformed", err);
### ValidationError
Useful for denoting a problem with a user-defined value. Generally, you won't throw this error.
new ValidationError(message[, code])
Arguments
message
- any messagecode
- an optional error code// Example
function validateUsername(username){
var errors = [];
if(username.length < 3) errors.push(new errors.Validation("username must be at least two characters long", "VAL_MIN_USERNAME_LENGTH"));
if(/-%$*&!/.test(username)) errors.push(new errors.Validation("username may not contain special characters", "VAL_USERNAME_SPECIALCHARS"));
return errors;
}
Modifies an error's stack to include the current stack and logs it to stderr. Useful for logging errors received by a callback.
log(err[, message])
Arguments
err
- any error or error message received from a callbackmessage
- any message you'd like to prepend// Example
mysql.query('SELECT * `FROM` users', function(err, results){
if(err) return errors.log(err, "Had trouble retrieving users.");
console.log(results);
});
### generateClass
Simple interface for generating a new Error class type.
helpers.generateClass(name[, options])
Arguments
name
- The full name of the new Error classoptions
extends
- The base class for the new Error class. Default is Error
.args
- Array of names of values to accept and store from the class constructor. Default is ['message']
.generateMessage
- A function for defining a custom error message.// Example
var ArgumentNullError = helpers.generateClass("ArgumentNullError", {
extends: ArgumentError,
args: ['argumentName'],
generateMessage: function(){
return "Missing argument: " + this.argumentName;
}
});
throw new ArgumentNullError("username");
Express middleware for preventing the web server from crashing when an error is thrown from an asynchronous context.
Any error that would have caused a crash is logged to stderr.
// Example
var app = express();
app.use(express.static(__dirname + '/../public'));
app.use(express.bodyParser());
app.use(errors.middleware.crashProtector());
//insert new middleware here
app.get('/healthcheck', function (req, res, next){res.send('YESOK')});
app.use(app.router);
app.use(errors.middleware.errorHandler);
module.exports = app;
### Error Handler
Express middleware that translates common errors into HTTP status codes and messages.
// Example
var app = express();
app.use(express.static(__dirname + '/../public'));
app.use(express.bodyParser());
app.use(errors.middleware.crashProtector());
//insert new middleware here
app.get('/healthcheck', function (req, res, next){res.send('YESOK')});
app.use(app.router);
app.use(errors.middleware.errorHandler);
module.exports = app;
This library was developed by David Fenster at Shutterstock
Copyright (C) 2013-2014 by Shutterstock Images, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Common error classes and utility functions
The npm package common-errors receives a total of 2,204 weekly downloads. As such, common-errors popularity was classified as popular.
We found that common-errors demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.
Research
Security News
The Socket Research Team investigates a malicious Python typosquat of a popular password library that forces Windows shutdowns when input is incorrect.