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?