Jodaille
Jodaille

Reputation: 25

How hide PDOException with zend framework 2 on production environment?

We use a zend framework 2 for a web application.

We though to have disabled error_reporting and display_errors in our production environment.

But if an SQL error occured (It should not in production but ... :-) ), the exception is still displayed:

PDOException
File:
[...]/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php:165
Message:
SQLSTATE[42000]: Syntax error or access violation

The query use Doctrine\DBAL\Statement (Doctrine2).

We cannot find where to globally catch this exception.

Upvotes: 1

Views: 985

Answers (3)

AlexP
AlexP

Reputation: 9857

Ensure you have the correct view_manager config settings.

// config/autoload/global.php
return array(
    'view_manager' => array(        
        'display_not_found_reason' => false,
        'display_exceptions'       => false,
    ),
);

Remember that this config is merged; if it's in your main global.php it will take preference over the module.config.php.

How it works

Every time ZF2 encounters and error (an exception) it will always catch the error. Instead of 'rethrowing' the exception information to the screen, the info is added to the MVC event and an 'error' event is triggered (either dispatch.error or render.error depending on where it is within the dispatch loop).

The Zend\Mvc\View\Http\ViewManager attaches 'error listeners' to handle these occurrences - (normally to show the error template). If you're using the standard skeleton application, the default error template will check the display_exceptions option and only render the error if it's enabled.

Upvotes: 1

darroosh
darroosh

Reputation: 831

inside: Zend\Db\Adapter\Driver\Pdo\Connection

search for line :

$this->resource = new \PDO($dsn, $username, $password, $options);
$this->resource->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

and change it into:

$this->resource = new \PDO($dsn, $username, $password, $options);
$this->resource->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);

I don't know if there is a way to override it globally without changing it in the library itself..

Upvotes: 1

Exlord
Exlord

Reputation: 5371

this is how i do it, inside index.php

try {
    include ROOT . '/init_autoloader.php';

    Zend\Mvc\Application::init(include 'config/application.config.php')->run();
} catch (Exception $e) {
    if (IS_DEVELOPMENT_SERVER)
        throw $e;
    else {
        echo('Error : ' . $e->getCode() . " " . $e->getMessage());
    }
}

Upvotes: 0

Related Questions