Answer the question
In order to leave comments, you need to log in
What class structure to use for custom exceptions?
Hello! I am writing a common class for requests from API and WEB. I need to make it so that for WEI some error numbers are given, and for API others ... I believe that I need to use some constants that the class must return in case of an error, and already in places where this class is called, match the constants errors and the necessary error number and already give it to the client.
Tell me how to do it right? Just make an exception in the type class:
And already in the file where this class is called, wrap the code in something like this:throw new \FirstClassException("INVALID_TOKEN");
try{
// тут работа с классом, где предполагаются исключения типа FirstClassException
}catch(FirstClassException $e){
switch($e->getMessage()){
case 'INVALID_TOKEN':
$response->setError("Не верный токен", 101);
break;
//.... перечисление вчех вариантов ошибок
}
}
throw new \FirstClassException("INVALID_TOKEN");
Answer the question
In order to leave comments, you need to log in
Do you feel sorry for the exceptions?) make your own for the api, and your own for the web - then there will be no confusion in the logs and code.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question