4

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.

6
  • Basically you don't show errors to the client, but if you insist you may try the following Commented Apr 16, 2013 at 9:38
  • I guess you'll have to use try catch blocks for that Commented Apr 16, 2013 at 9:39
  • "user friendly" these messages are not for users - they are for you. Commented Apr 16, 2013 at 9:39
  • @HamZaDzCyberDeV, so if something goes wrong, the user shouldn't get a message saying "Something's gone wrong, please try later"? Commented Apr 16, 2013 at 9:52
  • @oshirowanen Yes you show them something like that, but you won't show them Warning: include(./file.php): failed to open stream: No such file or directory in \path\index.php on line 22 Commented Apr 16, 2013 at 9:55

5 Answers 5

2

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 (e.g. 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

1 Comment

Sounds as if I didn't plain myself properly. That is basically all I want to display, i.e. a message simply saying that something has gone wrong unexpectedly. Is there a recommended way of doing that?
0

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

1 Comment

Using this trick, if you make sure to first execute the set_error_handler in it's own file and then only include the next step ( for example in a bootstrap.php / init.php file ) you can even catch fatal syntax errors as exceptions ;) Yes, this will transform ALL php errors/notices/warnings into exceptions. Even the fatals.
0

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.