📜 ⬆️ ⬇️

PHP errors: classification, examples, handling

The article presents another attempt to deal with errors that may occur on your path as a php developer, their possible classification, examples of their occurrence, the effect of errors on the response to the client, as well as instructions on writing your error handler.

The article is divided into four sections:
  1. Error classification.
  2. An example demonstrating different types of errors and its behavior with different settings.
  3. Writing your own error handler.
  4. Useful links.

Error classification


All errors, conditionally, can be categorized according to several criteria.
Fatality:

Ability to catch an error by the function defined in set_error_handler ():

Initiator:

For us, within the framework of this article, the classifications according to the first two criteria are most interesting, as will be discussed later.

Examples of errors


Listing index.php
<?php //     error_reporting(E_ALL | E_STRICT); //     ini_set('display_errors', 'On'); //     require 'errors.php'; 

Listing errors.php
 <?php echo "  . <br>"; /* *   (  set_error_handler()) */ // NONFATAL - E_NOTICE // echo $undefined_var; // NONFATAL - E_WARNING // array_key_exists('key', NULL); // NONFATAL - E_DEPRECATED split('[/.-]', "12/21/2012"); // split() deprecated   php 5.3.0 // NONFATAL - E_STRICT // class c {function f(){}} c::f(); // NONFATAL - E_USER_DEPRECATED // trigger_error("E_USER_DEPRECATED", E_USER_DEPRECATED); // NONFATAL - E_USER_WARNING // trigger_error("E_USER_WARNING", E_USER_WARNING); // NONFATAL - E_USER_NOTICE // trigger_error("E_USER_NOTICE", E_USER_NOTICE); // FATAL,     set_error_handler - E_RECOVERABLE_ERROR // class b {function f(int $a){}} $b = new b; $b->f(NULL); // FATAL,     set_error_handler - E_USER_ERROR // trigger_error("E_USER_ERROR", E_USER_ERROR); /* *  (   set_error_handler()) */ // FATAL - E_ERROR // undefined_function(); // FATAL - E_PARSE // parse_error // FATAL - E_COMPILE_ERROR // $var[]; echo "  . <br>"; 

Note: for full functionality of the script requires PHP version not lower than 5.3.0.

The errors.php file contains expressions that trigger almost all possible errors. The exceptions were: E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_WARNING, generated by the Zend core. In theory, you shouldn't meet them in real work.
The following table lists the behaviors of this script under different conditions (depending on the values ​​of the display_errors and error_reporting directives):
Error groupDirective Values *Server response statusAnswer to customer **
E_PARSE, E_COMPILE_ERROR ***display_errors = off
error_reporting = ANY
500Empty value
display_errors = on
error_reporting = ANY
200Error message
E_USER_ERROR, E_ERROR, E_RECOVERABLE_ERRORdisplay_errors = off
error_reporting = ANY
500Script output before error
display_errors = on
error_reporting = ANY
200Error message and script output before error
Not fatal errorsdisplay_errors = off
error_reporting = ANY
and
display_errors = on
error_reporting = 0
200All script output
display_errors = on
error_reporting = E_ALL | E_STRICT
200Error message and all script output
* ANY value means E_ALL | E_STRICT or 0.
** The answer to the client may differ from the answers on real scripts. For example, the output of any information before the inclusion of the errors.php file will appear in all the cases considered.
*** If in the errors.php file, replace the example for the error E_COMPILE_ERROR with require "missing_file.php"; , then the error will fall into the second group.
')
The above table can be described as follows:
  1. The presence in the script file of an error that causes it to be in an “unusable” state (the inability to correctly process) will yield an empty value or only an error message itself, depending on the value of the display_errors directive.
  2. The script in the file with a fatal error that is not related to the first item will be executed in the normal mode until the error itself.
  3. The presence of a fatal error in the file with display_errors = Off will indicate 500 response status.
  4. Not fatal errors, as one would expect, in the context of the possibility of executing the script as a whole, will not affect the performance.

Own error handler


To write your own error handler, you need to know that:

I will explain the third point: the function registered by us with the help of register_shutdown_function () will be executed anyway - whether the script completed correctly or was interrupted due to a critical (fatal) error. We can unambiguously determine the second option using the information provided by the error_get_last () function, and, if there was an error, execute our own error handler.
Let's demonstrate the above on the modified index.php script:
 <?php /** *   * @param int $errno   * @param string $errstr    * @param string $errfile  ,     * @param int $errline  ,     * @return boolean */ function error_handler($errno, $errstr, $errfile, $errline) { //      (   "@" error_reporting()  0) if (error_reporting() & $errno) { $errors = array( E_ERROR => 'E_ERROR', E_WARNING => 'E_WARNING', E_PARSE => 'E_PARSE', E_NOTICE => 'E_NOTICE', E_CORE_ERROR => 'E_CORE_ERROR', E_CORE_WARNING => 'E_CORE_WARNING', E_COMPILE_ERROR => 'E_COMPILE_ERROR', E_COMPILE_WARNING => 'E_COMPILE_WARNING', E_USER_ERROR => 'E_USER_ERROR', E_USER_WARNING => 'E_USER_WARNING', E_USER_NOTICE => 'E_USER_NOTICE', E_STRICT => 'E_STRICT', E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', E_DEPRECATED => 'E_DEPRECATED', E_USER_DEPRECATED => 'E_USER_DEPRECATED', ); //      echo "<b>{$errors[$errno]}</b>[$errno] $errstr ($errfile  $errline )<br />\n"; } //      PHP return TRUE; } /** *     */ function fatal_error_handler() { //       if ($error = error_get_last() AND $error['type'] & ( E_ERROR | E_PARSE | E_COMPILE_ERROR | E_CORE_ERROR)) { //   (     ) ob_end_clean(); //    error_handler($error['type'], $error['message'], $error['file'], $error['line']); } else { //  ()     ob_end_flush(); } } //     error_reporting(E_ALL | E_STRICT); //     ini_set('display_errors', 'On'); //    (     ) ob_start(); //     set_error_handler("error_handler"); //  ,       (,   ) register_shutdown_function('fatal_error_handler'); require 'errors.php'; 

Do not forget that errors of a mixed type, after the declaration of their own error handler, have become not fatal. Plus, the entire output of the script to the fatal error, along with the standard error message will be reset.

In general, the considered example of an error handler does not deal with processing as such, but only demonstrates the possibility itself. Its further behavior depends on your desires and / or requirements. For example, all cases of accessing the handler can be recorded in the log, and in the case of fatal errors, in addition, notify the resource administrator.

useful links


Source: https://habr.com/ru/post/161483/


All Articles