cancellation too presumptive?
See original GitHub issueJust some food for thought…
Nevertheless, a warning is logged into the console in case a cancelled task omitted to handle a cancellation exception.
I have quite a few instances where one saga spawns a fetcher function, and that fetcher func then invokes the actual function doing the xhr. This is to keep functions lean and “doing one thing”. Maybe I want to transform the parameters or data before/after fetching, or maybe I want to check status codes and validate returned values versus nulls, etc.
function doFetch(params) {
yield xhr(url, params)
}
function* fetch(params) {
const { statusCode, body } = yield call(doFetch, params)
if (statusCode > 499) {
return null
}
const data = body.someData
// ... do some transformations on the data
return {
transformedData
}
}
function* fetchSomeStuff() {
yield put(START_FETCH)
// ...maybe configure some parameters
const results = yield call(fetch, params)
// ...do some data transforms on results
yield put(FETCH_SUCCESS, results)
}
When I want to sure up this code with some error handling, in order to avoid a console.warn
by redux-saga I have to put in a try/catch for every step along the saga. This is fairly inconvenient to have to handle a thrown saga cancelled exception at every invocation, particularly because I only want to use the catch to “cleanup” in one of the cases:
function doFetch(params) {
let req
try {
return (yield req = xhr(url, params))
} catch (e) {
if (e instanceof SagaCancellationException) {
req.abort()
} else {
throw e
}
}
}
function* fetch(params) {
// doesn't have to handle errors, since there's nothing to do and the
// caller handles anything that might be thrown...
try {
// ... same code as above
} catch (e) {
// there's nothing to do here except stop the console.warn() from showing up
}
}
function* fetchSomeStuff() {
try {
yield put(START_FETCH)
// ...maybe configure some parameters
const results = yield call(fetch, params)
// ...do some data transforms on results
yield put(FETCH_SUCCESS, results)
} catch (e) {
// capture SagaCancellationException, but throw any js errors
if (!e instanceof io.SagaCancellationException) {
console.error(e)
}
}
}
Issue Analytics
- State:
- Created 8 years ago
- Comments:9 (6 by maintainers)
Top GitHub Comments
I’m thinking of making this an opt-in behavior. Provide some config argument, so the developer can choose to disable the console warnings
@aft-luke yes. Included with the new 0.6.0