PHP: How can we turn off exception messages on the production website and store them in dev?

How can I disable exception messages on the production website and save them in dev?

Example:

try{ //some code } catch(Exception $e){ echo $e.getMessage(); } 

Edit:

How to do it on the Zend Framework ? (.ini file, but what about the exception code that should be written?)

Edit 2:

If my example does not work, how does the zend framework disable exception messages in application.ini?

 resources.frontController.params.displayExceptions = 0 
+4
source share
5 answers

I’m not sure that we will recover.

Other answers describe whether or not to exclude an exception regarding environment settings.

The answer is simple: Exceptions should be thrown always, and not in relation to the environment.

If you need to, you can handle them differently, but you should avoid conditional exceptions.

There are many ways you can configure error handling in your application:

  • ini settings in apache or .htaccess config
  • using the same settings through php functions (e.g. error_reporting() )
  • setting up your front controller (using application.ini or getting an instance of the front controller in Bootstrap )
  • update default error handler ZF
  • create your own error handler

The easiest way is application.ini :

 phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 resources.frontController.throwExceptions = 1 resources.frontController.params.displayExceptions = 1 

In the application.ini development section, you can use zeros if necessary.

I recommend using the Log Application resource to handle exceptions in the production environment.

+6
source

I think you should have some kind of flag in your configuration file that points to the installation as dev or production. Then, starting with echo $e.getMessage() , you can pass it to a function that outputs it if it is a dev server and remains completely if it is production.

+1
source

There is no way in this code. You must change the code.

For example, make function show_error (), which prints an error on dev and does nothing on prod (only different implementations).

+1
source

If you need to print messages, you can wrap your code inside a function (or a static class method) that checks CONSTANT for values ​​DEVELOPMENT | PRODUCTION

Example:

 function printMessage($Exception) { if(DEV_ENVIRONMENT) { echo $Exception->getMessage(); } } 
+1
source

My wild guess is to only change the ErrorController and view the scripts. Here are some random error.phtml:

 <?php echo $this->message; ?> <?php if ('development' == APPLICATION_ENV): ?> <h3>Exception information:</h3> <p> <b>Message:</b> <?php echo $this->exception->getMessage() ?> </p> <h3>Stack trace:</h3> <pre><?php echo $this->exception->getTraceAsString() ?> </pre> <h3>Request Parameters:</h3> <pre><?php echo var_export($this->request->getParams(), true) ?> </pre> <?php endif ?> 

Therefore, an error message is always displayed, but information about the error itself is only on dev.

+1
source

All Articles