https://github.com/jneidel/err-hndlr
Error handler for either throwing locally or sending a bug ticket to a rest api
https://github.com/jneidel/err-hndlr
error-handler error-handling error-log errors node-module
Last synced: about 1 year ago
JSON representation
Error handler for either throwing locally or sending a bug ticket to a rest api
- Host: GitHub
- URL: https://github.com/jneidel/err-hndlr
- Owner: jneidel
- License: mit
- Created: 2018-09-16T07:39:52.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-24T21:07:00.000Z (over 7 years ago)
- Last Synced: 2025-03-24T15:59:56.580Z (about 1 year ago)
- Topics: error-handler, error-handling, error-log, errors, node-module
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# err-hndlr
> Error handler for either throwing locally or sending a bug ticket to a rest api
[](https://travis-ci.org/jneidel/err-hndlr)
[](https://github.com/jneidel/err-hndlr/blob/master/license)
[](https://www.npmjs.com/package/err-hndlr)
## Install
[](https://www.npmjs.com/package/err-hndlr)
```
$ npm install err-hndlr
```
## Usage
```js
const errHndlr = require( "err-hndlr" );
errHndlr.init(
process.argv[2] === "--debug",
"https://api.jneidel.com/errors/submit",
{
applicationId: "1337"
}
)
errHndlr.throw(
"smt went wrong",
{
env: {...},
},
false
)
// If --debug flag was passed:
//=> Error: something went wrong
// If --debug flag was not passed:
// POST https://api.jneidel.com/error/submit
// {
// applicationId: "1337",
// error: {
// msg: "smt went wrong",
// stack: "...",
// },
// env: {...},
// }
```
## API
### init( isThrow, apiAddress, data, options )
Globally initializes the error handler. This function has to be run before throwing any errors.
```js
errHndlr.init(
false,
http://api.jneidel.com/errors/submit,
{ id: "1337" },
)
```
**isThrow:**
Default: true
Type: boolean
Whenever errors should be thrown locally (`true`) or be sent to the rest api (`false`).
**apiAddress:**
Type: string
Rest api endpoint where error requests should be sent if `isThrow=false`. The data will be sent in the body of the POST request.
**data:**
Default: {}
Type: object
The data that will be sent along on every request. You might want to include device specific data (os, etc.) and application data (name, version, etc.) here.
**options:**
Default: {}
Type: object
Currently supported options are:
**app:**
Require your package.json through the app option to include the app's name and version in every request.
```js
// options:
{
app: require( "../package.json" )
}
// POST request:
{
...
app: {
name: "err-hndlr",
version: "0.0.1",
}
}
```
**os:**
Pass a truthy value to include the `os.type()` and `os.platform()` in every request.
```js
// options:
{
os: true
}
// POST request:
{
...
os: {
type: "Linux",
platform: "linux",
}
}
```
### throw( msg, data, isExit )
```js
errHndlr.throw(
"something went wrong",
data: {
env: {...}
},
false,
).catch( err => {...} ) // To handle connection errors (ECONNREFUSED)
```
**msg:**
Type: string
The message used to call `new Error( msg )`.
**data:**
Default: {}
Type: object
Data to be include on this specific request. Might include environmental variables, state, etc.
**isExit:**
Default: false
Type: boolean
Whenever the process should exit if the error is thrown.
## Related
- [`jneidel/api.jneidel.com`](https://github.com/jneidel/api.jneidel.com) - The api this module was built for. See for a usage example.
## License
MIT © [Jonathan Neidel](https://jneidel.com)