Jump to content

Error handler doesn't work the same on dev / prod environments


sKunKbad

Recommended Posts

I've made a custom error handler, but it doesn't seem to work the same way on the dev and prod environments. Dev is windows/xampp, and prod is a standard LAMP install. Sometimes on prod I get a blank white screen and dev shows no errors. If I remove the error handler, everything goes back to normal. Just wondering if anyone sees something wrong here:

 

<?php
function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars)
{
        // email address to email errors to on the production environment
        $email_address = 'myemailaddress@gmail.com';

        switch ($e_number)
        {
                case E_USER_ERROR:
                        $error_type = 'E_USER_ERROR';
                        break;

                case E_USER_WARNING:
                        $error_type = 'E_USER_WARNING';
                        break;

                case E_USER_NOTICE:
                        $error_type = 'E_USER_NOTICE';
                        break;
                
                case E_WARNING:
                        $error_type = 'E_WARNING';
                        break;

                case E_NOTICE:
                        $error_type = 'E_NOTICE';
                        break;

                case E_STRICT:
                        $error_type = 'E_STRICT';
                        break;

                default:
                        $error_type = 'UNKNOWN ERROR TYPE';
                        break;
        }

        $message = '<hr />PHP ' . $error_type . ' #' . $e_number . ' - Date/Time: ' . date('n/j/Y H:i:s') . "\n" .
                                '<br />File: <b>' . $e_file . "</b>\n" .
                                '<br />Line: <b>' . $e_line . "</b>\n" .
                                '<br /><b>' . $e_message . '</b><hr />';

        // Output for development environment
        if (stristr($_SERVER['HTTP_HOST'], 'localhost' ))
        {
                echo $message;
        }

        // Email for production environment
        else
        {
                error_log($message, 1, $email_address );

                if ( $e_number != E_NOTICE && $e_number < E_STRICT)
                {
                        die('A system error occurred. We apologize for the inconvenience.');
                }
        }

        // Don't execute PHP internal error handler
        return true;
}

function my_error_handling()
{
        set_error_handler('my_error_handler', E_ALL);
}

Link to comment
Share on other sites

As you have set_error_handler('my_error_handler', E_ALL); within your my_error_handling() function make sure you are calling that function in order for the error handler to work. Also in order for errors to be displayed you need to enable display_errors either within your script using ini_set or within your PHP configuration file (php.ini).

 

Also PHP will also log the errors within your servers error logs too. You can configure PHP to log errors to a dedicated file within the php.ini. You may be able set it with ini_se too.

Link to comment
Share on other sites

Yes, the function is for sure being called. I'll need to check display_errors. What is odd is that most errors are being sent to me via email, which is a function of the custom error handler. There are just some instances where I'm getting a blank white screen, and removing the custom error handler seems to make that go away.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.