1

I have an app with Rails as a backend and React as a frontend. I am trying to handle Stripe errors on server side and render any error by updating my state in the client.

here the code in my controller method:

def create
  # here some code to handle payment...
   rescue Stripe::CardError => e
     render json: e.json_body[:error][:message], status: 422
  end

and my react function that handles the response:

  axios.post("/orders"})
  .then(res => {
      this.setState({
        hasOrdered: true,
        orderSuccess: true,
        loading: false
      });
  })
  .catch(err => {
    console.log(err);
    this.setState({
      loading: false,
      error: err
    });
  });

the console.log(err) output is simply: Error: Request failed with status code 422 whereas the response I get through the network is:

network tab outbut in the browser

And what I want is the update the state with "Your card has insufficient funds."

How can I do this ?

1 Answer 1

2

OK got the solution: I have to access err.response.data :

 .catch(err => {
    console.log(err.response.data);
    this.setState({
      loading: false,
      error: err.response.data
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.