简体   繁体   中英

PHP - Generic user friendly error messages

As the moment, I have the following on my PHP pages:

error_reporting( E_ALL | E_STRICT );
ini_set('display_errors', 1);

Is there a recommended way of replacing this with a more user friendly message when something goes wrong? But as the same time, record the error on the server somewhere?

So instead of displaying an full error message, I just want the user to know that something has gone wrong, and it will be corrected ASAP. Where the full error that occurred gets recorded in the background without showing it to the user.

Errors come from two sources:

  1. Your script is in a sitiuation that you did not anticipate.
  2. Something outside of the control of your script is wrong.

The first kind of errors come from bugs in your program and need to be fixed there. Examples are:

  • Calling a method on a non-object (eg on a string or an int )
  • Passing an arguments of the wrong type to a function (like passing a bool to mysqli_query ).

On development systems, there is nothing wrong with having display_errors turned on. On production servers, turn off display_errors and turn on log_errors instead. This logs errors to the log of your web server. More sophisticated ways to deal with errors can be achived by implementing your own error handler and registering it with set_error_handler .

The second kind of errors is outside the control of your script, so your script must be prepared to deal with them gracefully. Examples are:

  • The database system is down.
  • A file that you try to access does not exist.

To handle these errors, read the documentation for every function and method that you use, determine when a call can fail and act accordingly. Exceptions might help you here.

Not dealing with the second kind of errors appropriately usually leads to an error of the first kind. For example, if you do not check whether mysqli_connect actually establishes a database connection (second kind of error), a following call to mysqli_query will produce an error of the first kind.

Recording on the server can be done by logging the errors , but I don't think there's any sensible way of translating these errors to user friendly messages. And there shouldn't be a way.

Your application should never produce any PHP errors. If it does, the only thing you could (and should) do is present the user with a message that something has unexpectedly gone wrong.

User friendly error messages are messages you create yourself, by throwing certain exceptions or other forms of error handling.

Yes, you can use set_error_handler to call user defined function & by using it with trigger_error you can get custom error messages.

Check Example #1 Error handling with set_error_handler() and trigger_error() on set_error_handler page.

Unfortunately you can not catch compile time errors, so any syntax error in your code can not be caught by PHP error handling capabilities.

You can use register_shutdown_function , set_error_handler and set_exception_handler to customize error behavior.

I have used it to make more user friendly error messages, as well as giving the user the option to send an error report with a description of his actions etc.

Example:

function myExceptionHandler($e){
    //Debug data to display to the user in error.php.
    $_SESSION['error_dump'] = print_r($e,true);
    header('Location: error.php');
}
set_exception_handler('myExceptionHandler');

I'd suggest you transform all php errors/warning/notices into exceptions and handle the exception just like you would any exception. This is possible in php, and makes error handling much more elegant than the default way:

set_error_handler( function( $num, $msg, $file, $line ) {
  # take into account the '@' operators ( or remove this line and ignore them ):
  if ( error_reporting() === 0 ) return false;
  throw new \ErrorException( $msg, $num, 0, $file, $line );
});
// stolen from my blog @ http://codehackit.blogspot.be/2012/01/php-errorswarningsnotices-to-exceptions.html

Then you could wrap your entire index/controller method into a try catch:

try {
  # request handler.. 
} catch ( ErrorException $e ) {
  # tell the user something went wrong and save $e->getMessage() into a custom log file
}

This gives you a lot of flexibility and the possibility of nesting try/catches of course.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM