Created
February 19, 2017 10:28
-
-
Save pelid/ac3a68b2ceb3095638d28b63a98e81b3 to your computer and use it in GitHub Desktop.
saga-middleware-example
This file contains 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
import {Base404Error, Order404} from './exceptions.js' | |
import './order404.tag.html' | |
import './page404.tag.html' | |
export const handler = func => function*(...args){ | |
try { | |
return yield* func(...args) | |
} catch(error) { | |
if (!(error instanceof Base404Error)) | |
throw error | |
if (error instanceof Order404){ | |
riot.mount('#page-container', 'pages-order404', {error}) | |
return | |
} | |
riot.mount('#page-container', 'pages-page404', {error}) // универсальная заглушка на все случаи жизни | |
} | |
} |
This file contains 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
import {go2page} from '../../helpers' // its a legacy code | |
import * as exceptions from './exceptions.js' | |
export const handler = saga => function*(...args){ | |
try { | |
let result = yield* saga(...args) | |
if (result instanceof exceptions.Redirect2URL) | |
throw result | |
return result | |
} catch(error) { | |
if (error instanceof exceptions.Redirect2URL){ | |
let {compoundURL, title, shouldReplace} = error.destination | |
_.defer(()=>{ | |
// TODO window.history.pushState | |
}) | |
} else | |
throw error | |
} | |
} |
This file contains 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
// .... | |
// внутри takeLatest мы уже знаем какую pageSaga нужно запустить | |
let wrappedPageSaga = showPlugIfFailure( | |
redirectsHandler.handler( | |
handler404.handler( | |
handleAPIAuthErrors.handler(pageSaga) | |
) | |
) | |
) | |
yield* wrappedPageSaga | |
// ... |
This file contains 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
import failure_plug_saga from './pages/failure_plug' | |
const showPlugIfFailure = saga => function*(...args){ | |
try { | |
return yield* saga(...args) | |
} catch(exception) { | |
Raven.captureException(exception) | |
return yield* failure_plug_saga(...args, exception) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment