nystudio107 / craft-retour

Retour allows you to intelligently redirect legacy URLs, so that you don't lose SEO value when rebuilding & restructuring a website
https://nystudio107.com/plugins/retour
Other
38 stars 26 forks source link

500 error after create a new site in craftcms #244

Closed xinningsu closed 2 years ago

xinningsu commented 2 years ago

Question

I have multiple sites in caftcms, after I ceated a new site, and open the homepage of the new site, I got this error:

2022-08-22 16:09:37 [-][1][ms5j6j2kgd50drmbh42ts2842f][error][nystudio107\pluginvite\helpers\{closure}] GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: craft-retour-buildchain (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://craft-retour-buildchain:3001/@vite/client in /app/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:210

looks another dependency plugin nystudio107/craft-plugin-vite called a non existent host craft-retour-buildchain

Additional context

2022-08-22 16:09:37 [-][1][ms5j6j2kgd50drmbh42ts2842f][error][nystudio107\pluginvite\helpers\{closure}] GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: craft-retour-buildchain (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://craft-retour-buildchain:3001/@vite/client in /app/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:210
Stack trace:
#0 /app/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(158): GuzzleHttp\Handler\CurlFactory::createRejection()
#1 /app/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(110): GuzzleHttp\Handler\CurlFactory::finishError()
#2 /app/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php(47): GuzzleHttp\Handler\CurlFactory::finish()
#3 /app/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php(28): GuzzleHttp\Handler\CurlHandler->__invoke()
#4 /app/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php(48): GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler\{closure}()
#5 /app/vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php(35): GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler\{closure}()
#6 /app/vendor/guzzlehttp/guzzle/src/Middleware.php(31): GuzzleHttp\PrepareBodyMiddleware->__invoke()
#7 /app/vendor/guzzlehttp/guzzle/src/RedirectMiddleware.php(71): GuzzleHttp\Middleware::GuzzleHttp\{closure}()
#8 /app/vendor/guzzlehttp/guzzle/src/Middleware.php(61): GuzzleHttp\RedirectMiddleware->__invoke()
#9 /app/vendor/guzzlehttp/guzzle/src/HandlerStack.php(75): GuzzleHttp\Middleware::GuzzleHttp\{closure}()
#10 /app/vendor/guzzlehttp/guzzle/src/Client.php(331): GuzzleHttp\HandlerStack->__invoke()
#11 /app/vendor/guzzlehttp/guzzle/src/Client.php(168): GuzzleHttp\Client->transfer()
#12 /app/vendor/guzzlehttp/guzzle/src/Client.php(187): GuzzleHttp\Client->requestAsync()
#13 /app/vendor/nystudio107/craft-plugin-vite/src/helpers/FileHelper.php(97): GuzzleHttp\Client->request()
#14 [internal function]: nystudio107\pluginvite\helpers\FileHelper::nystudio107\pluginvite\helpers\{closure}()
#15 /app/vendor/yiisoft/yii2/caching/Cache.php(607): call_user_func()
#16 /app/vendor/nystudio107/craft-plugin-vite/src/helpers/FileHelper.php(81): yii\caching\Cache->getOrSet()
#17 /app/vendor/nystudio107/craft-plugin-vite/src/services/ViteService.php(182): nystudio107\pluginvite\helpers\FileHelper::fetch()
#18 /app/vendor/nystudio107/craft-plugin-vite/src/services/ViteService.php(167): nystudio107\pluginvite\services\ViteService->fetch()
#19 /app/vendor/nystudio107/craft-plugin-vite/src/services/ViteService.php(410): nystudio107\pluginvite\services\ViteService->devServerRunning()
#20 [internal function]: nystudio107\pluginvite\services\ViteService->injectErrorEntry()
#21 /app/vendor/yiisoft/yii2/base/Component.php(633): call_user_func()
#22 /app/vendor/yiisoft/yii2/web/View.php(164): yii\base\Component->trigger()
#23 /app/vendor/craftcms/cms/src/web/View.php(1161): yii\web\View->endBody()
khalwat commented 2 years ago

What version of Craft CMS, and what version of Retour?

xinningsu commented 2 years ago

Craft CMS: 3.7.46 Retour: 3.1.73

khalwat commented 2 years ago

You should be able to fix this by updating to the latest version of craft-plugin-vite by doing:

composer update nystudio107/craft-plugin-vite
xinningsu commented 2 years ago

@khalwat thanks for your help, but unfortunately, it doesn't work. I don't use Vite, and this url hardcode here is not accessible: https://github.com/nystudio107/craft-retour/blob/develop/src/Retour.php#L145

2022-08-24 11:22:54 [-][-][mec5g510jc1gmjdq7r51m74m2i][error][nystudio107\pluginvite\helpers\FileHelper::fetchResponse] cURL error 6: Could not resolve host: craft-retour-buildchain (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://craft-retour-buildchain:3001/__vite_ping
khalwat commented 2 years ago

You don't use Vite, but my plugin does.

Where are you seeing this error? In logs, or elsewhere?

khalwat commented 2 years ago

The code specifically turns off HTTP_ERRORS:

https://github.com/nystudio107/craft-plugin-vite/blob/4360a27fe197a85121fb62e0320090c69d9f1a04/src/helpers/FileHelper.php#L111

...and also catches any exceptions that might be thrown:

https://github.com/nystudio107/craft-plugin-vite/blob/4360a27fe197a85121fb62e0320090c69d9f1a04/src/helpers/FileHelper.php#L123

What version of Guzzle is installed in your setup?

khalwat commented 2 years ago

oooooo, I think I know what might be going on here! Give me a moment

khalwat commented 2 years ago

Addressed in 1.0.28: https://github.com/nystudio107/craft-plugin-vite/releases/tag/1.0.28 & 4.0.4: https://github.com/nystudio107/craft-plugin-vite/releases/tag/4.0.4

To update, do this again:

composer update nystudio107/craft-plugin-vite

...and let me know how you go

xinningsu commented 2 years ago

@khalwat Sorry for late reply, have been busy these days.

I have Guzzle 7.4.5, I saw the error in logs.

After I upgrade nystudio107/craft-plugin-vite to 1.0.28, still not working. but a litter different, another plugin craft-typogrify involved. Strange thing is this happens only when I create a new site in cms and visit the new site url first time. after refreshing the page, the error is gone.

2022-08-29 12:56:06 [-][1][0svnabdjqjjsp9688hv5me6c7s][error][nystudio107\pluginvite\helpers\FileHelper::fetchResponse] cURL error 6: Could not resolve host: craft-retour-buildchain (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://craft-retour-buildchain:3001/__vite_ping
2022-08-29 12:56:06 [-][1][0svnabdjqjjsp9688hv5me6c7s][error][LogicException] LogicException: Unable to register extension "nystudio107\typogrify\twigextensions\TypogrifyTwigExtension" as extensions have already been initialized. in /app/vendor/twig/twig/src/ExtensionSet.php:145
Stack trace:
#0 /app/vendor/twig/twig/src/Environment.php(701): Twig\ExtensionSet->addExtension()
#1 /app/vendor/craftcms/cms/src/web/View.php(362): Twig\Environment->addExtension()
#2 /app/vendor/nystudio107/craft-typogrify/src/Typogrify.php(62): craft\web\View->registerTwigExtension()
#3 /app/vendor/yiisoft/yii2/base/BaseObject.php(109): nystudio107\typogrify\Typogrify->init()
#4 /app/vendor/yiisoft/yii2/base/Module.php(161): yii\base\BaseObject->__construct()
#5 /app/vendor/craftcms/cms/src/base/Plugin.php(115): yii\base\Module->__construct()
#6 [internal function]: craft\base\Plugin->__construct()
#7 /app/vendor/yiisoft/yii2/di/Container.php(419): ReflectionClass->newInstanceArgs()
#8 /app/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build()
#9 /app/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get()
#10 /app/vendor/craftcms/cms/src/Craft.php(61): yii\BaseYii::createObject()
#11 /app/vendor/craftcms/cms/src/services/Plugins.php(948): Craft::createObject()
#12 /app/vendor/craftcms/cms/src/services/Plugins.php(232): craft\services\Plugins->createPlugin()
#13 /app/vendor/craftcms/cms/src/services/Plugins.php(797): craft\services\Plugins->loadPlugins()
#14 /app/modules/sealink/Module.php(60): craft\services\Plugins->isPluginInstalled()
#15 /app/vendor/yiisoft/yii2/base/BaseObject.php(109): modules\sealink\Module->init()
#16 /app/vendor/yiisoft/yii2/base/Module.php(161): yii\base\BaseObject->__construct()
#17 [internal function]: yii\base\Module->__construct()
#18 /app/vendor/yiisoft/yii2/di/Container.php(411): ReflectionClass->newInstanceArgs()
#19 /app/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build()
#20 /app/vendor/yiisoft/yii2/BaseYii.php(345): yii\di\Container->get()
#21 /app/vendor/yiisoft/yii2/base/Module.php(445): yii\BaseYii::createObject()
#22 /app/vendor/yiisoft/yii2/base/Application.php(313): yii\base\Module->getModule()
#23 /app/vendor/craftcms/cms/src/web/Application.php(116): yii\base\Application->bootstrap()
#24 /app/vendor/yiisoft/yii2/base/Application.php(271): craft\web\Application->bootstrap()
#25 /app/vendor/craftcms/cms/src/web/Application.php(93): yii\base\Application->init()
#26 /app/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\web\Application->init()
#27 /app/vendor/yiisoft/yii2/base/Application.php(204): yii\base\BaseObject->__construct()
#28 [internal function]: yii\base\Application->__construct()
#29 /app/vendor/yiisoft/yii2/di/Container.php(419): ReflectionClass->newInstanceArgs()
#30 /app/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build()
#31 /app/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get()
#32 /app/vendor/craftcms/cms/src/Craft.php(61): yii\BaseYii::createObject()
#33 /app/vendor/craftcms/cms/bootstrap/bootstrap.php(247): Craft::createObject()
#34 /app/vendor/craftcms/cms/bootstrap/web.php(51): require('...')
#35 /app/web/index.php(81): require('...')
#36 {main}
xinningsu commented 2 years ago

I noticed the plugin catches exceptions, but better avoid the exception instead of catching it every time? Such as here: https://github.com/nystudio107/craft-retour/blob/4.0.2/src/Retour.php#L128 If we can custom useDevServer to false, I think the problem will be gone, not sure if new problem will be introduced.

khalwat commented 2 years ago

Well, so here's the thing that I don't understand. It should automatically be setting useDevServer to false already:

https://github.com/nystudio107/craft-plugin-vite/blob/develop/src/services/VitePluginService.php#L54

...unless somehow, for some weird, reason, you have the VITE_PLUGIN_DEVSERVER environment variable set?

xinningsu commented 2 years ago

@khalwat No, I don't have VITE_PLUGIN_DEVSERVER set. And that part of code is not executed, it's return here: https://github.com/nystudio107/craft-plugin-vite/blob/develop/src/services/VitePluginService.php#L52

khalwat commented 2 years ago

Oh nice catch, I was completely overlooking that! That's the root problem here then, though I have no idea how creating a new site could result in a non-CP request!

xinningsu commented 2 years ago

Creating a new site won't have the issue, only when visit the new site first time, such as: https://localhost/new-site-url/

khalwat commented 2 years ago

Addressed in: https://github.com/nystudio107/craft-plugin-vite/releases/tag/1.0.29 & https://github.com/nystudio107/craft-plugin-vite/releases/tag/4.0.5

To update, do this again:

composer update nystudio107/craft-plugin-vite

...and let me know how you go. I'm feeling good about this one, thanks for your patience.

xinningsu commented 2 years ago

@khalwat I have craft-plugin-vite upgraded to 1.0.29, but the error still appears in my log file:

2022-08-29 15:24:03 [-][1][vk5fspj4d9li6krlbdgn1goj69][error][nystudio107\pluginvite\helpers\FileHelper::fetchResponse] cURL error 6: Could not resolve host: craft-retour-buildchain (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://craft-retour-buildchain:3001/__vite_ping

But if I manually change this value to false, won't see the error.

xinningsu commented 2 years ago

every request will add a record to log file, although the page is not 500.

2022-08-29 15:52:51 [-][1][-][error][nystudio107\pluginvite\helpers\FileHelper::fetchResponse] cURL error 6: Could not resolve host: craft-retour-buildchain (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://craft-retour-buildchain:3001/__vite_ping
2022-08-29 15:53:15 [-][1][-][error][nystudio107\pluginvite\helpers\FileHelper::fetchResponse] cURL error 6: Could not resolve host: craft-retour-buildchain (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://craft-retour-buildchain:3001/__vite_ping
2022-08-29 15:53:27 [-][1][-][error][nystudio107\pluginvite\helpers\FileHelper::fetchResponse] cURL error 6: Could not resolve host: craft-retour-buildchain (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://craft-retour-buildchain:3001/__vite_ping
2022-08-29 15:53:51 [-][1][-][error][nystudio107\pluginvite\helpers\FileHelper::fetchResponse] cURL error 6: Could not resolve host: craft-retour-buildchain (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://craft-retour-buildchain:3001/__vite_ping
2022-08-29 15:53:55 [-][1][-][error][nystudio107\pluginvite\helpers\FileHelper::fetchResponse] cURL error 6: Could not resolve host: craft-retour-buildchain (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://craft-retour-buildchain:3001/__vite_ping
khalwat commented 2 years ago

Very strange, because it should be setting that value to false here:

https://github.com/nystudio107/craft-plugin-vite/blob/develop/src/services/VitePluginService.php#L52

xinningsu commented 2 years ago

@khalwat yep, I can confirm that part of code has been executed. so I guess maybe there are multiple instances of VitePluginService. some instances with useDevServer = true. this can explain why I manually change this value to false, won't see the error.

khalwat commented 2 years ago

Each plugin I write will have an instance of VitePluginService but they all are executing the same code path, which should set useDevServer to false if that environment variable is not there.

Very puzzling.

khalwat commented 2 years ago

Okay so I managed to reproduce this locally, here's what was happening:

    public function init()
    {
        parent::init();
        // See if the $pluginDevServerEnvVar env var exists, and if not, don't run off of the dev server
        $useDevServer = (bool)App::env($this->pluginDevServerEnvVar);
        if ($useDevServer === false) {
            $this->useDevServer = false;
        }

This is the code from VitePluginService... the problem here is the parent::init() which is called first (as you normally would using typical PHP inheritance patterns). This call to parent::init() is what is attempting to ping the dev server:

    public function init()
    {
        parent::init();
        // Do nothing for console requests
        $request = Craft::$app->getRequest();
        if ($request->getIsConsoleRequest()) {
            return;
        }
        // Our component is lazily loaded, so the View will be instantiated by now
        if ($this->devServerRunning() && Craft::$app->getConfig()->getGeneral()->devMode) {
            Craft::$app->getView()->on(View::EVENT_END_BODY, [$this, 'injectErrorEntry']);
        }
    }

The call to devServerRunning() is what triggers it. At this point, useDevServer still has not properly been set to false because that section of VitePluginService::init() has not been executed yet. So the environment variable check just needs to be done before we call parent::init()

khalwat commented 2 years ago

Addressed in: https://github.com/nystudio107/craft-plugin-vite/releases/tag/1.0.30 & https://github.com/nystudio107/craft-plugin-vite/releases/tag/4.0.6

To update, do this again:

composer update nystudio107/craft-plugin-vite

...and let me know how you go. This fixes it locally in my testing.

xinningsu commented 2 years ago

@khalwat The problem has been completely solved, thanks.

khalwat commented 2 years ago

Thank you for your help, and for your patience @xinningsu ! 非常感谢