Exceptions Where i use them
After reading “How to Use Exceptions Correctly” and commenting on it, I decided to write a short article where I use exceptions, and not how to “correctly”, everything is based on experience, briefly and concisely.
In debug mode, I use PHP_Exceptionizer ( discussed here ). Allows you to make the code cleaner, turns any notis and warning into reception, which in turn allows you not to miss them. Naturally, this is enabled only in debug mode.
I also recommend logging all the "misunderstood" executions:
Logics
- Validation of data: in other words, an error of the input data, the format of the field is not the same, the field is not filled in, the error of uniqueness of the field is all the errors of the input data, in such cases I throw an execution listing all the fields and errors that are subsequently displayed to the user.
- 404 The document was not found: If a specific entity was requested (for example, a line from the database) by its identifier (and not only), then if it is not there, an execution is thrown, which later turns into page 404.
- 403 Access is denied: If there was an attempt to access the user where it cannot be - then execution is thrown (you can pass in the arguments why there is no access there), which later turns into 403.
- 401 Authorization required: The user requests data where authorization is needed. We throw out the execution (in the arguments we can pass Relarm - the text of the authorization request), and the user receives either a request for HTTP authorization or a regular form.
- 301 Redirect: Need to redirect? we throw execution - in it we transfer where it is necessary to redirect.
Mistakes
- Fatal errors: Everything is clear here, there is no connection to the database, or the config cannot be read. Just write to the log and show the deface.
- Critical errors: For example: SMSka is not sent, the file cannot be uploaded - we throw a specific execution. and in the right places (for example - confirmation of the phone), we process it and write to the user that we can not send SMS.
Debugging
In debug mode, I use PHP_Exceptionizer ( discussed here ). Allows you to make the code cleaner, turns any notis and warning into reception, which in turn allows you not to miss them. Naturally, this is enabled only in debug mode.
Example
I also recommend logging all the "misunderstood" executions:
class Controller_Front
{
// на уровне приложения
public function execute()
{
try {
...
// где то на уровне контроллера
try {
... // тут вызов основнного тела контроллера
// Захотели мы редиректнуть на хабр.
throw new Controller_RedirectException('http://habr.ru');
...
} catch (Controller_DataException $e) {
// Сохраняем ошибки и показывем их в шаблоне
$this->errors = $e->getErrors();
}
...
} catch (Controller_RedirectException $e) {
// Метод, который посылает заголовки
$this->_processRedirectException($e);
} catch (Controller_AuthException $e) {
// Метод, запрашивающий HTTP авторизацию
$this->_processAuthException($e);
} catch (Exception $e) {
// Записывем эксепшн в лог.
$this->_logException($e);
// Показывем дефейс.
$this->_processFatalException($e);
}
}
}
* This source code was highlighted with Source Code Highlighter.