-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from flownative/task/refactor-to-throwablestorage
Refactor to `ThrowableStorageInterface` implementation
- Loading branch information
Showing
8 changed files
with
126 additions
and
143 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Flownative\Sentry\Log; | ||
|
||
class CaptureResult { | ||
public function __construct( | ||
public readonly bool $suceess, | ||
public readonly string $message, | ||
public readonly string $eventId | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Flownative\Sentry\Log; | ||
|
||
use Flownative\Sentry\SentryClientTrait; | ||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Flow\Log\ThrowableStorageInterface; | ||
|
||
/** | ||
* Captures a throwable to Sentry. | ||
* | ||
* @phpstan-consistent-constructor | ||
* @Flow\Proxy(false) | ||
* @Flow\Autowiring(false) | ||
*/ | ||
class SentryStorage implements ThrowableStorageInterface | ||
{ | ||
use SentryClientTrait; | ||
|
||
/** | ||
* Factory method to get an instance. | ||
* | ||
* @param array $options | ||
* @return ThrowableStorageInterface | ||
*/ | ||
public static function createWithOptions(array $options): ThrowableStorageInterface | ||
{ | ||
return new static(); | ||
} | ||
|
||
/** | ||
* @param \Closure $requestInformationRenderer | ||
* @return ThrowableStorageInterface | ||
*/ | ||
public function setRequestInformationRenderer(\Closure $requestInformationRenderer): ThrowableStorageInterface | ||
{ | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param \Closure $backtraceRenderer | ||
* @return ThrowableStorageInterface | ||
*/ | ||
public function setBacktraceRenderer(\Closure $backtraceRenderer): ThrowableStorageInterface | ||
{ | ||
return $this; | ||
} | ||
|
||
/** | ||
* Stores information about the given exception and returns information about | ||
* the exception and where the details have been stored. The returned message | ||
* can be logged or displayed as needed. | ||
* | ||
* The returned message follows this pattern: | ||
* Exception #<code> in <line> of <file>: <message> - See also: <dumpFilename> | ||
* | ||
* @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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters