Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

417
Views
How to handle API code 400 errors by error message in react.js

I am working on a project with react, and I am new to it. I would like to raise the following question. I have a POST API that throws errors on some occasions. I can handle the errors by error code (e.g. 400, 401, 402, 500). But I would like to have more cases of error code 400 e.g.

OnSend = () => {
    MyService.SendPost(this.state.enteredValue) //do the POST request
        .then(() => {
            this.setState({result: 'success'});
        })
        .catch((error) => {
            switch(error.errorCode) {
                case 400:
                    this.setState({result: '400')};
                        break;
                case 500:
                    this.setState({result: '500')};
                    break;
            }
        });
};

What I would like to do is something like the following

OnSend = () => {
    MyService.SendPost(this.state.enteredValue)
        .then(() => {
            this.setState({result: 'success'});
        })
        .catch((error) => {
            switch(error.errorCode) {
                case 400:
                    switch(error.ERRORMESSAGEHERE) { //how should I handle this?
                        case 'error message 1':
                            this.setState({result: '400 with error message 1')};
                            break;
                        case 'error message 2':
                            this.setState({result: '400 with error message 2')};
                            break;
                      }
                      break;
                case 500:
                    this.setState({result: '500')};
                    break;
            }
        });
};

I hope that the above example may help. Is something like that feasible?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!