First of all I must warn you that on a live site that is open to public, emailing errors could flood your mailbox in case of some critical failure when every request to the site causes a distinct email to be sent. It happened not once to the companies I worked for. And sometimes it's hard to find the actual cause in the waterfall of the errors.
Monitoring server logs is considered much safer and accurate solution. Using Grep on the error log proved to me much more efficient than browsing emails. But either way, the question here is how to handle all errors uniformly, while the action taken in the handler could be anything.
So just like it shown in my article on error reporting basics, errors and exceptions are different beasts and each has its own handler. Hence, to have the error processing uniform you have to convert errors to exceptions first, and then do the required processing in the exception handler, whatever it will be - logging, emailing or anything.
As such, you will need two functions featured in the article, the error handler that simply transfers errors into exceptions,
set_error_handler(function ($level, $message, $file = '', $line = 0)
{
throw new ErrorException($message, 0, $level, $file, $line);
});
and the exception handler like this
set_exception_handler(function ($e)
{
error_log($e);
http_response_code(500);
if (ini_get('display_errors')) {
echo $e;
} else {
error_log($e, 1, "[email protected]", "From: [email protected]");
echo "<h1>500 Internal Server Error</h1>
An internal server error has been occurred.<br>
Please try again later.";
}
});
that will send you an email every time an error occurs and the code is run on a live site.
Also, you would probably like to add a fatal error handler which could be instrumental in handling the eponymous errors. The full example of all three handlers combined together can be seen in the article.
Regarding mysqli, you are absolutely right, the following line
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
will tell mysqli to throw exceptions, which will be processed by the handler defined above.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.