Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How can we disable exception messages on production website and keep them in dev?

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
like image 813
Ben Avatar asked Dec 22 '22 23:12

Ben


2 Answers

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.

like image 50
takeshin Avatar answered Jan 19 '23 01:01

takeshin


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();
     }
}
like image 29
Andreas Avatar answered Jan 18 '23 23:01

Andreas