Last active
February 14, 2017 01:18
-
-
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.
This file contains hidden or 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
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); | |
} | |
} |
This file contains hidden or 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
// 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