diff --git a/README.md b/README.md index 1668408..d186574 100644 --- a/README.md +++ b/README.md @@ -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`): @@ -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 +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. diff --git a/composer.json b/composer.json index 5b0ad5d..a8e3998 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 067e4fe..4b46ff6 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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() ; diff --git a/src/DependencyInjection/HawkExtension.php b/src/DependencyInjection/HawkExtension.php index 78ee5af..73bb0ab 100644 --- a/src/DependencyInjection/HawkExtension.php +++ b/src/DependencyInjection/HawkExtension.php @@ -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; @@ -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 diff --git a/src/Service/BeforeSendServiceInterface.php b/src/Service/BeforeSendServiceInterface.php new file mode 100644 index 0000000..7f99d73 --- /dev/null +++ b/src/Service/BeforeSendServiceInterface.php @@ -0,0 +1,10 @@ +