PHP Classes

Suppressing error in production

Recommend this page to a friend!

      PHP Error and Exception Handling class  >  All threads  >  Suppressing error in production  >  (Un) Subscribe thread alerts  
Subject:Suppressing error in production
Summary:Don't want the error file to show in production
Messages:9
Author:Gerry Danen
Date:2015-12-25 03:05:12
 

  1. Suppressing error in production   Reply   Report abuse  
Picture of Gerry Danen Gerry Danen - 2015-12-25 03:05:12
I don't want to show the file path to potential hackers in production mode.

See http://birds.gerrydanen.com/testEH.php?test=0 (change test to 1 for test mode)

I have
if ( $_IsProduction )
{
define('MODE', 'PRODUCTION');
define('APP_ERROR', 0); // production mode
define('DEBUGGING', FALSE);
define('SEND_ERROR_MAIL', TRUE);
define('ERROR_LOGGING', TRUE);
}
else // test
{
define('MODE', 'DEVELOPMENT');
define('APP_ERROR', E_ALL); // development mode
define('DEBUGGING', TRUE); // development mode
define('SEND_ERROR_MAIL', FALSE);
define('ERROR_LOGGING', TRUE);
}

How do I suppress
"Fatal error: Call to undefined function debug() in /home/gerrydan/birds/public_html/testEH.php on line 24" or at least show just the file name (testEH.php)?

  2. Re: Suppressing error in production   Reply   Report abuse  
Picture of Nitesh Apte Nitesh Apte - 2015-12-25 05:08:16 - In reply to message 1 from Gerry Danen
That has been already taken care of in debug method and define.inc configuration file. Just change the MODE and other values that you want in Production environment.

The way you have done it also very good. And since, you have separate Production and Development mode, when it's Production environment,

echo DEBUGGING === TRUE ? $webMessage : SITE_GENERIC_ERROR_MSG;
MODE == 'DEVELOPMENT' ? exit() : "";

these two statement will give you what you want.

  3. Re: Suppressing error in production   Reply   Report abuse  
Picture of Gerry Danen Gerry Danen - 2015-12-25 16:08:34 - In reply to message 2 from Nitesh Apte
I have changed this function:

public function enableHandler()
{
$displayErrors = MODE === 'DEVELOPMENT' ? 1 : 0;
ini_set('display_errors', $displayErrors);
\error_reporting(APP_ERROR);

\set_error_handler(array($this, 'errorHandler'), APP_ERROR);
\set_exception_handler(array($this, 'exceptionHandler'));
\register_shutdown_function(array($this, 'fatalError'));
}

The displayErrors will be moved to the define file, but I just wanted to share the idea.

It now works as I like it.

  4. Re: Suppressing error in production   Reply   Report abuse  
Picture of Nitesh Apte Nitesh Apte - 2015-12-25 16:30:07 - In reply to message 3 from Gerry Danen
That's great!

And thanks a lot for sharing it. I'll have a look at it and will try to include in my MVC framework.

Merry Christmas!!

  5. Re: Suppressing error in production   Reply   Report abuse  
Picture of Nitesh Apte Nitesh Apte - 2015-12-25 16:32:26 - In reply to message 4 from Nitesh Apte
Also, in case you have time please have a look at http://www.phpclasses.org/package/9536-PHP-A-Front-Controller-based-PHP-MVC-framework.html

or https://github.com/niteshapte/define-mvc. It might give you idea how can this be used.

Also, you are more than welcome to fork the repo and contribute.


Thanks

  6. Re: Suppressing error in production   Reply   Report abuse  
Picture of Gerry Danen Gerry Danen - 2015-12-25 16:44:24 - In reply to message 5 from Nitesh Apte
Thanks!

Merry Christmas to you also.

  7. Re: Suppressing error in production   Reply   Report abuse  
Picture of Nitesh Apte Nitesh Apte - 2015-12-26 19:06:47 - In reply to message 3 from Gerry Danen
Hi Gerry,

Just saw the method that you have changed. We actually don't need to explicitly set 'display_errors'. All the errors and exceptions will be handled by the class only. In case you set it, the error messages will be displayed twice:
1. Because of 'display_errors'
2. Because of the package

While working in production, changed the values accordingly in 'define.inc' file. I think it is better to set environment(development or production) from a configuration file. This is what I will do if it is production env:

define('MODE', 'PROD');
define('APP_ERROR', E_ALL ^ E_NOTICE);
define('DEBUGGING', FALSE);

MODE will be changed so that script doesn't exit and show some generic message. These two statements of ErrorExceptionHandler.php
will do it for me -
echo DEBUGGING === TRUE ? $webMessage : SITE_GENERIC_ERROR_MSG;
MODE == 'DEVELOPMENT' ? exit() : "";

Hope, I was able to make it clear.

Another biggest advantage of this package - In this one you can add custom codes for exception. Make sure when you create/use code for exception, it follow some rules. For instance, use code 888 for exceptions occured during database connectivity. So, in case after a month you want to check how many times there was database connectivity failures, you just need to search for 888 in your log file.

Let me know if there is any question.

Thanks
With Regards
Nitesh Apte

  8. Re: Suppressing error in production   Reply   Report abuse  
Picture of Gerry Danen Gerry Danen - 2015-12-28 18:03:57 - In reply to message 7 from Nitesh Apte
I have added define('ERROR_SHOW_ON_PAGE', 0); to the defines, which I change to 1 for testing.

I like to see the PHP error also during testing (at least for now), so I have added ini_set('display_errors', ERROR_SHOW_ON_PAGE); to the enableHandler() function.

Custom errors would be useful. How would you trigger a custom error code?

  9. Re: Suppressing error in production   Reply   Report abuse  
Picture of Nitesh Apte Nitesh Apte - 2015-12-28 18:52:23 - In reply to message 8 from Gerry Danen
Hi Gerry,

That's also a very good approach. Thanks for letting me know.

For custom error codes, you can use 'trigger_error' method. For example, in a class you can do something like this -

trigger_error("This is custom error code message", CUSTOM_CODE);

and put this code (CUSTOM_CODE) in ErrorCode.inc file:
<?php
return array (
// other inbuilt or custom error codes

CUSTOM_CODE => 'CUSTOM ERROR CODE'
);
?>


Hope this helps.


N