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.
– Alexandre Lopes
@Alexandrelopes and the
display_errors=off
along with theif/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 :/).– Guilherme Nascimento
I imagine William, I will do some tests. Very interesting the answer.
– Alexandre Lopes
It would be possible to do this without using
display_errors
more dynamically ?– Alexandre Lopes
@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...– Guilherme Nascimento
@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.– Guilherme Nascimento
Sorry William, I did not understand very well. It is possible to create a
echo
to display a message if an error occurs?– Alexandre Lopes
Let’s go continue this discussion in chat.
– Guilherme Nascimento
@Alexandrelopes yes it is possible, do exactly what this in answer if and Else
– Guilherme Nascimento
I tried to add
error_reporting(0);
within the if and Else to hide the errors, but it did not work. How to implement?– Alexandre Lopes
I tried so:
if ($json_file) {
 
} else {
 error_reporting(0); echo '<p>Não foi possível localizar a informação desejada.</p>';
}
– Alexandre Lopes
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>
– Alexandre Lopes
@Alexandrelopes as so "didn’t work"? Explains better
– Guilherme Nascimento
@Alexandrelopes who said it was to put error_reporting(0); puts error_reporting at the beginning of the script, first of all.
– Guilherme Nascimento