THpubs
THpubs

Reputation: 8172

How to return the error from a promise to a redux saga catch block? Now get the error undefined

I'm calling a firebase function in my saga. The function works properly and if an error returns, it comes to the saga's catch block. But the error variable is not passed. Here's the code:

const firebaseAuth = ({ email, password }) =>
  firebase.auth().signInWithEmailAndPassword(email, password)
    .catch((error) => { throw error; });


function* loginUser(action) {
  try {
    const user = yield call(firebaseAuth, action.payload);

    yield put({ type: USER_LOGIN_SUCCESS, payload: { user } });
  } catch (error) {
    /* eslint-disable no-debugger */
    debugger;

    // I need to see the error here.

    yield put({ type: USER_LOGIN_FAIL });
  }
}

function* sessionSaga() {
  yield takeEvery(USER_LOGGING_IN, loginUser);
}

export default sessionSaga;

Currently, when the program breaks at the debugger statement, the error is undefined. Any idea how to fix this?

Upvotes: 0

Views: 594

Answers (1)

RamAlx
RamAlx

Reputation: 7344

Ι think you should add your function that calling the Promise inside your try-catch block:

function* loginUser(action) {
  try {
      const firebaseAuth = ({ email, password }) =>
      firebase.auth().signInWithEmailAndPassword(email, password)
        .catch((error) => { throw error; });

     const user = yield call(firebaseAuth, action.payload);

    yield put({ type: USER_LOGIN_SUCCESS, payload: { user } });
  } catch (error) {
    /* eslint-disable no-debugger */
    debugger;

    // I need to see the error here.

    yield put({ type: USER_LOGIN_FAIL });
  }
}

Upvotes: 2

Related Questions