mongodb-js-errors
Advanced tools
Comparing version 0.1.0 to 0.2.0
108
index.js
@@ -10,2 +10,57 @@ /* eslint camelcase:0, complexity: 1 */ | ||
/** | ||
* A mapping of a boom function to a message. | ||
* | ||
* @param {Function} func - The boom function. | ||
* @param {String} message - The message. | ||
*/ | ||
function Mapping(func, message) { | ||
this.func = func; | ||
this.message = message; | ||
} | ||
/** | ||
* Translate the message to a mapping. | ||
* | ||
* @param {String} msg - The message to translate. | ||
* | ||
* @returns {Mapping} The function/message mapping. | ||
*/ | ||
function translate(msg) { | ||
if (/connection closed/.test(msg)) { | ||
return new Mapping(boom.serverTimeout, 'The connection to MongoDB was closed'); | ||
} else if (/cannot drop/.test(msg)) { | ||
return new Mapping(boom.badRequest, 'This index cannot be destroyed'); | ||
} else if (/(auth failed|Authentication Failed)/.test(msg)) { | ||
return new Mapping(boom.forbidden, 'Invalid auth credentials'); | ||
} else if (/(ECONNREFUSED|ENOTFOUND)/.test(msg)) { | ||
return new Mapping(boom.notFound, 'MongoDB not running on the provided host and port'); | ||
} else if (/connection to \[.*\] timed out/.test(msg)) { | ||
return new Mapping(boom.notFound, 'Could not connect to MongoDB because the conection timed out'); | ||
} else if (/failed to connect/.test(msg)) { // Host not reachable | ||
return new Mapping(boom.notFound, 'Could not connect to MongoDB on the provided host and port'); | ||
} else if (/does not exist/.test(msg)) { | ||
return new Mapping(boom.notFound, msg); | ||
} else if (/already exists/.test(msg)) { | ||
return new Mapping(boom.conflict, msg); | ||
} else if (/pipeline element 0 is not an object/.test(msg)) { | ||
return new Mapping(boom.badRequest, msg); | ||
} else if (/(target namespace exists|already exists)/.test(msg)) { | ||
return new Mapping(boom.conflict, 'Collection already exists'); | ||
} else if (/server .* sockets closed/.test(msg)) { | ||
return new Mapping(boom.serverTimeout, 'The connection to MongoDB was closed'); | ||
} else if (/operation exceeded time limit/.test(msg)) { | ||
return new Mapping(boom.serverTimeout, 'Operation exceeded the specified time limit'); | ||
} else if (/Error from KDC: UNKNOWN_SERVER/.test(msg)) { | ||
return new Mapping(boom.serverTimeout, 'Invalid service name'); | ||
} else if (/Matching credential/.test(msg)) { | ||
return new Mapping(boom.serverTimeout, 'Invalid principal'); | ||
} else if (/self signed certificate in certificate chain/.test(msg)) { | ||
return new Mapping(boom.serverTimeout, 'Invalid or missing certificate'); | ||
} else if (/socket hang up/.test(msg)) { | ||
return new Mapping(boom.serverTimeout, 'Socket could not establish connection to provided host and port'); | ||
} | ||
return null; | ||
} | ||
/** | ||
* Is the error returned from the driver caused | ||
@@ -83,44 +138,19 @@ * by an access control restriction? | ||
debug('decoding error message: `%s`', msg); | ||
/** | ||
* mongod won't let us connect anymore because we have too many idle | ||
* connections to it. Restart the process to flush and get back to | ||
* a clean state. | ||
*/ | ||
if (/connection closed/.test(msg)) { | ||
err = boom.serverTimeout('Too many connections to mongod'); | ||
} else if (/cannot drop/.test(msg)) { | ||
err = boom.badRequest('This index cannot be destroyed'); | ||
} else if (/auth failed/.test(msg)) { | ||
err = boom.forbidden('Invalid auth credentials'); | ||
} else if (/connection to \[.*\] timed out/.test(msg)) { | ||
err = boom.notFound('Could not connect to MongoDB because the conection timed out'); | ||
} else if (/failed to connect/.test(msg)) { // Host not reachable | ||
err = boom.notFound('Could not connect to MongoDB. Is it running?'); | ||
} else if (/does not exist/.test(msg)) { | ||
err = boom.notFound(msg); | ||
} else if (/already exists/.test(msg)) { | ||
err = boom.conflict(msg); | ||
} else if (/pipeline element 0 is not an object/.test(msg)) { | ||
err = boom.badRequest(msg); | ||
} else if (/(target namespace exists|already exists)/.test(err.message)) { | ||
err = boom.conflict('Collection already exists'); | ||
} else if (/server .* sockets closed/.test(msg)) { | ||
err = boom.serverTimeout('Too many connections to MongoDB'); | ||
} else if (/operation exceeded time limit/.test(msg)) { | ||
err = boom.serverTimeout('Operation exceeded the specified time limit'); | ||
} else if (/connect ECONNREFUSED/.test(msg)) { | ||
err = boom.notFound('MongoDB not running'); | ||
} else if (err.name === 'MongoError') { | ||
// All unknown driver errors to bubble up. | ||
err = boom.badImplementation(err.message); | ||
err.driver = true; | ||
} else { | ||
debug('does not look like a driver error'); | ||
process.nextTick(fn.bind(null, err)); | ||
return; | ||
var mapping = translate(msg); | ||
if (mapping === null) { | ||
if (err.name === 'MongoError') { | ||
// All unknown driver errors to bubble up. | ||
mapping = new Mapping(boom.badImplementation, msg); | ||
} else { | ||
debug('does not look like a driver error'); | ||
process.nextTick(fn.bind(null, err)); | ||
return; | ||
} | ||
} | ||
debug('decoded error is: `%s`', err.message); | ||
process.nextTick(fn.bind(null, err)); | ||
debug('decoded error is: `%s`', mapping.message); | ||
process.nextTick(fn.bind(null, mapping.func.call(null, mapping.message))); | ||
}; | ||
module.exports = exports; | ||
module.exports.translate = translate; |
{ | ||
"name": "mongodb-js-errors", | ||
"description": "Helpers for handling errors from the MongoDB driver.", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"scripts": { | ||
@@ -21,2 +21,3 @@ "fmt": "mongodb-js-fmt ./*.js test/*.js", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"eslint-config-mongodb-js": "^1.0.5", | ||
@@ -23,0 +24,0 @@ "mocha": "^2.3.3", |
@@ -29,5 +29,5 @@ # mongodb-errors [![travis][travis_img]][travis_url] [![npm][npm_img]][npm_url] | ||
[travis_img]: https://img.shields.io/travis/mongodb-js/mongodb-js-errors.svg | ||
[travis_url]: https://travis-ci.org/mongodb-js/mongodb-js-errors | ||
[travis_img]: https://img.shields.io/travis/mongodb-js/errors.svg | ||
[travis_url]: https://travis-ci.org/mongodb-js/errors | ||
[npm_img]: https://img.shields.io/npm/v/mongodb-js-errors.svg | ||
[npm_url]: https://npmjs.org/package/mongodb-js-errors |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18944
169
6
1