PHP: Errors and Error Management

Errors are an integral part of every computer language—although one that, most of the time, programmers would rather not have to deal with!

PHP has some excellent facilities for dealing with errors that provide an excellent level of fine-grained control over how errors are thrown, handled and reported. Proper error management is essential to writing applications that are both stable and capable of detecting when the inevitable problem arises, thus handling failure in a graceful manner.

Types of Errors
There are several types of errors—usually referred to as error levels in PHP:

Compile-time errors
Errors detected by the parser while it is compiling a script. Cannot be trapped from within the script itself.

Fatal errors
Errors that halt the execution of a script. Cannot be trapped.

Recoverable errors
Errors that represent significant failures, but can still be handled in a safe way.

Warnings
Recoverable errors that indicate a run-time fault. Do not halt the execution of the script.

Notices
Indicate that an error condition occurred, but is not necessarily significant. Do not halt the execution of the script.

As you can see, it is not always possible for a script to detect a fault and recover from it. With the exception of parsing errors and fatal errors, however, your script can atleast be advised that a fault has occurred, thus giving you the possibility to handle failure gracefully.

Error Reporting

By default, PHP reports any errors it encounters to the script’s output. Unless you happen to be in a debugging environment, this is rarely a feature that you will want to take advantage of:allowing users to see the errors that your scripts encounter is not just bad form—it could be a significant security issue.

Luckily, several configuration directives in the php.ini file allow you to fine-tune how—and which—errors are reported. The most important ones are error_reporting, display_errors and log_errors.

The error_reporting directive determines which errors are reported by PHP. A series of built-in constants allow you to prevent PHP from reporting errors beneath a certain pre-defined level. For example, the following allows for the reporting of all errors, except notices:

error_reporting=E_ALL & ~E_NOTICE

Handling Errors

Your scripts should always be able to recover from a trappable error—even if it’s just to advise the user that an error occurred and notify support staff of the same fact. This way, your script won’t simply capitulate when something unexpected occurs—resulting in better communication with your customers and the possible avoidance of some major problems.
Luckily, error handling is very easy. Your scripts can declare a catch-all function that is called by PHP when an error condition occurs by calling the set_error_handler() function:

$oldErrorHandler = ’’;
function myErrorHandler ($errNo, $errStr, $errFile, $errLine, $errContext) {
logToFile("Error $errStr in $errFile at line $errLine");

// Call the old error handler

if ($oldErrorHandler) {
$oldErrorHandler ($errNo, $errStr, $errFile, $errLine, $errContext);
}
}
$oldErrorHandler = set_error_handler ($oldErrorHandler);

As you can see, the function name of the old error handler (if any) is returned by the call to set_error_handler()—this allows you to stack several error handlers on top of each other, thus making it possible to have different functions handle different kinds of errors.
It’s important to keep in mind that your error handler will completely bypass PHP’s error mechanism—meaning that you will be responsible for handling all errors, and stopping the script’s execution if necessary.






Share this

0 Comment to " PHP: Errors and Error Management "

Post a Comment