A
A
Alexey Konovalov2018-06-11 18:24:30
PHP
Alexey Konovalov, 2018-06-11 18:24:30

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;
      //.... перечисление вчех вариантов ошибок
   }
}

It seems to me that the logic is correct, but I don't like this line:
throw new \FirstClassException("INVALID_TOKEN");
Probably, it should be somehow prettier.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2018-06-11
@SilenceOfWinter

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 question

Ask a Question

731 491 924 answers to any question