born05 / craft-sentry

Pushes Craft CMS errors to Sentry.
MIT License
10 stars 11 forks source link

Use getenv() to get CRAFT_ENVIRONMENT #20

Closed weotch closed 2 years ago

weotch commented 2 years ago

I'm getting this error on PHP 8, Craft 4, and 2.0.0-beta.1 of this plugin:

Exception 'Error' with message 'Undefined constant "born05\sentry\CRAFT_ENVIRONMENT"'

in /Users/reinhard/Work - Client/Sendbird/craft-cms/vendor/born05/craft-sentry/src/Plugin.php:64

Stack trace:
#0 /craft-cms/vendor/yiisoft/yii2/base/BaseObject.php(109): born05\sentry\Plugin->init()
#1 /craft-cms/vendor/yiisoft/yii2/base/Module.php(161): yii\base\BaseObject->__construct(Array)
#2 /craft-cms/vendor/craftcms/cms/src/base/Plugin.php(122): yii\base\Module->__construct('sentry-sdk', Object(craft\console\Application), Array)
...
jamesmacwhite commented 2 years ago

@weotch Not the plugin maintainer but that's strange as I believe the CRAFT_ENVIRONMENT constant is still valid and works in Craft CMS 4.

https://craftcms.com/docs/4.x/config/#craft-environment

However, I do know by the default Craft CMS 4 now looks for environment variables with the CRAFT_ prefix against config settings now, what might be the issue is in the web/index.php, there was always this line present which defined CRAFT_ENVIRONMENT to a value.

https://github.com/craftcms/craft/blob/309136a5abb3ba48018caf10ea03b61fa4a4b744/bootstrap.php#L20

In Craft CMS 4 this has been removed due to Craft 4 automatically using environment variable names with the CRAFT_ prefix. So I assume this is why it's undefined.

Rather than getenv() using the App helper with App::env('CRAFT_ENVIRONMENT') is probably better though.

Without having to change the plugin you can fix this by defining the constant in your web/index.php and console file, but that's probably more of a workaround.

define('CRAFT_ENVIRONMENT', App::env('CRAFT_ENVIRONMENT') ?: 'dev');
roelvanhintum commented 2 years ago

@weotch & @jamesmacwhite thanks for point this out. I've fixed it more accordingly, i think, to Craft's way of doing this in Craft 4. Please let me know if this doesn't work as expected. See version: 2.0.0

jamesmacwhite commented 2 years ago

@roelvanhintum Awesome, you can probably use ?: when checking for presence of a CRAFT_ environment variable not present, so you can specify the fallback in a slightly cleaner way