Skip to content

Commit

Permalink
Merge pull request #20 from flownative/with-additional-data
Browse files Browse the repository at this point in the history
Let exceptions provide additional data
  • Loading branch information
robertlemke authored Feb 21, 2023
2 parents cd490f6 + 6500101 commit 5c63a1f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Classes/Context/WithExtraDataInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace Flownative\Sentry\Context;

/*
* This file is part of the Flownative.Sentry package.
*
* (c) Robert Lemke, Flownative GmbH - www.flownative.com
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

interface WithExtraDataInterface
{
/**
* Returns an array with extra data to communicate to Sentry when capturing an exception.
*
* This is supposed to be implemented by a user-defined exception.
*/
public function getExtraData(): array;
}
6 changes: 6 additions & 0 deletions Classes/SentryClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Flownative\Sentry\Context\UserContext;
use Flownative\Sentry\Context\UserContextServiceInterface;
use Flownative\Sentry\Context\WithExtraDataInterface;
use GuzzleHttp\Psr7\ServerRequest;
use Jenssegers\Agent\Agent;
use Neos\Flow\Annotations as Flow;
Expand All @@ -27,6 +28,7 @@
use Neos\Flow\Session\Session;
use Neos\Flow\Session\SessionManagerInterface;
use Neos\Flow\Utility\Environment;
use Neos\Utility\Arrays;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Sentry\Event;
Expand Down Expand Up @@ -187,6 +189,10 @@ public function captureThrowable(Throwable $throwable, array $extraData = [], ar
if ($throwable instanceof WithReferenceCodeInterface) {
$extraData['Reference Code'] = $throwable->getReferenceCode();
}
if ($throwable instanceof WithExtraDataInterface) {
$extraData = Arrays::arrayMergeRecursiveOverrule($extraData, $throwable->getExtraData());
}

$extraData['PHP Process Inode'] = getmyinode();
$extraData['PHP Process PID'] = getmypid();
$extraData['PHP Process UID'] = getmyuid();
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ similar to the following message:
… NOTICE Exception 12345: The exception message (Ref: 202004161706040c28ae | Sentry: ignored)
```
## Additional Data
Exceptions declared in an application can optionally implement
`WithAdditionalDataInterface` provided by this package. If they do, the
array returned by `getAdditionalData()` will be visible in the "additional
data" section in Sentry.

Note that the array must only contain values of simple types, such as
strings, booleans or integers.

## Testing the Client

This package provides a command controller which allows you to log a
Expand Down

0 comments on commit 5c63a1f

Please sign in to comment.