|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Flownative\Sentry\Log; |
| 5 | + |
| 6 | +use Flownative\Sentry\SentryClientTrait; |
| 7 | +use Neos\Flow\Annotations as Flow; |
| 8 | +use Neos\Flow\Log\ThrowableStorageInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * Captures a throwable to Sentry. |
| 12 | + * |
| 13 | + * @phpstan-consistent-constructor |
| 14 | + * @Flow\Proxy(false) |
| 15 | + * @Flow\Autowiring(false) |
| 16 | + */ |
| 17 | +class SentryStorage implements ThrowableStorageInterface |
| 18 | +{ |
| 19 | + use SentryClientTrait; |
| 20 | + |
| 21 | + /** |
| 22 | + * Factory method to get an instance. |
| 23 | + * |
| 24 | + * @param array $options |
| 25 | + * @return ThrowableStorageInterface |
| 26 | + */ |
| 27 | + public static function createWithOptions(array $options): ThrowableStorageInterface |
| 28 | + { |
| 29 | + return new static(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @param \Closure $requestInformationRenderer |
| 34 | + * @return ThrowableStorageInterface |
| 35 | + */ |
| 36 | + public function setRequestInformationRenderer(\Closure $requestInformationRenderer): ThrowableStorageInterface |
| 37 | + { |
| 38 | + return $this; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param \Closure $backtraceRenderer |
| 43 | + * @return ThrowableStorageInterface |
| 44 | + */ |
| 45 | + public function setBacktraceRenderer(\Closure $backtraceRenderer): ThrowableStorageInterface |
| 46 | + { |
| 47 | + return $this; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Stores information about the given exception and returns information about |
| 52 | + * the exception and where the details have been stored. The returned message |
| 53 | + * can be logged or displayed as needed. |
| 54 | + * |
| 55 | + * The returned message follows this pattern: |
| 56 | + * Exception #<code> in <line> of <file>: <message> - See also: <dumpFilename> |
| 57 | + * |
| 58 | + * @param \Throwable $throwable |
| 59 | + * @param array $additionalData |
| 60 | + * @return string Informational message about the stored throwable |
| 61 | + */ |
| 62 | + public function logThrowable(\Throwable $throwable, array $additionalData = []): string |
| 63 | + { |
| 64 | + $message = $this->getErrorLogMessage($throwable); |
| 65 | + try { |
| 66 | + if ($sentryClient = self::getSentryClient()) { |
| 67 | + $captureResult = $sentryClient->captureThrowable($throwable, $additionalData); |
| 68 | + if ($captureResult->suceess) { |
| 69 | + $message .= ' (Sentry: #' . $captureResult->eventId . ')'; |
| 70 | + } else { |
| 71 | + $message .= ' (Sentry: ' . $captureResult->message . ')'; |
| 72 | + } |
| 73 | + } |
| 74 | + } catch (\Throwable $e) { |
| 75 | + $message .= ' – Error capturing message: ' . $this->getErrorLogMessage($e); |
| 76 | + } |
| 77 | + |
| 78 | + return $message; |
| 79 | + } |
| 80 | + |
| 81 | + protected function getErrorLogMessage(\Throwable $error): string |
| 82 | + { |
| 83 | + // getCode() does not always return an integer, e.g. in PDOException it can be a string |
| 84 | + if (is_int($error->getCode()) && $error->getCode() > 0) { |
| 85 | + $errorCodeString = ' #' . $error->getCode(); |
| 86 | + } else { |
| 87 | + $errorCodeString = ' [' . $error->getCode() . ']'; |
| 88 | + } |
| 89 | + $backTrace = $error->getTrace(); |
| 90 | + $line = isset($backTrace[0]['line']) ? ' in line ' . $backTrace[0]['line'] . ' of ' . $backTrace[0]['file'] : ''; |
| 91 | + |
| 92 | + return 'Exception' . $errorCodeString . $line . ': ' . $error->getMessage(); |
| 93 | + } |
| 94 | +} |
0 commit comments