PHP - General User Error Messages

As a 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 to replace this with a more user-friendly message when something goes wrong? But how then write the error to the server somewhere?

So instead of displaying a full error message, I just want the user to know that something went wrong and it will be fixed as soon as possible. If a complete error that has occurred is recorded in the background without showing it to the user.

+4
source share
5 answers

Errors come from two sources:

  • Your script is in a state you did not expect.
  • Something out of control of your script is wrong.

The first kind of errors comes from errors in your program and should be fixed there. Examples are:

  • Method call for non-object (e.g. string or int )
  • Passing arguments of the wrong function type (e.g. passing bool to mysqli_query ).

There is nothing wrong with development systems that display_errors enabled. On production servers, disable display_errors and enable log_errors . This logs errors in the log of your web server. More sophisticated ways to fix errors can be achieved by implementing your own error handler and registering it with set_error_handler .

Errors of the second type are independent of your script, so your script should be prepared to handle them gracefully. Examples are:

  • The database system is not working.
  • The file you are trying to retrieve does not exist.

To handle these errors, read the documentation for each function and method used, determine when the call may fail and act accordingly. Exceptions can help you here.

Non-observance of the second type of errors, as a rule, leads to an error of the first kind. For example, if you do not check if mysqli_connect establishes a connection to the database (the second type of error), the next call to mysqli_query will result in an error of the first kind.

+3
source

Writing to the server can be done by logging errors , but I don’t think there is a reasonable way to translate these errors to user friendly messages. And there should be no way.

There should never be PHP errors in your application. If so, the only thing you could (and should) have done was to present a message to the user stating that something unexpectedly went wrong.

Friendly error messages are messages that you create yourself by eliminating certain exceptions or other forms of error handling.

0
source

Yes, you can use set_error_handler to call a user-defined function and with trigger_error you can get error messages.

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

0
source

Unfortunately, you cannot catch compile-time errors, so any syntax error in your code cannot be resolved using the PHP error handling capabilities.

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

I used it to create more user-friendly error messages, as well as provide the user with the ability to send an error report describing 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'); 
0
source

I would suggest you convert all php errors / warnings / notifications to exceptions and handle the exception like any exception. This is possible in php and makes error handling more elegant than the default:

 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 can wrap your entire index / controller method in 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 great flexibility and the possibility of nesting try / catch, of course.

Greetings

0
source

All Articles