🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

common-errors

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

common-errors

Common error classes and utility functions

0.1.2
Source
npm
Version published
Weekly downloads
6K
11.35%
Maintainers
1
Weekly downloads
 
Created
Source

node-errors

Common error classes and utility functions

Class Directory

Common Error Classes

  • AlreadyInUseError
  • ArgumentError
  • ArgumentNullError
  • GenericError
  • HttpStatusError
  • NotPermittedError
  • ValidationError

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.

Arguments

new ArgumentNullError(entityName, arg1, [arg2, arg3, arg4, ...])

  • entityName - the entity that owns the protected resource
  • args - the fields or attributes that are already in use
throw new errors.ArgumentNull('user', 'username');
### ArgumentError

Applicable when there's a generic problem with an argument received by a function call.

Arguments

new ArgumentError(argumentName)

  • argumentName - the name of the argument that has a problem
throw new errors.Argument('username');
### ArgumentNullError

Applicable when an argument received by a function call is null/undefined or empty.

Arguments

new ArgumentNullError(argumentName)

  • argumentName - the name of the argument that is null
throw new errors.ArgumentNull('username');
### GenericError

Applicable for any error returned by a callback, or any time you want to throw a new error that is based from another error. Call stacks from both errors will be concatenated for a full call stack. This effectively patches a design issue in JavaScript where logging an error from an asynchronous callback will show only the call stack from the error's context, but does not show the the stack where it was consumed. For example, if you perform a SQL query that returns an error with the callback, you don't know where in your code the offending query was generated. Wrapping the returned error in your own GenericError will solve this problem.

Arguments

new GenericError(message[, innerError])

  • message - any message you want
  • innerError - any error that you want to preserve with the new error
mysql.query('SELECT * FROM users', function(err, results){
	if(err) return new errors.Generic("Had trouble retrieving users.", err);
	console.log(results);
})
### HttpStatusError

Represents a message and a HTTP status code.

Arguments

new HttpStatusError(message, statusCode)

  • message - any message
  • statusCode - any HTTP status code integer
throw new errors.HttpStatus("Not Found", 404);
### NotPermittedError

Applicable when an operation is not permitted

Arguments

new NotPermittedError(message)

  • message - any message
throw new errors.NotPermitted("username cannot be changed once set.")
### ValidationError

Useful for denoting a problem with a user-defined value. Generally, you wont throw this error.

Arguments

new ValidationError(message[, code])

  • message - any message
  • code - an optional error code
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;
}

Utility Functions

### Log

Wraps a given error in a GenericError and logs it to stderr. Useful for logging errors received by a callback.

Arguments

log(err[, message])

  • err - any error or error message received from a callback
  • message - any message you'd like to prepend
mysql.query('SELECT * FROM users', function(err, results){
	if(err) return errors.log(err, "Had trouble retrieving users.");
	console.log(results);
});

Express Middleware Functions

### Crash Protector

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.

var app = appInitialize.initialize(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.

var app = appInitialize.initialize(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;

Authors

This library was developed by David Fenster at Shutterstock

License

Copyright (C) 2013 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.

Keywords

common

FAQs

Package last updated on 28 Jun 2013

Did you know?

Socket

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.

Install

Related posts