Skip to content

Commit deb8639

Browse files
author
Sergio Daniel Xalambrí
committed
v1.0.0
1 parent 86b4795 commit deb8639

File tree

8 files changed

+133
-1
lines changed

8 files changed

+133
-1
lines changed

.babelrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"plugins": [
3+
"transform-es2015-modules-commonjs",
4+
"transform-es2015-arrow-functions"
5+
]
6+
}

.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "airbnb",
3+
}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ node_modules
3131

3232
# Optional REPL history
3333
.node_repl_history
34+
35+
build

.npmignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
lib
3+
test
4+
logs
5+
*.log
6+
.babelrc

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
# redux-catch
2-
Error catcher middleware for Redux reducers and middlewares
2+
Error catcher middleware for Redux reducers and middlewares.
3+
4+
## API
5+
### Apply middleware
6+
```javascript
7+
import { createStore, applyMiddleware } from 'redux';
8+
9+
import reduxCatch from 'redux-catch';
10+
11+
import reducer from './reducer';
12+
13+
const store = createStore(reducer, applyMiddleware(
14+
reduxCatch(error => console.error(error))
15+
));
16+
```
17+
- `reduxCatch` receive a function to use when an error happen.
18+
- The error handler function could be just a `console.error` like the example above or a function to log the error in some kind of error tracking platform.
19+
- You should use this middleware as the first middleware in the chain, so its allowed to catch all the possible errors in the application.

lib/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const catchMiddleware = errorHandler => () => next => action => {
2+
try {
3+
return next(action);
4+
} catch (error) {
5+
errorHandler(error);
6+
return error;
7+
}
8+
};
9+
10+
export default catchMiddleware;

package.json

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "redux-catch",
3+
"version": "1.0.0",
4+
"description": "Error catcher middleware for Redux reducers and middlewares",
5+
"main": "build/index.js",
6+
"scripts": {
7+
"lint": "eslint lib/index.js",
8+
"prebuild": "npm run lint",
9+
"build": "babel lib --out-dir build",
10+
"pretest": "npm run build",
11+
"test": "babel-node test/index.js | tap-spec",
12+
"prepublish": "npm run test"
13+
},
14+
"repository": {
15+
"type": "git",
16+
"url": "git+https://github.com/sergiodxa/redux-catch.git"
17+
},
18+
"keywords": [
19+
"redux",
20+
"error",
21+
"catch",
22+
"middleware"
23+
],
24+
"author": "Sergio Daniel Xalambrí <[email protected]> (http://sergio.xalambri.com.ar/)",
25+
"license": "MIT",
26+
"bugs": {
27+
"url": "https://github.com/sergiodxa/redux-catch/issues"
28+
},
29+
"homepage": "http://sergio.xalambri.com.ar/redux-catch",
30+
"devDependencies": {
31+
"babel": "6.5.2",
32+
"babel-cli": "6.7.5",
33+
"babel-core": "6.7.6",
34+
"babel-eslint": "6.0.2",
35+
"babel-plugin-transform-es2015-arrow-functions": "6.5.2",
36+
"babel-plugin-transform-es2015-modules-commonjs": "6.7.4",
37+
"eslint": "2.7.0",
38+
"eslint-config-airbnb": "7.0.0",
39+
"eslint-plugin-jsx-a11y": "0.6.2",
40+
"eslint-plugin-react": "4.3.0",
41+
"tap-spec": "4.1.1",
42+
"tape": "4.5.1"
43+
}
44+
}

test/index.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import test from 'tape';
2+
3+
import middleware from '../build/index.js';
4+
5+
const mockedMiddlewareAPI = {};
6+
const baseError = new Error('There was an error.');
7+
8+
test('Catch middleware - error case', t => {
9+
const mockedNext = () => {
10+
throw baseError;
11+
};
12+
13+
t.plan(2);
14+
15+
function errorHandler(error) {
16+
t.ok(
17+
error.message === baseError.message,
18+
'it should receive the expected error message in the `errorHandler`'
19+
);
20+
}
21+
22+
const error = middleware(errorHandler)(mockedMiddlewareAPI)(mockedNext)();
23+
24+
t.ok(
25+
error.message === baseError.message,
26+
'it should return the expected error message'
27+
);
28+
});
29+
30+
test('Catch middleware - success case', t => {
31+
const mockedNext = action => action;
32+
33+
t.plan(1);
34+
35+
const action = middleware(error => error)(mockedMiddlewareAPI)(mockedNext)({
36+
type: 'TEST_ACTION',
37+
});
38+
39+
t.equal(
40+
action.type,
41+
'TEST_ACTION',
42+
'it should be the passed action'
43+
);
44+
});

0 commit comments

Comments
 (0)