From e48af455e1b8732459b6fc64c48aaa8caf685520 Mon Sep 17 00:00:00 2001 From: Karsten Dambekalns Date: Mon, 18 Nov 2024 20:28:48 +0100 Subject: [PATCH] Align return type of `captureMessage` to `captureThrowable` --- Classes/Command/SentryCommandController.php | 8 +++-- Classes/SentryClient.php | 36 ++++++++------------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/Classes/Command/SentryCommandController.php b/Classes/Command/SentryCommandController.php index a95f7c8..05cd3fc 100644 --- a/Classes/Command/SentryCommandController.php +++ b/Classes/Command/SentryCommandController.php @@ -77,7 +77,7 @@ public function testCommand(string $mode = self::TEST_MODE_THROW): void private function captureMessage(): void { - $eventId = $this->sentryClient->captureMessage( + $captureResult = $this->sentryClient->captureMessage( 'Flownative Sentry Plugin Test', Severity::debug(), [ @@ -86,7 +86,11 @@ private function captureMessage(): void ); $this->outputLine(); - $this->outputLine('An informational message was sent to Sentry Event ID: #%s', [$eventId]); + if ($captureResult->suceess) { + $this->outputLine('An informational message was sent to Sentry Event ID: #%s', [$captureResult->eventId]); + } else { + $this->outputLine('Sending an informational message to Sentry failed: %s', [$captureResult->message]); + } $this->outputLine(); } diff --git a/Classes/SentryClient.php b/Classes/SentryClient.php index e0e4682..9851823 100644 --- a/Classes/SentryClient.php +++ b/Classes/SentryClient.php @@ -222,38 +222,28 @@ public function captureThrowable(Throwable $throwable, array $extraData = [], ar ); } - public function captureMessage(string $message, Severity $severity, array $extraData = [], array $tags = []): ?EventId + public function captureMessage(string $message, Severity $severity, array $extraData = [], array $tags = []): CaptureResult { if (empty($this->dsn)) { - if ($this->logger) { - $this->logger->warning('Sentry: Failed capturing message, because no Sentry DSN was set. Please check your settings.'); - } - return null; - } - - if (preg_match('/Sentry: [0-9a-f]{32}/', $message) === 1) { - return null; + return new CaptureResult( + false, + 'Failed capturing message, because no Sentry DSN was set. Please check your settings.', + '' + ); } + $this->setTags(); $this->configureScope($extraData, $tags); $eventHint = EventHint::fromArray([ 'stacktrace' => $this->prepareStacktrace() ]); - $this->setTags(); - $sentryEventId = \Sentry\captureMessage($message, $severity, $eventHint); - - if ($this->logger) { - $this->logger->log( - (string)$severity, - sprintf( - '%s (Sentry: %s)', - $message, - $sentryEventId - ) - ); - } + $sentryEventId = SentrySdk::getCurrentHub()->captureMessage($message, $severity, $eventHint); - return $sentryEventId; + return new CaptureResult( + true, + $message, + (string)$sentryEventId + ); } private function configureScope(array $extraData, array $tags): void