Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Dec 30, 2016
1 parent 02184f4 commit d78bfd8
Show file tree
Hide file tree
Showing 11 changed files with 511 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .npmignore
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
14 changes: 14 additions & 0 deletions .travis.yml
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
37 changes: 37 additions & 0 deletions README.md
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.

[![npm version](https://img.shields.io/npm/v/mali-onerror.svg?style=flat-square)](https://www.npmjs.com/package/mali-onerror)
[![build status](https://img.shields.io/travis/malijs/onerror/master.svg?style=flat-square)](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
33 changes: 33 additions & 0 deletions index.js
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)
})
}
}
}
48 changes: 48 additions & 0 deletions package.json
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"
}
}
16 changes: 16 additions & 0 deletions readme.hbs
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.

[![npm version](https://img.shields.io/npm/v/mali-onerror.svg?style=flat-square)](https://www.npmjs.com/package/mali-onerror)
[![build status](https://img.shields.io/travis/malijs/onerror/master.svg?style=flat-square)](https://travis-ci.org/malijs/onerror)

## API

{{>all-docs~}}


## License

Apache-2.0
Loading

0 comments on commit d78bfd8

Please sign in to comment.