Answer the question
In order to leave comments, you need to log in
API client design. What is the best way to return errors from the API?
Greetings.
There is a question about the design of working with errors in the client of a certain API. How better, more beautiful, easier to work, more true, more fashionable-stylish-youthful, how would you, the developers, like it more?
$result = $apiFacade->getData();
if ($result->isSuccess()) {
echo 'success: ' . $result->getData() . PHP_EOL;
} else {
if (ApiClient::ERROR_NOT_ENOUGHT_UNITS === $result->getError()->getCode()) {
// some actions for this error
} elseif (ApiClient::ERROR_OBJECT_NOT_EXISTS === $result->getError()->getCode()) {
// some actions for this error
} else {
// some actions for other errors
}
}
try {
$result = $apiFacade->getData();
} catch (ApiNotEnoughtUnitsException $exception) {
// $exception->getCode() вернёт код ошибки
// $exception->getMessage() вернёт текст ошибки
// some actions for this error
} catch (ApiObjectNotExistsException $exception) {
// some actions for this error
} catch (ApiException $exception) {
// some actions for other errors
}
echo 'success: ' . $result . PHP_EOL;
Answer the question
In order to leave comments, you need to log in
Exceptions and some global handler.
ps if
to switch
replace)
switch($result->getError()->getCode()) {
case ApiClient::ERROR_NOT_ENOUGHT_UNITS:
//...
break;
case ApiClient::ERROR_OBJECT_NOT_EXISTS:
//...
break;
default:
//...
break;
}
Throwing exceptions is bad, because an insignificant and uninteresting 404 when deleting an object can break the execution stack and give the user a beeeee on the screen. Exceptions are thrown in strongly typed languages because you can't "suddenly" return false instead of the expected type. Go solves this problem by allowing multiple values to be returned.
I faced. Once, too, I puzzled over this question, and did not find a definite answer, because every method has both strengths and weaknesses. Tried different options, including bikes, returning an error code, and even returning associative arrays like {status, result}. Many discouraged throwing exceptions, arguing that the exception should remain an exception and not become business logic (including when the exception's parameters are passed to its constructor when it is thrown, depending on the type of exception). In general, in practice, it turned out that this is quite convenient, it reduces the number of conditions, checks, the code becomes shorter and easier to read, and there are fewer bugs. Almost no effect on speed. In several recent php projects, model error handling was done in a similar way. When implementing rest-api, controllers are simply created for this model. Perhaps someone has a different experience and he will share negative impressions of exceptions, but after choosing this approach, I had no regrets. So, as a developer, this way of error control would be fine.
If the error is unrecoverable by the client and it exits after the occurrence of such an error, then this is an exceptional situation, we work with exceptions, show the user “Try later”, tell the API itself that “I have completed” (if something needs to be reported to this API).
If the client intends to work further (not enough money for the construction? We show the user “There is not enough money”, the API says “ok, not enough, that’s enough”, the client continues to work with the current amount of money), then this is not an exception, but normal work.
Maybe I'm wrong, but in your example it all depends on
$apiFacade->getData()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question