Skip to content

Commit

Permalink
Merge pull request #18 from netlogix/feature/rendering-group-by-code
Browse files Browse the repository at this point in the history
FEATURE: Allow mapping of renderingGroup by Exception Code
  • Loading branch information
paxuclus authored Dec 7, 2021
2 parents 22cb8e5 + cb0915a commit bebab1c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Classes/ExceptionHandler/ExceptionRenderingOptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);

namespace Netlogix\Sentry\Tests\Unit\ExceptionHandler;

use Neos\Flow\Tests\UnitTestCase;
use Netlogix\Sentry\ExceptionHandler\ExceptionRenderingOptionsResolver;

class ExceptionRenderingOptionsResolverTest extends UnitTestCase
{

/**
* @test
*/
public function If_no_renderingGroups_have_been_defined_an_empty_array_is_returned(): void
{
$resolver = new ExceptionRenderingOptionsResolver();

$result = $resolver->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;
}

}

0 comments on commit bebab1c

Please sign in to comment.