We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to disable phalcon exception message in production?

For example, "PhalconException: Action 'XXX' was not found on handler 'AbcdController'".


edited May '17

Use Phalcon\Events\Manager with dispatcher then attach a dispatch:beforeException with an internal class extended by Phalcon\Mvc\User\Plugin and define a beforeException function where you can catch the phalcon exceptions to do the appropriate action.
Example :
https://github.com/corentin-begne/phalconTool/blob/master/templates/project/app/config/services.php https://github.com/corentin-begne/phalconTool/blob/master/templates/project/app/plugins/SecurityPlugin.php

I implemented this solution:

Only you have to modify file index.php in public folder and set:

  1. Set error_reporting with value 0.
  2. In catch block, Comment or Delete echo code.
  3. If you want, could redirect to a specific controller and view and so not show blank page or default error 500 in browser

This is a example of my index.php file:

use Phalcon\Di\FactoryDefault;
use Phalcon\Http\Response; // Use Namespace to allow redirect

error_reporting(0); // Production // E_ALL Value to Development Enviroment

define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');

try {

     * The FactoryDefault Dependency Injector automatically registers
     * the services that provide a full stack framework.
    $di = new FactoryDefault();

     * Read services
    include APP_PATH . "/config/services.php";

     * Get config service for use in inline setup below
    $config = $di->getConfig();

     * Include Autoloader
    include APP_PATH . '/config/loader.php';

     * Handle the request
    $application = new \Phalcon\Mvc\Application($di);

    echo $application->handle()->getContent();

} catch (\Exception $e) {

    // *********** Production Enviroment  ******************
    // Disable phalcon errors...

    // you could comment or delete...
    //echo $e->getMessage() . '<br>';
    //echo '<pre>' . $e->getTraceAsString() . '</pre>';

    // If  you want, redirect to a specific view

    $response = new Response();

    // Redirect...
    $response->redirect('error/show500'); // I have a Controller called error and a view with styles to show error 500

    // Send response to the client