How to customize PHP errors?

Asked

Viewed 168 times

-2

I’m having the following doubt, imagine I’m making the requisition of a file JSON external via a script PHP

<?php
    $json_file = file_get_contents("http://exemplo.com/arquivojson");
?>

And if that file, or this site is off the air, then it’s returned to me something like:

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/passagem/public_html/teste-ok.php on line 2

Warning: file_get_contents(http://exemplo.com/arquivojson): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/passagem/public_html/teste-ok.php on line 2

It is possible to customize this error while unable to order this file?

that instead of displaying this error, just display a warning, something like:

Não foi possível localizar a informação desejada.

I searched and I couldn’t find enough information.

1 answer

2


If these errors appear in your production server is because it is already wrong, now if this is in development then there is no reason to "customize", these errors are to help the development, to give details, so that the errors are fixed previously.

Now back to production, simply hide in php.ini (never hang up, there are certain responses that will recommend to shut down, but this affects the logs), then seven:

display_errors=off

If your hosting has php_mod for Apache then you can do this more easily by adding it inside the .htaccess:

php_flag display_errors off

Note that if a error 500 is because your hosting doesn’t have mod_php5 or mod_php7

The display_errors will only hide on the web page, so when using the file_get_contents just use:

$json_file = file_get_contents("http://exemplo.com/arquivojson");

if ($json_file) {
   ... corre o script normal ...
} else {
   echo '<p>Não foi possível localizar a informação desejada.</p>';
}

set_error_handler/set_exception_handler e register_shutdown_function

This part of the answer is a "precaution", if there are other answers speaking that can customize all errors, yes you can, but it does not mean that it should, because the errors are as I said at the beginning, nor should exist, if there is an error in your project soon it should be corrected, as early as possible in the development environment, if by chance you get an error in production use the logs, you can use the PHP itself, setting the:

error_log="/pasta/aonde/quero/salvar/log/erros.log"

You can also use the if/Else function error_log() to send you an email: https://www.php.net/manual/en/function.error-log.php or save to a file

You can even intercept errors with set_error_handler, set_exception_handler and register_shutdown_function (the latter together with error_get_last() for fatal errors), but only to obtain and save them where desired

  • I’m actually using an external JSON API, in which if it becomes unavailable or the server temporarily goes offline, it will display this warning, rather than displaying an error, because in fact it would not be an "error" and would be temporarily unavailable.

  • 1

    @Alexandrelopes and the display_errors=off along with the if/else solves the problem exactly of your need for eventually unavailable Apis. PS: I edited the answer just in case appear certain new answers absurd that can give you bad suggestions and teach you the worst way (believe me, there is a lot of this on the site :/).

  • I imagine William, I will do some tests. Very interesting the answer.

  • It would be possible to do this without using display_errors more dynamically ?

  • @Alexandrelopes do this only in hosting, don’t do it in development, don’t have to be "dynamic", hosting=hidden errors, your computer=visible errors ... Now if the intention is to ingnorar warnings that are not errors, add this at the top of your main script: error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT & ~E_DEPRECATED);, but only in the script under production, this goes within your php of course. You can create a configuration file to tell when it is or is not production...

  • 1

    @Alexandrelopes ... create a file with only the name production and add in your script something like: <?php if (is_file('production')) { error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT & ~E_DEPRECATED); }, on your computer the file should not exist. And reinforcing, all errors and warnings should be treated in development, only eventual and "normal" errors is that you should probably hide, to avoid instabilities.

  • Sorry William, I did not understand very well. It is possible to create a echo to display a message if an error occurs?

  • 1

    @Alexandrelopes yes it is possible, do exactly what this in answer if and Else

  • I tried to add error_reporting(0); within the if and Else to hide the errors, but it did not work. How to implement?

  • I tried so: if ($json_file) {&#xA; &#xA;} else {&#xA; error_reporting(0); echo '<p>Não foi possível localizar a informação desejada.</p>';&#xA;}

  • If he could requisition, fine. Otherwise he adds error_reporting(0); which hides errors, and writes <p>Não foi possível localizar a informação desejada.</p>

  • @Alexandrelopes as so "didn’t work"? Explains better

  • 1

    @Alexandrelopes who said it was to put error_reporting(0); puts error_reporting at the beginning of the script, first of all.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.