diff --git a/Classes/ExceptionHandler/ExceptionRenderingOptionsResolver.php b/Classes/ExceptionHandler/ExceptionRenderingOptionsResolver.php index 8ee601b..57e46be 100644 --- a/Classes/ExceptionHandler/ExceptionRenderingOptionsResolver.php +++ b/Classes/ExceptionHandler/ExceptionRenderingOptionsResolver.php @@ -15,6 +15,12 @@ final class ExceptionRenderingOptionsResolver extends AbstractExceptionHandler { + /** + * @var array + * @Flow\InjectConfiguration(package="Neos.Flow", path="error.exceptionHandler") + */ + protected $options; + /** @noinspection PhpMissingParentConstructorInspection */ public function __construct() { @@ -25,6 +31,28 @@ public function resolveRenderingOptionsForThrowable(Throwable $throwable): array return $this->resolveCustomRenderingOptions($throwable); } + protected function resolveRenderingGroup(\Throwable $exception) + { + if (!isset($this->options['renderingGroups'])) { + return null; + } + $renderingGroup = parent::resolveRenderingGroup($exception); + if ($renderingGroup === null) { + // try to match using exception code + foreach ($this->options['renderingGroups'] as $renderingGroupName => $renderingGroupSettings) { + if (isset($renderingGroupSettings['matchingExceptionCodes'])) { + foreach ($renderingGroupSettings['matchingExceptionCodes'] as $exceptionCode) { + if ($exception->getCode() === $exceptionCode) { + return $renderingGroupName; + } + } + } + } + } + + return $renderingGroup; + } + /** * @param Throwable $exception * @internal diff --git a/README.md b/README.md index 0dcfea5..7e1af1e 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,9 @@ Neos: ignoredExceptions: matchingStatusCodes: [ 418 ] matchingExceptionClassNames: [ 'Your\Ignored\Exception' ] + # It is also possible to match against \Throwable::getCode(). Please note that this is not a Flow feature. + # Check \Netlogix\Sentry\ExceptionHandler\ExceptionRenderingOptionsResolver::resolveRenderingGroup() for more info + # matchingExceptionCodes: [1638880375] options: logException: false ``` diff --git a/Tests/Unit/ExceptionHandler/ExceptionRenderingOptionsResolverTest.php b/Tests/Unit/ExceptionHandler/ExceptionRenderingOptionsResolverTest.php new file mode 100644 index 0000000..f0f8718 --- /dev/null +++ b/Tests/Unit/ExceptionHandler/ExceptionRenderingOptionsResolverTest.php @@ -0,0 +1,61 @@ +resolveRenderingOptionsForThrowable(self::createThrowable()); + + self::assertEmpty($result); + } + + /** + * @test + */ + public function Exception_Code_is_used_to_match_rendering_Groups(): void + { + $resolver = new ExceptionRenderingOptionsResolver(); + + $resolver->setOptions([ + 'renderingGroups' => [ + 'someGroup' => [ + 'matchingExceptionCodes' => [self::code()], + 'options' => [ + 'foo' => 'bar' + ] + ] + ] + ]); + + $result = $resolver->resolveRenderingOptionsForThrowable(self::createThrowable()); + + self::assertArrayHasKey('renderingGroup', $result); + self::assertEquals('someGroup', $result['renderingGroup']); + + self::assertArrayHasKey('foo', $result); + self::assertEquals('bar', $result['foo']); + } + + private static function createThrowable(): \Throwable + { + return new \Exception('foo', self::code()); + } + + private static function code(): int + { + return 1638882641; + } + +}