From 6dfe73ec9e6255627af7fcc50d40e0dcd74b8136 Mon Sep 17 00:00:00 2001 From: Pavel Zotikov Date: Tue, 19 Nov 2024 16:28:15 +0300 Subject: [PATCH 1/4] Add support for custom beforeSend service in HawkBundle configuration --- src/DependencyInjection/Configuration.php | 11 +++++++---- src/DependencyInjection/HawkExtension.php | 13 ++++++++++++- src/Service/BeforeSendServiceInterface.php | 10 ++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/Service/BeforeSendServiceInterface.php 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 @@ + Date: Tue, 19 Nov 2024 16:28:42 +0300 Subject: [PATCH 2/4] update version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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", From 6d6cce3cadfaecd19d5d999a00b6b764964b70d6 Mon Sep 17 00:00:00 2001 From: Pavel Zotikov Date: Tue, 19 Nov 2024 16:45:05 +0300 Subject: [PATCH 3/4] Update README.md --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 1668408..6d24d41 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,32 @@ public function test() } ``` +### Example: BeforeSendService Class +```php +getUser(); + + if (!empty($user['email'])){ + unset($user['email']); + + $eventPayload->setUser($user); + } + + return $eventPayload; + } +} +``` + ## Issues and improvements Feel free to ask questions or improve the project. From 664d3a4711dc350e49f6b5c9b399b5f5712e3f62 Mon Sep 17 00:00:00 2001 From: Pavel Zotikov Date: Tue, 19 Nov 2024 18:55:47 +0300 Subject: [PATCH 4/4] Update README.md --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6d24d41..d186574 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,9 @@ 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; }