From 9a1b13a01095249cbd969bf208c3f52e284a2150 Mon Sep 17 00:00:00 2001 From: Christophe Gosiau Date: Thu, 12 Sep 2024 14:27:41 +0200 Subject: [PATCH] enable context on CLI scripts --- lib/Skeleton/Error/Handler/SentrySdk.php | 30 ++++++++++++++++-------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/Skeleton/Error/Handler/SentrySdk.php b/lib/Skeleton/Error/Handler/SentrySdk.php index 678fe13..bf9a2e3 100644 --- a/lib/Skeleton/Error/Handler/SentrySdk.php +++ b/lib/Skeleton/Error/Handler/SentrySdk.php @@ -51,18 +51,28 @@ public function handle() { \Sentry\SentrySdk::getCurrentHub()->bindClient($builder->getClient()); // Assign the session to the extra context + $environment = []; if (isset($_SESSION)) { - \Sentry\SentrySdk::getCurrentHub()->configureScope(function (\Sentry\State\Scope $scope): void { - $scope->setContext('environment', [ - 'session' => $_SESSION, - 'post' => $_POST, - 'get' => $_GET, - 'server' => $_SERVER, - ]); - if (isset($_SERVER['REMOTE_ADDR'])) { - $scope->setUser(['ip_address' => $_SERVER['REMOTE_ADDR'] ]); + $environment['session'] = $_SESSION; + } + if (!empty($_POST)) { + $environment['post'] = $_POST; + } + if (!empty($_GET)) { + $environment['get'] = $_GET; + } + if (isset($_SERVER)) { + $environment['server'] = $_SERVER; + } + if (count($environment) > 0) { + \Sentry\SentrySdk::getCurrentHub()->configureScope( + function (\Sentry\State\Scope $scope) use ($environment): void { + $scope->setContext('environment', $environment); + if (isset($_SERVER['REMOTE_ADDR'])) { + $scope->setUser(['ip_address' => $_SERVER['REMOTE_ADDR'] ]); + } } - }); + ); } \Sentry\SentrySdk::getCurrentHub()->captureException($this->exception);