User errors for Node.js
This Node.js module extends Error class by adding a code
property. This allows you to easily
identify instances at your error handler, just like system errors.
All you need to get it with you Node.js. If you're using a Linux system, you can use node-install for an easy installation.
To use have this module installed, just follow the next instructions.
ૐ » your-project-dir λ npm install node-usererror
ૐ » your-project-dir λ git clone https://github.com/IvanGaravito/node-usererror.git
Just require the module and use it like if it were the Error class:
var UserError = require('node-usererror')
try {
throw new UserError('My own error', 'EOWN')
} catch (err) {
if (err.code === 'EOWN') {
console.log(err) // Outputs: { [Error: My own error] code: 'EOWN', message: 'My own error' }
} else {
console.log('Something went wrong, aborting...')
process.exit(1)
}
}
Example
var UserError = require('node-usererror')
// ...
throw new UserError('My own error', 'EOWN')
Kind: inner class of node-usererror
Extends: Error
Param | Type | Description |
---|---|---|
message | string |
The error message |
code | any |
The error code you want to use for this error |
The MIT License (MIT)
Copyright (c) 2016 Ivan Garavito ivangaravito@gmail.com
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.