Skip to content

Commit 32ff214

Browse files
authored
Merge pull request #33 from flownative/task/refactor-to-throwablestorage
Refactor to `ThrowableStorageInterface` implementation
2 parents f1fa5a2 + 1f9bd5b commit 32ff214

File tree

8 files changed

+126
-143
lines changed

8 files changed

+126
-143
lines changed

Classes/Exception/DebugExceptionHandler.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

Classes/Exception/ProductionExceptionHandler.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

Classes/Log/CaptureResult.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Flownative\Sentry\Log;
5+
6+
class CaptureResult {
7+
public function __construct(
8+
public readonly bool $suceess,
9+
public readonly string $message,
10+
public readonly string $eventId
11+
) {}
12+
}

Classes/Log/SentryStorage.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

Classes/SentryClient.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Flownative\Sentry\Context\UserContext;
1717
use Flownative\Sentry\Context\UserContextServiceInterface;
1818
use Flownative\Sentry\Context\WithExtraDataInterface;
19+
use Flownative\Sentry\Log\CaptureResult;
1920
use GuzzleHttp\Psr7\ServerRequest;
2021
use Jenssegers\Agent\Agent;
2122
use Neos\Flow\Annotations as Flow;
@@ -182,12 +183,19 @@ public function getOptions(): Options
182183
return new Options();
183184
}
184185

185-
public function captureThrowable(Throwable $throwable, array $extraData = [], array $tags = []): void
186+
public function captureThrowable(Throwable $throwable, array $extraData = [], array $tags = []): CaptureResult
186187
{
187188
if (empty($this->dsn)) {
188-
return;
189+
return new CaptureResult(
190+
false,
191+
'Failed capturing message, because no Sentry DSN was set. Please check your settings.',
192+
''
193+
);
189194
}
190195

196+
$message = '';
197+
$sentryEventId = '';
198+
191199
if ($throwable instanceof WithReferenceCodeInterface) {
192200
$extraData['Reference Code'] = $throwable->getReferenceCode();
193201
}
@@ -215,20 +223,13 @@ public function captureThrowable(Throwable $throwable, array $extraData = [], ar
215223
$this->addThrowableToEvent($throwable, $event);
216224
$sentryEventId = SentrySdk::getCurrentHub()->captureEvent($event);
217225
} else {
218-
$sentryEventId = 'ignored';
219-
}
220-
if ($this->logger) {
221-
$this->logger->log(
222-
($captureException ? LogLevel::CRITICAL : LogLevel::NOTICE),
223-
sprintf(
224-
'Exception #%s: %s (Ref: %s | Sentry: %s)',
225-
$throwable->getCode(),
226-
$throwable->getMessage(),
227-
($throwable instanceof WithReferenceCodeInterface ? $throwable->getReferenceCode() : '-'),
228-
$sentryEventId
229-
)
230-
);
226+
$message = 'ignored';
231227
}
228+
return new CaptureResult(
229+
true,
230+
$message,
231+
(string)$sentryEventId
232+
);
232233
}
233234

234235
public function captureMessage(string $message, Severity $severity, array $extraData = [], array $tags = []): ?EventId

Configuration/Development/Settings.yaml

Lines changed: 0 additions & 5 deletions
This file was deleted.

Configuration/Production/Settings.yaml

Lines changed: 0 additions & 5 deletions
This file was deleted.

Configuration/Settings.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ Neos:
1212
log:
1313
systemLogger:
1414
backend: Flownative\Sentry\Log\SentryFileBackend
15+
throwables:
16+
storageClass: 'Flownative\Sentry\Log\SentryStorage'
17+
optionsByImplementation:
18+
'Flownative\Sentry\Log\SentryStorage': []

0 commit comments

Comments
 (0)