question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Right pattern for reporting error to user

See original GitHub issue

With Redux Saga, what’s the recommended pattern to report error while executing an operation to user? With redux-thunk we can return a promise while dispatching an operation and allow user to essentially observe that promise. Since all the actions are plain objects in saga world, this isn’t possible. And it’s not always convenient to report error via state change (think react native alerts)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tonyxiaocommented, Jun 6, 2016

In redux-thunk you can do

function fetchRequest(id) {
  return dispatch => {
    dispatch({ type: FETCH_OBJECTS_REQUEST });
    return callAPI(id).then(data => {
      dispatch({ type: FETCH_OBJECTS_SUCCESS, data });
      return data;
    }).catch(error => {
      dispatch({ type: FETCH_OBJECTS_FAIL, error });
      throw error;
    });
  };
}

And in the calling function

this.props.dispach(fetchRequest()).then((data) => {
  // Do something upon success
}, (err) => {
  // Do something upon failure
})

Strictly speaking this isn’t going through the store, but sometimes it’s really convenient to be able to do stuff like this when interacting with other APIs that are imperative rather than declarative.

1reaction
kuycommented, Jun 6, 2016

I think that there is no difference between redux-saga and redux-thunk on the ability for reporting the progress to user. Because of Redux is single state, we need to put the operation progress to the store. And the state is changed only by Actions. So you can dispatch Actions based on the progress of operations.

Note: callAPI(...) returns Promise object

redux-thunk (action creator)

function fetchRequest(id) {
  return dispatch => {
    dispatch({ type: FETCH_OBJECTS_REQUEST });
    callAPI(id).then(data => {
      dispatch({ type: FETCH_OBJECTS_SUCCESS, data });
    }).catch(error => {
      dispatch({ type: FETCH_OBJECTS_FAIL, error });
    });
  };
}

redux-saga (without try-catch)

function* runRequest({ payload }) {
  const { id } = payload;
  const { data, error } = yield call(callAPI, id);
  if (data && !error) {
    yield put({ type: FETCH_OBJECTS_SUCCESS, data });
  } else {
    yield put({ type: FETCH_OBJECTS_FAIL, error });
  }
}

function* handleRequest() {
  yield* takeLatest(FETCH_OBJECTS_REQUEST, runRequest);
}

redux-saga (with try-catch)

function* runRequest({ payload }) {
  const { id } = payload;
  try {
    const data = yield call(callAPI, id);
    yield put({ type: FETCH_OBJECTS_SUCCESS, data });
  } catch (error) {
    yield put({ type: FETCH_OBJECTS_FAIL, error });
  }
}

function* handleRequest() {
  yield* takeLatest(FETCH_OBJECTS_REQUEST, runRequest);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Report Errors in Forms: 10 Design Guidelines
How to Report Errors in Forms: 10 Design Guidelines · 1. Aim for Inline Validation Whenever Possible · 2. Indicate Successful Entry for...
Read more >
Patterns for Generation, Handling and Management of Errors
This paper sets out a collection (or possibly a language) of patterns that relate to the use of error generating, handling and logging...
Read more >
Error Messages: Examples, Best Practices & Common Mistakes
Useful error messages can keep users on your site and increase conversions. See examples and learn the best practices.
Read more >
Errors | Content patterns | Intuit Content Design System
Errors and error messages continue to be a content design and strategy challenge. Here's a plan to respond clearly and fairly when the...
Read more >
How to design and convey error messages properly - Bootcamp
1. Providing Clarity · 2. Concise, but precise · 3. Don't blame users · 4. Give users a solution · 5. Proper Placement...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found