views:

113

answers:

5

Hi,

How can be disable exception messages on the production website and keep them in dev?

Example:

try{
  //some code
}

catch(Exception $e){
   echo $e.getMessage();
}

Edit:

How it done on Zend Framework? (.ini file but what about exception code that should be write?)

Edit 2:

If my example can't work how zend framework disable exception messages in application.ini?

resources.frontController.params.displayExceptions = 0

Thanks

+1  A: 

I guess you should have some sort of flag in your config file which indicates the install as dev or production. Then intead of echo $e.getMessage() you can pass it to a function which prints it out if it is a dev server and stays quite if it is production.

Sabeen Malik
+1  A: 

In this code there is no way. You have to change to code.

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

BarsMonster
what about trigger_error($e.getMessage())?
Yosef
Well, yeah, that is also great :-)
BarsMonster
+1  A: 

If you want message printing you could wrap your code inside a function (or static class method) that checks the CONSTANT for values DEVELOPMENT|PRODUCTION

example:

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

My wild guess is to change only the ErrorController and it's view scripts. Here's 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 ?>

So the error message always gets displayed, but the information about the error itself only on dev.

robertbasic
+4  A: 

I'm not sure if we are getting you right.

The other answers tell how whether to throw or not the Exception regarding the environment setting.

The answer is simple: The exceptions should be thrown always, not regarding the environment.

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

There are many ways, you may set up error handling in your app:

  • ini settings in apache or .htaccess config
  • using the same settings via php functions (e.g. error_reporting())
  • configuring your front controller (using application.ini or getting front controller instance in Bootstrap)
  • update ZF's default error handler
  • create your own error handler

The simplest it seems is application.ini:

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

In development section of application.ini you may use zeros where needed.

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

takeshin
Thanks! but how resources.frontController.params.displayExceptions = 1 implemented in zend framework?
Yosef
@Yosef Just put it in `application.ini` and it should work. Be sure you have properly configured `[development : production]` section and your `APPLICATION_ENV` settings.
takeshin
Its already in application by default, I asking how zend implement it.
Yosef