Skip to content

Instantly share code, notes, and snippets.

@jktravis
Last active February 14, 2017 01:18
Show Gist options
  • Save jktravis/54510718a7dea30bd07748d087a19422 to your computer and use it in GitHub Desktop.
Save jktravis/54510718a7dea30bd07748d087a19422 to your computer and use it in GitHub Desktop.
A simple error handler to be used with the auto-dispatched failTypes in redux-logic.
function handleError (type, error) {
console.error(error);
let payload;
if (typeof error === 'string') {
payload = error;
} else if (error.message) {
payload = error.message;
}
return {type, payload, error: true };
}
export function createErrorHandler(type) {
return function (error) {
return handleError(type, error);
}
}
// Demonstrates the usage of the former.
import { createLogic } from 'redux-logic';
import { createErrorHandler } from './createErrorHandler';
const testErrorHandler = createErrorHandler('TESTING_FAILURE');
const testingLogic = createLogic({
type: 'TESTING',
latest: true,
processOptions: {
successType: 'TESTING_SUCCESS',
failType: testErrorHandler
},
process({action}) {
return new Promise((resolve, reject) => {
if (action.payload === 'fail') {
reject('Failed with reject');
} else if (action.payload === 'pass') {
resolve('passed');
} else {
const foo = action.get('foo'); // There is no `get` method on action
foo.toString();
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment