diff --git a/Classes/Exception/DebugExceptionHandler.php b/Classes/Exception/DebugExceptionHandler.php deleted file mode 100755 index a55beef..0000000 --- a/Classes/Exception/DebugExceptionHandler.php +++ /dev/null @@ -1,59 +0,0 @@ -renderingOptions = $this->resolveCustomRenderingOptions($exception); - - $exceptionWasLogged = false; - if ($this->throwableStorage instanceof ThrowableStorageInterface && isset($this->renderingOptions['logException']) && $this->renderingOptions['logException']) { - $message = $this->throwableStorage->logThrowable($exception); - $this->logger->critical($message); - $exceptionWasLogged = true; - } - - try { - if ($sentryClient = self::getSentryClient()) { - $sentryClient->captureThrowable($exception); - } - } catch (\Throwable $e) { - } - - if (PHP_SAPI === 'cli') { - # Doesn't return: - $this->echoExceptionCli($exception, $exceptionWasLogged); - } else { - $this->echoExceptionWeb($exception); - } - } -} diff --git a/Classes/Exception/ProductionExceptionHandler.php b/Classes/Exception/ProductionExceptionHandler.php deleted file mode 100755 index 8feabf0..0000000 --- a/Classes/Exception/ProductionExceptionHandler.php +++ /dev/null @@ -1,59 +0,0 @@ -renderingOptions = $this->resolveCustomRenderingOptions($exception); - - $exceptionWasLogged = false; - if ($this->throwableStorage instanceof ThrowableStorageInterface && isset($this->renderingOptions['logException']) && $this->renderingOptions['logException']) { - $message = $this->throwableStorage->logThrowable($exception); - $this->logger->critical($message); - $exceptionWasLogged = true; - } - - try { - if ($sentryClient = self::getSentryClient()) { - $sentryClient->captureThrowable($exception); - } - } catch (\Throwable $e) { - } - - if (PHP_SAPI === 'cli') { - # Doesn't return: - $this->echoExceptionCli($exception, $exceptionWasLogged); - } else { - $this->echoExceptionWeb($exception); - } - } -} diff --git a/Classes/Log/CaptureResult.php b/Classes/Log/CaptureResult.php new file mode 100644 index 0000000..d44fb89 --- /dev/null +++ b/Classes/Log/CaptureResult.php @@ -0,0 +1,12 @@ + in of : - See also: + * + * @param \Throwable $throwable + * @param array $additionalData + * @return string Informational message about the stored throwable + */ + public function logThrowable(\Throwable $throwable, array $additionalData = []): string + { + $message = $this->getErrorLogMessage($throwable); + try { + if ($sentryClient = self::getSentryClient()) { + $captureResult = $sentryClient->captureThrowable($throwable, $additionalData); + if ($captureResult->suceess) { + $message .= ' (Sentry: #' . $captureResult->eventId . ')'; + } else { + $message .= ' (Sentry: ' . $captureResult->message . ')'; + } + } + } catch (\Throwable $e) { + $message .= ' – Error capturing message: ' . $this->getErrorLogMessage($e); + } + + return $message; + } + + protected function getErrorLogMessage(\Throwable $error): string + { + // getCode() does not always return an integer, e.g. in PDOException it can be a string + if (is_int($error->getCode()) && $error->getCode() > 0) { + $errorCodeString = ' #' . $error->getCode(); + } else { + $errorCodeString = ' [' . $error->getCode() . ']'; + } + $backTrace = $error->getTrace(); + $line = isset($backTrace[0]['line']) ? ' in line ' . $backTrace[0]['line'] . ' of ' . $backTrace[0]['file'] : ''; + + return 'Exception' . $errorCodeString . $line . ': ' . $error->getMessage(); + } +} diff --git a/Classes/SentryClient.php b/Classes/SentryClient.php index 3dad613..135a54b 100644 --- a/Classes/SentryClient.php +++ b/Classes/SentryClient.php @@ -16,6 +16,7 @@ use Flownative\Sentry\Context\UserContext; use Flownative\Sentry\Context\UserContextServiceInterface; use Flownative\Sentry\Context\WithExtraDataInterface; +use Flownative\Sentry\Log\CaptureResult; use GuzzleHttp\Psr7\ServerRequest; use Jenssegers\Agent\Agent; use Neos\Flow\Annotations as Flow; @@ -182,12 +183,19 @@ public function getOptions(): Options return new Options(); } - public function captureThrowable(Throwable $throwable, array $extraData = [], array $tags = []): void + public function captureThrowable(Throwable $throwable, array $extraData = [], array $tags = []): CaptureResult { if (empty($this->dsn)) { - return; + return new CaptureResult( + false, + 'Failed capturing message, because no Sentry DSN was set. Please check your settings.', + '' + ); } + $message = ''; + $sentryEventId = ''; + if ($throwable instanceof WithReferenceCodeInterface) { $extraData['Reference Code'] = $throwable->getReferenceCode(); } @@ -215,20 +223,13 @@ public function captureThrowable(Throwable $throwable, array $extraData = [], ar $this->addThrowableToEvent($throwable, $event); $sentryEventId = SentrySdk::getCurrentHub()->captureEvent($event); } else { - $sentryEventId = 'ignored'; - } - if ($this->logger) { - $this->logger->log( - ($captureException ? LogLevel::CRITICAL : LogLevel::NOTICE), - sprintf( - 'Exception #%s: %s (Ref: %s | Sentry: %s)', - $throwable->getCode(), - $throwable->getMessage(), - ($throwable instanceof WithReferenceCodeInterface ? $throwable->getReferenceCode() : '-'), - $sentryEventId - ) - ); + $message = 'ignored'; } + return new CaptureResult( + true, + $message, + (string)$sentryEventId + ); } public function captureMessage(string $message, Severity $severity, array $extraData = [], array $tags = []): ?EventId diff --git a/Configuration/Development/Settings.yaml b/Configuration/Development/Settings.yaml deleted file mode 100644 index 07db229..0000000 --- a/Configuration/Development/Settings.yaml +++ /dev/null @@ -1,5 +0,0 @@ -Neos: - Flow: - error: - exceptionHandler: - className: Flownative\Sentry\Exception\DebugExceptionHandler diff --git a/Configuration/Production/Settings.yaml b/Configuration/Production/Settings.yaml deleted file mode 100644 index 00ecc2c..0000000 --- a/Configuration/Production/Settings.yaml +++ /dev/null @@ -1,5 +0,0 @@ -Neos: - Flow: - error: - exceptionHandler: - className: Flownative\Sentry\Exception\ProductionExceptionHandler diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 99ec675..24b4687 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -12,3 +12,7 @@ Neos: log: systemLogger: backend: Flownative\Sentry\Log\SentryFileBackend + throwables: + storageClass: 'Flownative\Sentry\Log\SentryStorage' + optionsByImplementation: + 'Flownative\Sentry\Log\SentryStorage': []