Skip to content

Commit

Permalink
- Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fantua committed Oct 30, 2017
0 parents commit fee46f6
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 0 deletions.
61 changes: 61 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

.idea

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Igor Machuzhak

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.
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# redux-async-catch
Async error catch middleware for Redux

[![npm version](https://img.shields.io/npm/v/redux-async-catch.svg?style=flat-square)](https://www.npmjs.com/package/redux-async-catch)

```sh
yarn add redux-async-catch
```

## Installation

```sh
yarn add redux-async-catch redux-thunk
```

Then, update your store configuration:

```js
import { createStore, applyMiddleware } from 'redux';
import asyncCatch from 'redux-async-catch';
import thunk from 'redux-thunk';
import fetchError from './actions/index';
import rootReducer from './reducers/index';

const store = createStore(
rootReducer,
applyMiddleware(
// Note: asyncCatch must be applied before thunk
asyncCatch(fetchError),
thunk
)

);
```

- `asyncCatch` receive a thunk action that will receive catched `error` and `lastAction` as a arguments

## Example

Handle `401` error code in your Application and refresh user token.

```js
// actions:

export const fetchUserData = () => async function (dispatch, getState) {
// You don't need to use try/catch anymore,
// all your errors will be handled in fetchError action
const user = await fetch('/api/me');
};

export const refreshUserToken = () => async function (dispatch, getState) {
// somme logic
};

export const fetchError = (e, lastAction) => function (dispatch, getState) {
switch (e.status) {

// when fetchUserData action was called and you got 401
// (user token has been expired), you can refresh token and
// dispatch lastAction again
case 401: {
dispatch(refreshUserToken());
dispatch(lastAction);
break;
}

default: {
console.error(e);
}
}
};

// also you can create your own catch implementation for some actions:
export const fetchSomeOther = () => async function (dispatch, getState) {
try {
const user = await fetch('/api/some/other');
} catch (e) {
if (e.status === '404') {
// some logic
} else {
throw e;
}
}
};

```

## License

MIT

9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default handler => store => next => async action => {
try {
return await next(action);
} catch (e) {
store.dispatch(handler(e, action));

return e;
}
}
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "redux-async-catch",
"version": "0.1.0",
"description": "Async error catch middleware for Redux",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/fantua/redux-async-catch.git"
},
"keywords": [
"redux",
"middleware",
"async",
"error",
"catch"
],
"homepage": "https://github.com/fantua/redux-async-catch",
"author": "Igor Machuzhak <[email protected]>",
"license": "MIT"
}

0 comments on commit fee46f6

Please sign in to comment.