Skip to content

Instantly share code, notes, and snippets.

@pelid
Created February 19, 2017 10:28
Show Gist options
  • Save pelid/ac3a68b2ceb3095638d28b63a98e81b3 to your computer and use it in GitHub Desktop.
Save pelid/ac3a68b2ceb3095638d28b63a98e81b3 to your computer and use it in GitHub Desktop.
saga-middleware-example
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}) // универсальная заглушка на все случаи жизни
}
}
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
}
}
// ....
// внутри takeLatest мы уже знаем какую pageSaga нужно запустить
let wrappedPageSaga = showPlugIfFailure(
redirectsHandler.handler(
handler404.handler(
handleAPIAuthErrors.handler(pageSaga)
)
)
)
yield* wrappedPageSaga
// ...
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