networkteam / sentry_client

TYPO3 Extension for exception logging with sentry, see http://www.getsentry.com
33 stars 36 forks source link

PHP Warning: Undefined array key "X-REQUEST-ID" // PHP 8.0 + TYPO3 11 #62

Closed Fanor51 closed 2 years ago

Fanor51 commented 2 years ago

Hello,

i coudn´t find any PHP recommendation so i want to ask if the extension is compatible with PHP 8.0 in combination with TYPO3 11. In my case if get the followed exception:

Fatal error: Uncaught TYPO3\CMS\Core\Error\Exception: PHP Warning: Undefined array key "X-REQUEST-ID" in /var/www/html/public/typo3conf/ext/sentry_client/Classes/Client.php line 129 in /var/www/html/public/typo3/sysext/core/Classes/Error/ErrorHandler.php:137 Stack trace: #0 /var/www/html/vendor/sentry/sentry/src/ErrorHandler.php(305): TYPO3\CMS\Core\Error\ErrorHandler->handleError(2, 'Undefined array...', '/var/www/html/p...', 129, Array) #1 /var/www/html/public/typo3conf/ext/sentry_client/Classes/Client.php(129): Sentry\ErrorHandler->handleError(2, 'Undefined array...', '/var/www/html/p...', 129) #2 /var/www/html/vendor/sentry/sentry/src/State/Hub.php(104): Networkteam\SentryClient\Client::Networkteam\SentryClient\{closure}(Object(Sentry\State\Scope)) #3 /var/www/html/vendor/sentry/sentry/src/functions.php(87): Sentry\State\Hub->configureScope(Object(Closure)) #4 /var/www/html/public/typo3conf/ext/sentry_client/Classes/Client.php(137): Sentry\configureScope(Object(Closure)) #5 /var/www/html/public/typo3conf/ext/sentry_client/Classes/Client.php(62): Networkteam\SentryClient\Client::setTagsContext() #6 /var/www/html/public/typo3conf/ext/sentry_client/Classes/Client.php(73): Networkteam\SentryClient\Client::init() #7 /var/www/html/public/typo3conf/ext/sentry_client/Classes/DebugExceptionHandler.php(13): Networkteam\SentryClient\Client::captureException(Object(TypeError)) #8 [internal function]: Networkteam\SentryClient\DebugExceptionHandler->handleException(Object(TypeError)) #9 {main} thrown in /var/www/html/public/typo3/sysext/core/Classes/Error/ErrorHandler.php on line 137

I tried to debug the place with something like this:

protected static function setTagsContext(): void

{
    configureScope(
        function (Scope $scope): void {

            if (isset($GLOBALS['TYPO3_REQUEST']) && class_exists(ApplicationType::class)) {
                $mode = ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend() ? 'FE' : 'BE';
            } elseif (defined('TYPO3_MODE')) {
                // deprecated in TYPO3 v11
                $mode = TYPO3_MODE;
            }
            $requestId = '';

            if (isset($_SERVER['X-REQUEST-ID'])) {
                $requestId = $_SERVER['X-REQUEST-ID'];
            }

            if (isset($_SERVER['HTTP_X_REQUEST_ID'])) {
                $requestId = $_SERVER['HTTP_X_REQUEST_ID'];
            }

            $scope->setTags(
                array_merge(
                    ['typo3_version' => GeneralUtility::makeInstance(Typo3Version::class)->getVersion()],
                    ($mode ? ['typo3_mode' => $mode] : []),
                    ($requestId ? ['request_id' => $requestId] : [])
                )
            );
        }
    );
}

but then i got new errors in "Networkteam\SentryClient\Client::captureException"

(1/1) TypeError Networkteam\SentryClient\Client::captureException(): Argument #1 ($exception) must be of type Throwable, string given, called in /var/www/html/public/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php on line 5381

at Networkteam\SentryClient\Client::captureException('', array('userFunc' => 'Networkteam\\SentryClient\\Client->captureException'), object(TYPO3\CMS\Core\Http\ServerRequest))

So i just want to ask if its compatible or if i got any errors in my configuration. The system i use was on TYPO9 and PHP 7.2 and got updated to TYPO 11 with PHP 8. In the old version the sentry integration worked perfectly.

The error can be reproduced with the throw exception TYPOSCRIPT config from your read.me.

If this wasn´t intended to work with PHP 8.0 i can try to fix the issues by myself and create a pull request if i get it done.

Best regards Fabio

Edit: used Version -> networkteam/sentry-client 4.1.0

christophlehmann commented 2 years ago

Hi Fabio,

thanks for the report. It's already fixed in 4.1.1. Feel free to reopen the issue if something is missing.

Fanor51 commented 2 years ago

Hello @christophlehmann,

i have installed now version 4.1.1 over composer 2 an still getting this error ->

1/1) TypeError Networkteam\SentryClient\Client::captureException(): Argument #1 ($exception) must be of type Throwable, string given, called in /var/www/html/public/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php on line 5381

in /var/www/html/public/typo3conf/ext/sentry_client/Classes/Client.php line 70


        return false;
    }

    public static function captureException(\Throwable $exception): ?EventId <---
    {
        if (self::init() && ExceptionBlacklistService::shouldHandleException($exception)) {
            $eventId = captureException($exception);
            return $eventId;

at Networkteam\SentryClient\Client::captureException('', array('userFunc' => 'Networkteam\SentryClient\Client->captureException'), object(TYPO3\CMS\Core\Http\ServerRequest)) in /var/www/html/public/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php line 5381

                        // Extensions should either drop the property altogether if they don't need current instance
                        // of ContentObjectRenderer, or set the property to protected and use the setter above.
                        $classObj->cObj = $this;
                    }
                    $content = $callable($content, $conf, $this->getRequest()); <---
                } else {
                    $this->getTimeTracker()->setTSlogMessage('Method "' . $parts[1] . '" did not exist in class "' . $parts[0] . '"', LogLevel::ERROR);
                }
            } else {

This error comes from the testing TYPOSCRIPT ->

page = PAGE
page.20 = USER
page.20 {
  userFunc = Networkteam\SentryClient\Client->captureException
}

I now an exception throw is intended with this but this seems to me like there is something wrong before the actual wanted exception get throwed.

Thanks for the help and best regards! Fabio

christophlehmann commented 2 years ago

The Type Error due to the testing code is intended behaviour. You can read it like "call any function that produces an error"

Will update the docs.