简体   繁体   中英

Symfony2 array to string conversion error handling in production

Amateur here.

Using Symfony 2.6.13.

Production environment: AWS Linux, Elastic Beanstalk. Development environment: WAMP.

I have an error which is confusing me as it only causes me an issue in development. Here is an extract from the error log on production:

[Wed Feb 22 10:40:16.484644 2017] [:error] [pid 12453] [client 172.31.46.85:18619] PHP Notice:  Array to string conversion in /var/app/current/app/cache/prod/twig/db/dbafcb45562bb5839ccefc3c501bc398a96f8c34fd19c0f11d60122efe04cb15.php on line 220, referer: https://xxx-xxx.xxx.com/contact/986513/risk/1

I get this error in both the production and development environment. In production the error seems to be ignored and the page loads. In development the error is not ignored and the page errors saying the following:

CRITICAL: Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown during the rendering of a template ("Notice: Array to string conversion")

Why would this error be ignore in production but not in development?

The answer to my question was that I wasn't aware of the difference between the development and production environment. I was expecting the error I received in development also to cause the same type of error in production and stop the page loading.

I need to understand the differences between the two environments in more detail.

Try setting error_reporting to '0' immediately before rendering, then setting it back to its original value afterwards.

This prevents 'Array to string conversion' errors being thrown which otherwise stop the template being rendered at all.

Instead, you'll see it gets outputted as Array in the rendered text.

We can easily achieve this using ini_set() .

Example below.


My twig helper class's render method looks like this, and works great:

    public function render(string $name, array $params): string
    {
        $this->setIniSettings();

        try {
            $result = $this->twig->render($name, $params);
        } catch (\Throwable $e) {
            $this->restoreIniSettings();
            throw $e;
        }

        $this->restoreIniSettings();
        return $result;
    }

I have a config file which defines the ini settings for rendering twig templates setup like so:

return [
    'ini_settings' => [
        'error_reporting' => '0'
    ],
];

In setIniSettings() i just loop over an array of settings i have defined in my config file like so:

    protected function setIniSettings()
    {
        $this->oldIniSettings = [];

        foreach (config('twig.ini_settings', []) as $setting => $value) {
            $this->oldIniSettings[$setting] = ini_set($setting, $value);
        }
    }

And then restoreIniSettings() looks like this:

    protected function restoreIniSettings()
    {
        foreach ($this->oldIniSettings as $setting => $value) {
            ini_set($setting, $value);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM