PHP is famous for displaying ugly error messages, though they are useful at times. I know I can hide error messages with
error_reporting(0);
That's fine, but it leaves the user completely in the dark as to what is going on. The page has stopped working and they don't know why. How can I display a simple message along the lines of "Sorry, there is an error. Please send an e-mail to the webmaster"?
It would be the same message for all errors, and I'm thinking of maybe popping up a javascript alert, but open to any other ideas.
Implement an error and exception handler
You need to write a custom error handler like this. As you can see at the bottom, I am introducing a FATAL error. Here PHP does not spit any ugly error messages as you have quoted. It would just print Some Error Occured. Please Try Later.
<?php
set_error_handler( "log_error" );
set_exception_handler( "log_exception" );
function log_error( $num, $str, $file, $line, $context = null )
{
log_exception( new ErrorException( $str, 0, $num, $file, $line ) );
}
function log_exception( Exception $e )
{
http_response_code(500);
log_error($e);
echo "Some Error Occured. Please Try Later.";
exit();
}
error_reporting(E_ALL);
require_once("texsss.php");// I am doing a FATAL Error here
There are some rules that should apply to production servers:
Never show them the original PHP error message! Set display_errors = off.
Log those errors. Set log_errors = on and define a valid log target in error_log.
Monitor the error log, and act upon it. :)
The handling of errors to the user side has been sufficiently answered by the others.
You can write custom error handler that does it
http://php.net/manual/en/function.set-error-handler.php
This is typically done via Exceptions. If something goes awry, you'd throw an Exception and then "handle it" with an exception handler.
function exception_handler($exception) {
echo "Oops! Something went wrong! We're looking into it!";
}
set_exception_handler('exception_handler');
throw new Exception('Uncaught Exception');
You can also catch a number of fatal errors by using register_shutdown_function.
set_error_handler might also tickle your fancy.
User custom error handling set_error_handler
and exception handler set_exception_handler
and there you can do what ever you like.
Related
I have this code here to create error log:
error_log(date("[Y-m-d H:i:s]:")." You messed up!", 3, "../my-errors.log");
Here, we can see the custom error 'You messed up!' that I have set to print in the error log. I don't want to use the custom error here. Instead of this I want to set the Errors/Warnings/Notices that are generated by PHP itself.
Is this possible and how can we do that?
Thankyou
if I understood it well, you're looking for error_get_last():
array error_get_last ( void )
error_get_last — Get the last occurred error. / Gets information about the last error that occurred.
Take a look:
$last_error = error_get_last();
$formated_last_error = sprintf('%s in %s on line %d'.PHP_EOL,
$last_error['message'], $last_error['file'], $last_error['line']);
error_log(date(DATE_ATOM) . $formated_last_error().PHP_EOL, 3, '/tmp/logs.log');
However, you should take a look at set_error_handler() function which is a general approach.
PHP is famous for displaying ugly error messages, though they are useful at times. I know I can hide error messages with
error_reporting(0);
That's fine, but it leaves the user completely in the dark as to what is going on. The page has stopped working and they don't know why. How can I display a simple message along the lines of "Sorry, there is an error. Please send an e-mail to the webmaster"?
It would be the same message for all errors, and I'm thinking of maybe popping up a javascript alert, but open to any other ideas.
Implement an error and exception handler
You need to write a custom error handler like this. As you can see at the bottom, I am introducing a FATAL error. Here PHP does not spit any ugly error messages as you have quoted. It would just print Some Error Occured. Please Try Later.
<?php
set_error_handler( "log_error" );
set_exception_handler( "log_exception" );
function log_error( $num, $str, $file, $line, $context = null )
{
log_exception( new ErrorException( $str, 0, $num, $file, $line ) );
}
function log_exception( Exception $e )
{
http_response_code(500);
log_error($e);
echo "Some Error Occured. Please Try Later.";
exit();
}
error_reporting(E_ALL);
require_once("texsss.php");// I am doing a FATAL Error here
There are some rules that should apply to production servers:
Never show them the original PHP error message! Set display_errors = off.
Log those errors. Set log_errors = on and define a valid log target in error_log.
Monitor the error log, and act upon it. :)
The handling of errors to the user side has been sufficiently answered by the others.
You can write custom error handler that does it
http://php.net/manual/en/function.set-error-handler.php
This is typically done via Exceptions. If something goes awry, you'd throw an Exception and then "handle it" with an exception handler.
function exception_handler($exception) {
echo "Oops! Something went wrong! We're looking into it!";
}
set_exception_handler('exception_handler');
throw new Exception('Uncaught Exception');
You can also catch a number of fatal errors by using register_shutdown_function.
set_error_handler might also tickle your fancy.
User custom error handling set_error_handler
and exception handler set_exception_handler
and there you can do what ever you like.
I'm looking for a general way to handle session_start errors, not a way to handle one specific error. There are numerous errors which can happen, such as the session directory being full, which cause a fatal error. I want a way to trap those errors and handle them cleanly, without having to write custom handlers for each possibility.
Something like this (but not this as it doesn't work):
try{
session_start();
}catch(Exception $e){
echo $e->getMessage();
}
All help appreciated, thanks in advance.
The regular PHP session functions don't throw exceptions but trigger errors. Try writing an error handler function and setting the error handler before calling session_start.
function session_error_handling_function($code, $msg, $file, $line) {
// your handling code here
}
set_error_handler('session_error_handling_function');
session_start();
restore_error_handler();
However, this is just for capturing the session errors. A better way would be to create a general error handler that creates exceptions from errors and surround code parts that may throw errors with try ... catch blocks.
i call an php pgm per cronjob at different times.
the pgm includes many php-files.
each file sends or gets data from partners.
How can i handle errors in one includes pgm.
at the time, one ftp-connection in an included pgm fails so the complete script crushes.
how can i handle this ?
You should wrap code, which is possible to crash, into try/catch construction. This will throw exeption, but the script will continue to work. More here.
Need to know more about you code inorder to give you definite answer.
In general php errors isn't catchable unless you define your own error handler from which you throw exceptions your self. Using the code below makes most runtime errors catchable (as long as they arent considered fatal)
error_reporing(E_ALL);
set_error_handler(function($errno, $errstr, $errfile, $errline) {
if($errno == E_STRICT || $errno == E_DEPRECATED) {
return true;
}
throw new RuntimeException('Triggered error (code '.$errno.') with message "'.$errstr.'"');
});
Btw, You could also define your own exception handler to display triggered errors with a full stack trace when an exception isn't catched.
Notice! I would not suggest that you add this code to a production website without rigorous testing first, making sure everything still works as expected.
Edit:
I have no idea what your code looks like, but I guess you can do something like:
require 'error-handler.php'; // where you have your error handler (the code seen above)
$files_to_include = array(
'some-file.php',
'some-other-file.php',
...
);
foreach($files_to_include as $file) {
try {
include $file;
}
catch(Exception $e) {
echo "$file failed\nMessage: ".$e->getMessage()."\nTrace:\n".$e->getTraceAsString();
}
}
I use a custom error handler with complete error reporting in PHP for tracking errors. This works great for debugging and logs all my errors in a database to investigate later.
Anyway, this method now disables the usage of # to ignore an error when one occurs. I now have the issue where I try to rename a directory on my system, because it may occasionally throw an error (if files are being accessed within it).
I would like to be able to catch this error in my code, to prevent executing the rest of the function, but I also do not want this error to appear in my error logging database (considering this error is 'managed' within the code, there is no need to see that it failed).
Is there a simple solution to this? I try using try / catch but it still appears to throw the error.
You can convert all errors/warnings/notices to exceptions
function exceptions_error_handler($severity, $message, $filename, $lineno) {
if (error_reporting() == 0) {
return;
}
if (error_reporting() & $severity) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
}
set_error_handler('exceptions_error_handler');
I think it is better to handle exceptions, than php native errors.
#zerkms' solution would work fine, but my error handler is already completed so to extend this to give me the functionality, I have simply included:
if ( error_reporting() == 0 )
return;
at the start of my handler. This way, if the # is used on a function, the error is still thrown, but ignored at the start of the handler (hence, not logged into the database, etc) and I will still get the boolean result from a function, such as rename().
This also still enabled me to use a try/catch solution on code if need be.