{"id":13527311,"url":"https://github.com/shutterstock/node-common-errors","last_synced_at":"2025-04-04T08:08:03.662Z","repository":{"id":57204295,"uuid":"11006364","full_name":"shutterstock/node-common-errors","owner":"shutterstock","description":"Common error classes and utility functions","archived":false,"fork":false,"pushed_at":"2021-08-27T21:32:48.000Z","size":134,"stargazers_count":284,"open_issues_count":4,"forks_count":26,"subscribers_count":39,"default_branch":"master","last_synced_at":"2024-05-13T22:44:06.130Z","etag":null,"topics":["error","errors","javascript","node"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shutterstock.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-06-27T19:55:36.000Z","updated_at":"2023-12-19T06:02:31.000Z","dependencies_parsed_at":"2022-09-18T00:51:01.676Z","dependency_job_id":null,"html_url":"https://github.com/shutterstock/node-common-errors","commit_stats":null,"previous_names":[],"tags_count":55,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shutterstock%2Fnode-common-errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shutterstock%2Fnode-common-errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shutterstock%2Fnode-common-errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shutterstock%2Fnode-common-errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shutterstock","download_url":"https://codeload.github.com/shutterstock/node-common-errors/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247142066,"owners_count":20890652,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["error","errors","javascript","node"],"created_at":"2024-08-01T06:01:45.508Z","updated_at":"2025-04-04T08:08:03.638Z","avatar_url":"https://github.com/shutterstock.png","language":"JavaScript","readme":"common-errors\n===========\n\n\u003e Common error classes and utility functions\n\n* Full suite of node.js Error classes like most other modern languages\n* Append stack traces from other asynchronously generated Errors\n* Generate your own custom Error classes in one line\n* Map HTTP status codes to Errors for automatic handling in web services and applications\n\n[![Build Status](https://secure.travis-ci.org/shutterstock/node-common-errors.png)](http://travis-ci.org/shutterstock/node-common-errors)\n\n## Install\n```npm install common-errors```\n\n## Class Directory\n\n### Common Error Classes\n\n* [AlreadyInUseError](#alreadyinuse)\n* [ArgumentError](#argument)\n* [ArgumentNullError](#argumentnull)\n* [AuthenticationRequiredError](#authrequired)\n* [ConnectionError](#connection)\n* [DataError](#data)\n  * [MemcacedError](#memcached)\n  * [MongoDBError](#mongodb)\n  * [RedisError](#redis)\n  * [RollbackError](#rollback)\n  * [SQLError](#sql)\n  * [TransactionError](#transaction)\n* [Error](#error)\n* [HttpStatusError](#httpstatus)\n* [InvalidOperationError](#invalidoperation)\n* [IOError](#io)\n  * [DirectoryNotFoundError](#directorynotfound)\n  * [DriveNotFoundError](#drivenotfound)\n  * [EndOfStreamError](#endofstream)\n  * [FileLoadError](#fileload)\n  * [FileNotFoundError](#filenotfound)\n  * [SocketError](#socket)\n* [NotFoundError](#notfound)\n* [NotImplementedError](#notimplemented)\n* [NotPermittedError](#notpermitted)\n* [NotSupportedError](#notsupported)\n* [OutOfMemoryError](#outofmemory)\n* [RangeError](#range)\n* [ReferenceError](#reference)\n* [StackOverflowError](#stackoverflow)\n* [SyntaxError](#syntax)\n* [TimeoutError](#timeout)\n* [TypeError](#type)\n* [URIError](#uri)\n* [ValidationError](#validation)\n\n\n### Utility Functions\n\n* [log](#log)\n* [prependCurrentStack](#prependCurrentStack)\n* [generateClass](#generateClass)\n\n### Express Middleware Functions\n\n* [Crash Protector](#crashprotector)\n* [Error Handler](#errorhandler)\n\n## Common Error Classes\n\n\u003ca name=\"alreadyinuse\" /\u003e\u003c/a\u003e\n\n### AlreadyInUseError\n\nApplicable when a resource is already in use, for example unique key constraints like a username.\n\n\tnew AlreadyInUseError(entityName, arg1, [arg2, arg3, arg4, ...])\n\n__Arguments__\n\n* `entityName` - the entity that owns the protected resource\n* `args` - the fields or attributes that are already in use\n\n```js\n// Example\nthrow new errors.AlreadyInUseError('user', 'username');\n```\n\n---------------------------------------\n\n\u003ca name=\"argument\" /\u003e\u003c/a\u003e\n\n### ArgumentError\n\nApplicable when there's a generic problem with an argument received by a function call.\n\n\tnew ArgumentError(argumentName[, inner_error])\n\n__Arguments__\n\n* `argumentName` - the name of the argument that has a problem\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.ArgumentError('username', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"argumentnull\" /\u003e\u003c/a\u003e\n\n### ArgumentNullError\n\nApplicable when an argument received by a function call is null/undefined or empty.\n\n\tnew ArgumentNullError(argumentName[, inner_error])\n\n__Arguments__\n\n* `argumentName` - the name of the argument that is null\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.ArgumentNullError('username', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"authrequired\" /\u003e\u003c/a\u003e\n\n### AuthenticationRequiredError\n\nApplicable when an operation requires authentication\n\n\tnew AuthenticationRequiredError(message, [inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.AuthenticationRequiredError(\"Please provide authentication.\", err)\n```\n\n---------------------------------------\n\n\u003ca name=\"connection\" /\u003e\u003c/a\u003e\n\n### ConnectionError\n\nApplicable when an error occurs on a connection.\n\n\tnew ConnectionError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.ConnectionError('database connection no longer available', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"data\" /\u003e\u003c/a\u003e\n\n### DataError\n\nApplicable when an error occurs on or with an external data source.\n\n\tnew DataError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.data.DataError('Too many rows returned from database', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"memcached\" /\u003e\u003c/a\u003e\n\n### MemcachedError\n\nApplicable when an error occurs while using memcached.\n\n\tnew MemcachedError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.data.MemcachedError('Expected value not found', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"mongodb\" /\u003e\u003c/a\u003e\n\n### MongoDBError\n\nApplicable when an error occurs while using MongoDB.\n\n\tnew MongoDBError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.data.MongoDBError('Retrieved value not in expected format', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"redis\" /\u003e\u003c/a\u003e\n\n### RedisError\n\nApplicable when an error occurs while using redis.\n\n\tnew RedisError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.data.RedisError('expected value not found in redis', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"rollback\" /\u003e\u003c/a\u003e\n\n### RollbackError\n\nApplicable when a transaction was unexpectedly rolled back.\n\n\tnew RollbackError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.data.RollbackError('database transaction was unexpectedly rolled back', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"sql\" /\u003e\u003c/a\u003e\n\n### SQLError\n\nApplicable when an error occurs while using a SQL database.\n\n\tnew SQLError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.data.SQLError('foreign key constraint violated', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"transaction\" /\u003e\u003c/a\u003e\n\n### TransactionError\n\nApplicable when an error unexpectedly interrupts a transaction.\n\n\tnew TransactionError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.data.TransactionError('transaction already complete', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"error\" /\u003e\u003c/a\u003e\n\n### Error\n\nThis is roughly the same as the native Error class. It additionally supports an inner_error attribute.\n\n\tnew Error(message, [inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.Error(\"Please provide authentication.\", err)\n```\n\n---------------------------------------\n\n\u003ca name=\"httpstatus\" /\u003e\u003c/a\u003e\n\n### HttpStatusError\n\nRepresents a message and a HTTP status code.\n\n\tnew HttpStatusError(status_code[, message])\n\n__Arguments__\n\n* `status_code` - any HTTP status code integer\n* `message` - any message\n\n```js\n// Example\nthrow new errors.HttpStatusError(404, \"Not Found\");\n```\n\n\tnew HttpStatusError(err[, req])\n\nFigure out a proper status code and message from a given error. The current mapping of error codes\nto HTTP status codes is as follows:\n\n```javascript\n{\n    \"ValidationError\": 400,\n    \"ArgumentError\": 400,\n    \"AuthenticationRequiredError\": 401,\n    \"NotPermittedError\": 403,\n    \"ArgumentNullError\": 404 // , or 400 depending on what's wrong with the request\n    \"NotFoundError\": 404,\n    \"NotSupportedError\": 405,\n    \"AlreadyInUseError\": 409,\n}\n```\n\nTo change the mappings, modify `HttpStatusError.message_map` and `HttpStatusError.code_map`\n\n__Arguments__\n\n* `err` - any instanceof Error\n* `req` - the request object\n\n\n\n```js\n// Example\nthrow new errors.HttpStatusError(err, req);\n```\n\n---------------------------------------\n\n\u003ca name=\"invalidoperation\" /\u003e\u003c/a\u003e\n\n### InvalidOperationError\n\nApplicable when an invalid operation occurs.\n\n\tnew InvalidOperationError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.InvalidOperationError('divide by zero', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"io\" /\u003e\u003c/a\u003e\n\n### IOError\n\nBase class for Errors while accessing information using streams, files and directories.\n\n\tnew IOError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.io.IOError(\"Could not open file\", err)\n```\n\n---------------------------------------\n\n\u003ca name=\"directorynotfound\" /\u003e\u003c/a\u003e\n\n### DirectoryNotFoundError\n\nApplicable when part of a file or directory cannot be found.\n\n\tnew DirectoryNotFoundError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.io.DirectoryNotFoundError(\"/var/log\", err)\n```\n\n---------------------------------------\n\n\u003ca name=\"drivenotfound\" /\u003e\u003c/a\u003e\n\n### DriveNotFoundError\n\nApplicable when trying to access a drive or share that is not available.\n\n\tnew DriveNotFoundError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.io.DriveNotFoundError(\"c\", err)\n```\n\n---------------------------------------\n\n\u003ca name=\"endofstream\" /\u003e\u003c/a\u003e\n\n### EndOfStreamError\n\nApplicable when reading is attempted past the end of a stream.\n\n\tnew EndOfStreamError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.io.EndOfStreamError(\"EOS while reading header\", err)\n```\n\n---------------------------------------\n\n\u003ca name=\"fileload\" /\u003e\u003c/a\u003e\n\n### FileLoadError\n\nApplicable when a file is found and read but cannot be loaded.\n\n\tnew FileLoadError(message[, inner_error])\n\n__Arguments__\n\n* `file_name` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.io.FileLoadError(\"./package.json\", err)\n```\n\n---------------------------------------\n\n\u003ca name=\"filenotfound\" /\u003e\u003c/a\u003e\n\n### FileNotFoundError\n\nApplicable when an attempt to access a file that does not exist on disk fails.\n\n\tnew FileNotFoundError(message[, inner_error])\n\n__Arguments__\n\n* `file_name` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.io.FileNotFoundError(\"./package.json\", err)\n```\n\n---------------------------------------\n\n\u003ca name=\"socket\" /\u003e\u003c/a\u003e\n\n### SocketError\n\nApplicable when an error occurs on a socket.\n\n\tnew SocketError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.SocketError('socket no longer available', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"notfound\" /\u003e\u003c/a\u003e\n\n### NotFoundError\n\nApplicable when an attempt to retrieve data yielded no result.\n\n\tnew NotFoundError(entity_name[, inner_error])\n\n__Arguments__\n\n* `entity_name` - a description for what was not found\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.NotFoundError(\"User\", err)\n```\n\n---------------------------------------\n\n\u003ca name=\"notimplemented\" /\u003e\u003c/a\u003e\n\n### NotImplementedError\n\nApplicable when a requested method or operation is not implemented.\n\n\tnew NotImplementedError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.NotImplementedError(\"Method is not yet implemented.\", err)\n```\n\n---------------------------------------\n\n\u003ca name=\"notpermitted\" /\u003e\u003c/a\u003e\n\n### NotPermittedError\n\nApplicable when an operation is not permitted\n\n\tnew NotPermittedError(message[, inner_error])\n\n__Arguments__\n\n* `message` - any message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.NotPermittedError(\"username cannot be changed once set.\", err)\n```\n\n---------------------------------------\n\n\u003ca name=\"notsupported\" /\u003e\u003c/a\u003e\n\n### NotSupportedError\n\nApplicable when a certain condition is not supported by your application.\n\n\tnew NotSupportedError(message[, inner_error])\n\n__Arguments__\n\n* `message` - a message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.NotSupportedError('Zero values', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"outofmemory\" /\u003e\u003c/a\u003e\n\n### OutOfMemoryError\n\nApplicable when there is not enough memory to continue the execution of a program.\n\n\tnew OutOfMemoryError(message[, inner_error])\n\n__Arguments__\n\n* `message` - a message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.OutOfMemoryError('Maximum mem size exceeded.', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"range\" /\u003e\u003c/a\u003e\n\n### RangeError\n\nRepresents 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.\n\n\tnew RangeError(message[, inner_error])\n\n__Arguments__\n\n* `message` - a message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.RangeError(\"Value must be between \" + MIN + \" and \" + MAX, err);\n```\n\n---------------------------------------\n\n\u003ca name=\"reference\" /\u003e\u003c/a\u003e\n\n### ReferenceError\n\nRepresents 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.\n\n\tnew ReferenceError(message[, inner_error])\n\n__Arguments__\n\n* `message` - a message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.ReferenceError(\"x is not defined\", err);\n```\n\n---------------------------------------\n\n\u003ca name=\"stackoverflow\" /\u003e\u003c/a\u003e\n\n### StackOverflowError\n\nApplicable when the execution stack overflows because it contains too many nested method calls.\n\n\tnew StackOverflowError(message[, inner_error])\n\n__Arguments__\n\n* `message` - a message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.StackOverflowError('Stack overflow detected.', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"syntax\" /\u003e\u003c/a\u003e\n\n### SyntaxError\n\nRepresents 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.\n\n\tnew SyntaxError(message[, inner_error])\n\n__Arguments__\n\n* `message` - a message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.SyntaxError(\"Unexpected token a\", err);\n```\n\n---------------------------------------\n\n\u003ca name=\"timeout\" /\u003e\u003c/a\u003e\n\n### TimeoutError\n\nApplicable when an operation takes longer than the alloted amount.\n\n\tnew TimeoutError(time[, inner_error])\n\n__Arguments__\n\n* `time` - a time duration\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.TimeoutError('100ms', err);\n```\n\n---------------------------------------\n\n\u003ca name=\"type\" /\u003e\u003c/a\u003e\n\n### TypeError\n\nRepresents 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.\n\n\tnew TypeError(message[, inner_error])\n\n__Arguments__\n\n* `message` - a message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.TypeError(\"number is not a function\", err);\n```\n\n---------------------------------------\n\n\u003ca name=\"uri\" /\u003e\u003c/a\u003e\n\n### URIError\n\nRepresents 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.\n\n\tnew URIError(message[, inner_error])\n\n__Arguments__\n\n* `message` - a message\n* `inner_error` - the Error instance that caused the current error. Stack trace will be appended.\n\n```js\n// Example\nthrow new errors.URIError(\"URI malformed\", err);\n```\n\n---------------------------------------\n\n\u003ca name=\"validation\" /\u003e\u003c/a\u003e\n\n### ValidationError\n\nUseful for denoting a problem with a user-defined value.  Generally, you won't throw this error.\nIt serializes to JSON, and it can also function as an envelope for multiple errors.\n\n\tnew ValidationError(message, [code], [field])\n\n__Arguments__\n\n* `message` - any message\n* `code` - an optional error code\n* `field` - an optional description of the data\n\n__Methods__\n\n* `addError(error)` - add an error object to the `errors` array, and return `this`.\n* `addErrors(errors)` - append an array of error objects to the `errors` array, and return `this`.\n\n```js\n// Example\nfunction validateUsername(username){\n\tvar errors = new errors.ValidationError();\n\tif(username.length \u003c 3) errors.addError(new errors.ValidationError(\"username must be at least two characters long\", \"VAL_MIN_USERNAME_LENGTH\", \"username\"));\n\tif(/-%$*\u0026!/.test(username)) errors.addError(new errors.ValidationError(\"username may not contain special characters\", \"VAL_USERNAME_SPECIALCHARS\", \"username\"));\n\treturn errors;\n}\n```\n\n## Utility Functions\n\n\u003ca name=\"log\" /\u003e\u003c/a\u003e\n\n### Log\n\nModifies an error's stack to include the current stack and logs it to *stderr*.  Useful for logging errors received by a callback.\n\n\tlog(err[, message])\n\n__Arguments__\n\n* `err` - any error or error message received from a callback\n* `message` - any message you'd like to prepend\n\n```js\n// Example\nmysql.query('SELECT * `FROM` users', function(err, results){\n\tif(err) return errors.log(err, \"Had trouble retrieving users.\");\n\tconsole.log(results);\n});\n```\n\n\u003ca name=\"prependCurrentStack\" /\u003e\u003c/a\u003e\n\n### prependCurrentStack\n\nModifies an error's stack to include the current stack without logging it.  Useful for logging errors received by a callback.\n\n\tprependCurrentStack(err)\n\n__Arguments__\n\n* `err` - any error or error message received from a callback\n\n```js\n// Example\nmysql.query('SELECT * `FROM` users', function(err, results){\n\tif(err) {\n\t\treturn errors.prependCurrentStack(err); // caller has better idea of source of err\n\t}\n\tconsole.log(results);\n});\n```\n\n\u003ca name=\"generateClass\" /\u003e\u003c/a\u003e\n\n### generateClass\n\nSimple interface for generating a new Error class type.\n\n\thelpers.generateClass(name[, options])\n\n__Arguments__\n\n* `name` - The full name of the new Error class\n* `options`\n  * `extends` - The base class for the new Error class. Default is `Error`.\n  * `globalize` - Boolean (default `true`) to store the Error in global space so that the Error is equivalent to others included from other versions of the module.\n  * `args` - Array of names of values to accept and store from the class constructor. Default is `['message', 'inner_error']`.\n  * `generateMessage` - A function for defining a custom error message.\n\n```js\n// Example\nvar ArgumentNullError = helpers.generateClass(\"ArgumentNullError\", {\n  extends: ArgumentError,\n  args: ['argumentName'],\n  generateMessage: function(){\n    return \"Missing argument: \" + this.argumentName;\n  }\n});\n\nthrow new ArgumentNullError(\"username\");\n```\n\n\n## Express Middleware Functions\n\n\u003ca name=\"crashprotector\" /\u003e\u003c/a\u003e\n\n### Crash Protector\n\nExpress middleware for preventing the web server from crashing when an error is thrown from an asynchronous context.  \nAny error that would have caused a crash is logged to *stderr*.\n\n```js\n// Example\nvar app = express();\n\napp.use(express.static(__dirname + '/../public'));\napp.use(express.bodyParser());\napp.use(errors.middleware.crashProtector());\n\n//insert new middleware here\n\napp.get('/healthcheck', function (req, res, next){res.send('YESOK')});\n\napp.use(app.router);\napp.use(errors.middleware.errorHandler);\n\nmodule.exports = app;\n```\n\n\u003ca name=\"errorhandler\" /\u003e\u003c/a\u003e\n\n### Error Handler\n\nExpress middleware that translates common errors into HTTP status codes and messages.\n\n```js\n// Example\nvar app = express();\n\napp.use(express.static(__dirname + '/../public'));\napp.use(express.bodyParser());\napp.use(errors.middleware.crashProtector());\n\n//insert new middleware here\n\napp.get('/healthcheck', function (req, res, next){res.send('YESOK')});\n\napp.use(app.router);\napp.use(errors.middleware.errorHandler);\n\nmodule.exports = app;\n```\n\n## Authors\n\nThis library was developed by David Fenster at [Shutterstock](http://www.shutterstock.com)\n\n## Contribute\n\nPlease do! Check out our [Contributing guidelines](CONTRIBUTING.md).\n\n## License\n\n[MIT](LICENSE) © 2013-2017 Shutterstock Images, LLC\n","funding_links":[],"categories":["JavaScript","Packages","Number"],"sub_categories":["Miscellaneous"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshutterstock%2Fnode-common-errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshutterstock%2Fnode-common-errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshutterstock%2Fnode-common-errors/lists"}