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.

How to get axios error response INTO the redux saga catch method

See original GitHub issue

The code with axios is:

export const createBlaBla = (payload) => {
  return axios.post('/some-url', payload)
    .then(response => response)
    .catch(err => err);
}

And then I’m using this with redux-saga like this:

function* createBlaBlaFlow(action) {
  try {
    const response = yield call(createBlaBla, action.payload);
    if (response) {
      yield put({
        type: CREATE_BLA_BLA_SUCCESS
      });
    }
  } catch (err) {
    // I need the error data here ..
    yield put({
      type: CREATE_BLA_BLA_FAILURE,
      payload: 'failed to create bla-bla'
    });
  }
}

In case of some error on the backend - like invalid data send to the backend - it returns a 400 response with some useful data:

{
  "code":"ERR-1000",
  "message":"Validation failed because ..."
  "method":"POST",
  "errorDetails":"..."
}

But I don’t receive this useful data in the catch statement inside the saga. I can console.log() the data in the axios catch statement, also I can get it inside the try statement in the saga, but it never arrives in the catch.

Probably I need to do something else? … Or the server shouldn’t return 400 response in this case?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
shinimacommented, Jan 15, 2019

You are catching errors in your createBlaBla, so the error does not bubble to the generator. Just replace you createBlaBla as follows:

export const createBlaBla = (payload) => {
  return axios.post('/some-url', payload)
}
2reactions
BenNeighbourcommented, Feb 17, 2021

@rajat-np I am trying that and it isn’t working for me? Could I have some clarity on this please?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get axios error response INTO the ... - Stack Overflow
So, I came up with two solutions of this problem. === First one - very dump workaround, but actually it can be handy...
Read more >
How to handle axios error in redux-saga in try catch - Martin l.k
The problem. My original code is as following: I have a fetch function in my /services/Api.js: import axios from axios ...
Read more >
Error Handling | Redux-Saga
We can catch errors inside the Saga using the familiar try/catch syntax. import Api from './path/to/api'
Read more >
Get axios Response object when error occurred with Redux ...
So, inside your error object, there is a response object and then you can access everything inside the data object. try { const...
Read more >
Data fetching with Redux and Axios - LogRocket Blog
payload; const dataOrParams = ["GET", "DELETE"].includes(method) ? "params" ...
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