Skip to content

Commit

Permalink
Merge pull request #9 from flownative/7-method-signature-doesnt-match…
Browse files Browse the repository at this point in the history
…-to-abstractexceptionhandler-anymore

Adjust to changed signature of echoExceptionCli()
  • Loading branch information
robertlemke authored Jun 8, 2022
2 parents a816e2e + a055203 commit f9d95d8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 47 deletions.
43 changes: 26 additions & 17 deletions Classes/Exception/DebugExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,46 @@
*/

use Flownative\Sentry\SentryClientTrait;
use Throwable;
use Neos\Flow\Log\ThrowableStorageInterface;

class DebugExceptionHandler extends \Neos\Flow\Error\DebugExceptionHandler
final class DebugExceptionHandler extends \Neos\Flow\Error\DebugExceptionHandler
{
use SentryClientTrait;

/**
* @param Throwable $exception
* Handles the given exception
*
* @param \Throwable $exception The exception object
* @return void
*/
public function echoExceptionWeb($exception): void
public function handleException($exception)
{
try {
if ($sentryClient = self::getSentryClient()) {
$sentryClient->captureThrowable($exception);
}
} catch (\Throwable $e) {
// Ignore if the error is suppressed by using the shut-up operator @
if (error_reporting() === 0) {
return;
}

$this->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;
}
parent::echoExceptionWeb($exception);
}

/**
* @param Throwable $exception
*/
public function echoExceptionCli(Throwable $exception): void
{
try {
if ($sentryClient = self::getSentryClient()) {
$sentryClient->captureThrowable($exception);
}
} catch (\Throwable $e) {
}
parent::echoExceptionCli($exception);

if (PHP_SAPI === 'cli') {
# Doesn't return:
$this->echoExceptionCli($exception, $exceptionWasLogged);
} else {
$this->echoExceptionWeb($exception);
}
}
}
56 changes: 34 additions & 22 deletions Classes/Exception/ProductionExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,50 @@
*/

use Flownative\Sentry\SentryClientTrait;
use Throwable;
use Neos\Flow\Log\ThrowableStorageInterface;

class ProductionExceptionHandler extends \Neos\Flow\Error\ProductionExceptionHandler
{
use SentryClientTrait;

/**
* @param Throwable $exception
* Handles the given exception
*
* @param \Throwable $exception The exception object
* @return void
*/
public function echoExceptionWeb($exception): void
public function handleException($exception)
{
try {
if ($sentryClient = self::getSentryClient()) {
$sentryClient->captureThrowable($exception);
}
} catch (\Throwable $e) {
// Ignore if the error is suppressed by using the shut-up operator @
if (error_reporting() === 0) {
return;
}
parent::echoExceptionWeb($exception);
}

/**
* @param Throwable $exception
*/
public function echoExceptionCli(Throwable $exception): void
{
try {
if ($sentryClient = self::getSentryClient()) {
$sentryClient->captureThrowable($exception);
}
} catch (\Throwable $e) {
$this->renderingOptions = $this->resolveCustomRenderingOptions($exception);

if ($this->throwableStorage instanceof ThrowableStorageInterface && isset($this->renderingOptions['logException']) && $this->renderingOptions['logException']) {
$message = $this->throwableStorage->logThrowable($exception);
$this->logger->critical($message);
}
parent::echoExceptionCli($exception);
}

switch (PHP_SAPI) {
case 'cli':
try {
if ($sentryClient = self::getSentryClient()) {
$sentryClient->captureThrowable($exception);
}
} catch (\Throwable $e) {
}
echo $this->buildView($exception, $this->renderingOptions)->render();
break;
default:
try {
if ($sentryClient = self::getSentryClient()) {
$sentryClient->captureThrowable($exception);
}
} catch (\Throwable $e) {
}
$this->echoExceptionWeb($exception);
}
}
}
17 changes: 10 additions & 7 deletions Classes/Log/SentryFileBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ class SentryFileBackend extends FileBackend
private $capturingMessage = false;

/**
* @param string $message
* @param int $severity
* @param null $additionalData
* @param null $packageKey
* @param null $className
* @param null $methodName
* Appends the given message along with the additional information into the log.
*
* @param string $message The message to log
* @param int $severity One of the LOG_* constants
* @param mixed $additionalData A variable containing more information about the event to be logged
* @param string|null $packageKey Key of the package triggering the log (determined automatically if not specified)
* @param string|null $className Name of the class triggering the log (determined automatically if not specified)
* @param string|null $methodName Name of the method triggering the log (determined automatically if not specified)
* @return void
* @api
*/
public function append($message, $severity = LOG_INFO, $additionalData = null, $packageKey = null, $className = null, $methodName = null): void
public function append(string $message, int $severity = LOG_INFO, $additionalData = null, string $packageKey = null, string $className = null, string $methodName = null): void
{
if ($this->capturingMessage) {
return;
Expand Down
2 changes: 1 addition & 1 deletion Classes/SentryClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function captureThrowable(Throwable $throwable, array $extraData = [], ar
$this->logger->log(
($captureException ? LogLevel::CRITICAL : LogLevel::NOTICE),
sprintf(
'Exception %s: %s (Ref: %s | Sentry: %s)',
'Exception #%s: %s (Ref: %s | Sentry: %s)',
$throwable->getCode(),
$throwable->getMessage(),
($throwable instanceof WithReferenceCodeInterface ? $throwable->getReferenceCode() : '-'),
Expand Down

0 comments on commit f9d95d8

Please sign in to comment.