Skip to content

Commit

Permalink
Merge pull request #9 from codex-team/feature/add-before-send-service…
Browse files Browse the repository at this point in the history
…-support

Add support for beforeSend service configuration
  • Loading branch information
pavelzotikov authored Nov 20, 2024
2 parents ae13aaf + 664d3a4 commit 26addcd
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Create a configuration file at `config/packages/hawk.yaml` with the following co
```yaml
hawk:
integration_token: '%env(HAWK_TOKEN)%'

# Optional: Configure a custom beforeSend service
before_send_service: 'App\Hawk\BeforeSendService'
```
In the `config/packages/monolog.yaml` file, specify the handler settings under the appropriate section (`dev` or `prod`):
Expand Down Expand Up @@ -91,6 +94,41 @@ public function test()
}
```

### Example: BeforeSendService Class

If you want to process or modify error data before sending it to Hawk, you can define a custom service by implementing the `BeforeSendServiceInterface`.

```php
<?php
namespace App\Hawk;
use Hawk\EventPayload;
use HawkBundle\Service\BeforeSendServiceInterface;
class BeforeSendService implements BeforeSendServiceInterface
{
public function __invoke(EventPayload $eventPayload): ?EventPayload
{
$user = $eventPayload->getUser();
// Modify or add additional data to the error report
if (!empty($user['email'])){
unset($user['email']);
$eventPayload->setUser($user);
}
// Return null to prevent the event from being sent to Hawk
if ($eventPayload->getContext()['skip_sending'] ?? false) {
return null;
}
return $eventPayload;
}
}
```

## Issues and improvements

Feel free to ask questions or improve the project.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Symfony errors Catcher module for Hawk.so",
"keywords": ["hawk", "php", "error", "catcher", "monolog", "symfony"],
"type": "library",
"version": "0.0.7",
"version": "0.0.8",
"license": "MIT",
"require": {
"php": "^7.2 || ^8.0",
Expand Down
11 changes: 7 additions & 4 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public function getConfigTreeBuilder(): TreeBuilder

$rootNode
->children()
->scalarNode('integration_token')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('integration_token')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('before_send_service')
->defaultNull()
->end()
->end()
;

Expand Down
13 changes: 12 additions & 1 deletion src/DependencyInjection/HawkExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use HawkBundle\Catcher;
use HawkBundle\Monolog\Handler;
use HawkBundle\Service\BeforeSendServiceInterface;
use HawkBundle\Transport\GuzzlePromisesTransport;
use Monolog\Logger;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -34,9 +35,19 @@ public function load(array $configs, ContainerBuilder $container)
// Register TransportInterface
$container->register(GuzzlePromisesTransport::class);

$options = ['integrationToken' => $config['integration_token']];

if (
isset($config['before_send_service'])
&& class_exists($config['before_send_service'])
&& is_subclass_of($config['before_send_service'], BeforeSendServiceInterface::class)
) {
$options['beforeSend'] = new Reference($config['before_send_service']);
}

// Register Catcher
$container->register(Catcher::class)
->setArgument('$options', ['integrationToken' => $config['integration_token']])
->setArgument('$options', $options)
->setArgument('$transport', new Reference(GuzzlePromisesTransport::class));

// Register Monolog\Handler
Expand Down
10 changes: 10 additions & 0 deletions src/Service/BeforeSendServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace HawkBundle\Service;

interface BeforeSendServiceInterface
{

}

0 comments on commit 26addcd

Please sign in to comment.