common-errors
Common error classes and utility functions
- Full suite of node.js Error classes like most other modern languages
- Append stack traces from other asynchronously generated Errors
- Generate your own custom Error classes in one line
- Map HTTP status codes to Errors for automatic handling in web services and applications
Install
npm install common-errors
Class Directory
Common Error Classes
Utility Functions
Express Middleware Functions
Common Error Classes
AlreadyInUseError
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 resource
args - 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 problem
inner_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 null
inner_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 message
inner_error - the Error instance that caused the current error. Stack trace will be appended.
// Example
throw new errors.AuthenticationRequiredError("Please provide authentication.", err)
ConnectionError
Applicable when an error occurs on a connection.
new ConnectionError(message[, inner_error])
Arguments
message - any message
inner_error - the Error instance that caused the current error. Stack trace will be appended.
// Example
throw new errors.ConnectionError('database connection no longer available', err);
DataError
Applicable when an error occurs on or with an external data source.
new DataError(message[, inner_error])
Arguments
message - any message
inner_error - the Error instance that caused the current error. Stack trace will be appended.
// Example
throw new errors.data.DataError('Too many rows returned from database', err);
MemcachedError
Applicable when an error occurs while using memcached.
new MemcachedError(message[, inner_error])
Arguments
message - any message
inner_error - the Error instance that caused the current error. Stack trace will be appended.
// Example
throw new errors.data.MemcachedError('Expected value not found', err);
MongoDBError
Applicable when an error occurs while using MongoDB.
new MongoDBError(message[, inner_error])
Arguments
message - any message
inner_error - the Error instance that caused the current error. Stack trace will be appended.
// Example
throw new errors.data.MongoDBError('Retrieved value not in expected format', err);
RedisError
Applicable when an error occurs while using redis.
new RedisError(message[, inner_error])
Arguments
message - any message
inner_error - the Error instance that caused the current error. Stack trace will be appended.
// Example
throw new errors.data.RedisError('expected value not found in redis', err);
RollbackError
Applicable when a transaction was unexpectedly rolled back.
new RollbackError(message[, inner_error])
Arguments
message - any message
inner_error - the Error instance that caused the current error. Stack trace will be appended.
// Example
throw new errors.data.RollbackError('database transaction was unexpectedly rolled back', err);
SQLError
Applicable when an error occurs while using a SQL database.
new SQLError(message[, inner_error])
Arguments
message - any message
inner_error - the Error instance that caused the current error. Stack trace will be appended.
// Example
throw new errors.data.SQLError('foreign key constraint violated', err);
TransactionError
Applicable when an error unexpectedly interrupts a transaction.
new TransactionError(message[, inner_error])
Arguments
message - any message
inner_error - the Error instance that caused the current error. Stack trace will be appended.
// Example
throw new errors.data.TransactionError('transaction already complete', 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 message
inner_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 integer
message - 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. The current mapping of error codes
to HTTP status codes is as follows:
{
"ValidationError": 400,
"ArgumentError": 400,
"AuthenticationRequiredError": 401,
"NotPermittedError": 403,
"ArgumentNullError": 404 // , or 400 depending on what's wrong with the request
"NotFoundError": 404,
"NotSupportedError": 405,
"AlreadyInUseError": 409,
}
To change the mappings, modify HttpStatusError.message_map and HttpStatusError.code_map
Arguments
err - any instanceof Error
req - the request object
// Example
throw new errors.HttpStatusError(err, req);
InvalidOperationError
Applicable when an invalid operation occurs.
new InvalidOperationError(message[, inner_error])
Arguments
message - any message
inner_error - the Error instance that caused the current error. Stack trace will be appended.
// Example
throw new errors.InvalidOperationError('divide by zero', err);
IOError
Base class for Errors while accessing information using streams, files and directories.
new IOError(message[, inner_error])
Arguments
message - any message
inner_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 message
inner_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 message
inner_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 message
inner_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 message
inner_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 message
inner_error - the Error instance that caused the current error. Stack trace will be appended.
// Example
throw new errors.io.FileNotFoundError("./package.json", err)
SocketError
Applicable when an error occurs on a socket.
new SocketError(message[, inner_error])
Arguments
message - any message
inner_error - the Error instance that caused the current error. Stack trace will be appended.
// Example
throw new errors.SocketError('socket no longer available', err);
NotFoundError
Applicable when an attempt to retrieve data yielded no result.
new NotFoundError(entity_name[, inner_error])
Arguments
entity_name - a description for what was not found
inner_error - the Error instance that caused the current error. Stack trace will be appended.
// Example
throw new errors.NotFoundError("User", err)
NotImplementedError
Applicable when a requested method or operation is not implemented.
new NotImplementedError(message[, inner_error])
Arguments
message - any message
inner_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 message
inner_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 message
inner_error