Answer the question
In order to leave comments, you need to log in
How to correctly use exception in api and is it necessary at all?
Hello colleagues.
In practice, I often meet completely different approaches to error handling, in particular in api services.
Somewhere they throw exceptions, somewhere they return an array like this in jason
if (!Yii::$app->request->isPost) {
return ['result' => 'error', 'message' => 'POST only', 'code' => 405];
}
if (!$guid = Yii::$app->request->post('guid')) {
return ['result' => 'error', 'message' => 'Guid required', 'code' => 400];
}
if (!$reason = Yii::$app->request->post('reason')) {
return ['result' => 'error', 'message' => 'Reason required', 'code' => 400];
}
Answer the question
In order to leave comments, you need to log in
and how are you going to catch the exceptions on the server on the client?
if something went wrong on the server, it must inform the client about it. and how to process this answer, the task of the client.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question