# A simple but useful error handler

I am trying to make a very simple error handler that would catch and handle all kinds of errors in both development and production environments. The idea is to keep it as simple as possible yet useful in the production environment.

In order to do so I am converting all errors into exceptions. Then I will need just a single function to handle both errors and exceptions.

Then, according to my principles of error handling

• in the dev environment, it should just echo the error information out, and
• in the prod environment, the error information has to be logged and a generalized error message has to be shown on-screen.

It am going to distinguish the modes using display_errors php.ini parameter and also consider all CLI scripts to be in the dev mode too.

set_error_handler(function ($level,$message, $file = '',$line = 0)
{
throw new ErrorException($message, 0,$level, $file,$line);
});

set_exception_handler(function ($e) { if (ini_get('display_errors') || php_sapi_name() === 'cli') { echo$e;
} else {
error_log(\$e);
header('HTTP/1.1 500 Internal Server Error', TRUE, 500);
echo "<h1>500 Internal Server Error</h1>
An internal server error has been occurred.<br>
}
});


There is vast room for improvement, like to create distinct exception classes for each error type (such as DeprecatedErrorException, NoticeErrorException etc.) or, based on the severity, to decide whether the script execution has to be terminated or not. Or a more creative/user-friendly error message. But I am not sure if I should include all that in a simple demonstration example. What are essential additions or modifications could be made to this code?

Perhaps a more "friendly" message would be nice and/or a way to customize it. As an example the generic catcher we're using right now reads:

echo '<div style="width: 700px;">
<h3>Sorry but we ran into an unexpected problem.</h3>
The problem has been logged and support staff have been notified.
</div>';


"been occurred" is grammatically incorrect, and I don't think this line makes any sense:

if (ini_get('display_errors') || php_sapi_name() === 'cli')


php_sapi_name() will always return either a 1 or a 0, depending on what method the server is using to access the php script[internally]. It has nothing to do with client side whether you are in development or production..

ini_get() will return a boolean value or an empty string, as stated in the php_sapi_name manual:

A boolean ini value of off will be returned as an empty string or "0" while a boolean ini value of on will be returned as "1".

So your if statement is basically saying "if 0, 1 or an empty string, run the code below," which is useless. That has no logical use to error reporting.

What is the purpose of checking what type of interface the server is using to access PHP in this scenario?

• We're Code Review, not post review - if you can improve the spelling of the non-code part of the post, you're encouraged to propose an edit. It's a shame to spoil this otherwise good answer with rants such as (ironically) "*If your[sic] gonna keep spamming my post's[sic] atleast[sic] learn to spell in your own." – Toby Speight Mar 9 '20 at 16:16
• Someones already done it, I was just pointing out the fact that he keeps stalking all my questions & answers & leaving negative comments, not usefull critism, when he has only two questions both with errors & not very much code to actually review. – Ryan Stone Mar 9 '20 at 20:03
• If you have a problem with a user, please leave a moderator flag on any post of that user, or take it to meta.CR. Regardless of the user's actions, this is not the way to handle it. – Mast Mar 9 '20 at 21:29