-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
511 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*.orig | ||
.idea | ||
.DS_Store | ||
node_modules | ||
dump.rdb | ||
npm-debug.log | ||
.nyc_output | ||
test | ||
.travis.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
language: node_js | ||
node_js: | ||
- "6.9" | ||
- "7.0" | ||
- "7.1" | ||
- "7.2" | ||
env: | ||
- CXX=g++-4.8 | ||
addons: | ||
apt: | ||
sources: | ||
- ubuntu-toolchain-r-test | ||
packages: | ||
- g++-4.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# mali-onerror | ||
|
||
Mali on error middleware. Calls function with error and context. | ||
The called function does not have access to control flow. | ||
|
||
[](https://www.npmjs.com/package/mali-onerror) | ||
[](https://travis-ci.org/malijs/onerror) | ||
|
||
## API | ||
|
||
<a name="module_mali-onerror"></a> | ||
|
||
### mali-onerror ⇒ <code>function</code> | ||
Mali on error middleware. Calls function with error and context. The called function does not | ||
have access to control flow. | ||
|
||
**Returns**: <code>function</code> - the middleware function ) | ||
|
||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| fn | <code>function</code> | The function to call when an error occurs. Function has to have signature with signature <code>(err, ctx)</code> | | ||
|
||
**Example** | ||
|
||
```js | ||
const onError = require('mali-onerror') | ||
|
||
function errorLogger (err, ctx) { | ||
console.log('Error on %s: %s', ctx.name, err.toString()) | ||
} | ||
|
||
app.use(onError(errorLogger)) | ||
``` | ||
|
||
## License | ||
|
||
Apache-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const CallType = require('mali-call-types') | ||
const inject = require('error-inject') | ||
|
||
/** | ||
* Mali on error middleware. Calls function with error and context. The called function does not | ||
* have access to control flow. | ||
* @module mali-onerror | ||
* | ||
* @param {Function} fn The function to call when an error occurs. Function has to have signature | ||
* with signature <code>(err, ctx)</code> | ||
* @return {Function} the middleware function ) | ||
* @example | ||
* const onError = require('mali-onerror') | ||
* | ||
* function errorLogger (err, ctx) { | ||
* console.log('Error on %s: %s', ctx.name, err.toString()) | ||
* } | ||
* | ||
* app.use(onError(errorLogger)) | ||
*/ | ||
module.exports = function (fn) { | ||
return function onError (ctx, next) { | ||
if (ctx.type === CallType.RESPONSE_STREAM || ctx.type === CallType.DUPLEX) { | ||
inject(ctx.call, err => fn(err, ctx)) | ||
return next() | ||
} else { | ||
return next().catch(err => { | ||
fn(err, ctx) | ||
return Promise.reject(err) | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"name": "mali-onerror", | ||
"version": "0.0.0", | ||
"description": "On error middleware for Mali", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/malijs/onerror.git" | ||
}, | ||
"author": { | ||
"name": "Bojan D.", | ||
"email": "[email protected]" | ||
}, | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/malijs/onerror/issues" | ||
}, | ||
"homepage": "https://github.com/malijs/onerror", | ||
"keywords": [ | ||
"mali", | ||
"grpc", | ||
"service", | ||
"server", | ||
"microservice" | ||
], | ||
"scripts": { | ||
"test": "ava -v", | ||
"docs": "jsdoc2md index.js --heading-depth 3 --template readme.hbs | md-wrap-code > README.md" | ||
}, | ||
"dependencies": { | ||
"error-inject": "^1.0.0", | ||
"mali-call-types": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
"async": "^2.1.4", | ||
"ava": "^0.17.0", | ||
"babel-eslint": "^7.1.1", | ||
"highland": "^3.0.0-beta.3", | ||
"jsdoc-to-markdown": "^2.0.1", | ||
"lodash": "^4.17.3", | ||
"mali": "^0.0.2", | ||
"md-wrap-code": "^0.1.1", | ||
"standard": "^8.6.0" | ||
}, | ||
"standard": { | ||
"parser": "babel-eslint" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# mali-onerror | ||
|
||
Mali on error middleware. Calls function with error and context. | ||
The called function does not have access to control flow. | ||
|
||
[](https://www.npmjs.com/package/mali-onerror) | ||
[](https://travis-ci.org/malijs/onerror) | ||
|
||
## API | ||
|
||
{{>all-docs~}} | ||
|
||
|
||
## License | ||
|
||
Apache-2.0 |
Oops, something went wrong.