Skip to content

Commit 4cb8548

Browse files
authored
Merge pull request #7 from codex-team/feature/laravel-before-send
Add `before_send_service` support in Laravel provider
2 parents 04a85d7 + f446862 commit 4cb8548

File tree

5 files changed

+201
-32
lines changed

5 files changed

+201
-32
lines changed

README.md

Lines changed: 159 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,47 @@
1-
**This project is currently in the testing phase and may undergo changes.**
2-
31
# Hawk Laravel
42

5-
Laravel errors Catcher for [Hawk.so](https://hawk.so).
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/codex-team/hawk.laravel.svg?style=flat-square)](https://packagist.org/packages/codex-team/hawk.laravel)
4+
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE)
5+
[![PHP Version](https://img.shields.io/packagist/php-v/codex-team/hawk.laravel?style=flat-square)](https://www.php.net/)
6+
7+
Laravel error catcher for [Hawk.so](https://hawk.so).
68

79
## Setup
810

9-
1. [Register](https://garage.hawk.so/sign-up) an account, create a Project and get an Integration Token.
11+
---
1012

11-
2. Install SDK via [composer](https://getcomposer.org) to install the Catcher
13+
1. [Register](https://garage.hawk.so/sign-up) an account, create a project, and get an **Integration Token**.
14+
2. Install the SDK via [Composer](https://getcomposer.org):
1215

13-
- Catcher provides support for PHP 7.2 or later
14-
- Your Laravel version needs to be 11.x or higher
16+
```bash
17+
composer require codex-team/hawk.laravel
18+
```
1519

16-
### Install command
20+
### Requirements
1721

18-
```bash
19-
$ composer require codex-team/hawk.laravel
20-
```
22+
- PHP **7.2+**
23+
- Laravel **11.x+**
24+
25+
## Features
26+
27+
---
2128

22-
To enable capturing unhandled exceptions for reporting to `Hawk`, modify your `bootstrap/app.php` file as follows:
29+
- 🦅 Automatic error catching
30+
- 💎 Manual exception and message sending
31+
- 🙂 Attaching user information
32+
- 📦 Attaching additional context
33+
- 🛡️ Sensitive data filtering
34+
- 🌟 BeforeSend hook for event preprocessing
35+
- 🗂️ Breadcrumbs collection (routes, queries, jobs, logs)
36+
- ⚡ Laravel 11+ support
37+
38+
## Configuration
39+
40+
---
41+
42+
### Enable exception capturing
43+
44+
Update your `bootstrap/app.php`:
2345

2446
```php
2547
<?php
@@ -39,12 +61,13 @@ return Application::configure(basePath: dirname(__DIR__))
3961
})
4062
->withExceptions(function (Exceptions $exceptions) {
4163
HawkBundle\Integration::handles($exceptions);
42-
})->create();
64+
})
65+
->create();
4366
```
4467

4568
### Register the Service Provider
4669

47-
To complete the setup, add the `Hawk` service provider to your `config/app.php` or `bootstrap/providers.php` file in the providers array:
70+
Add the `Hawk` service provider to your `config/app.php` or `bootstrap/providers.php`:
4871

4972
```php
5073
'providers' => [
@@ -53,34 +76,143 @@ To complete the setup, add the `Hawk` service provider to your `config/app.php`
5376
],
5477
```
5578

56-
### Configuration
79+
### Publish configuration
5780

58-
Set up Hawk using the following command:
81+
Run:
5982

6083
```bash
61-
$ php artisan hawkbundle:publish
84+
php artisan hawkbundle:publish
6285
```
6386

64-
This will create the configuration file (`config/hawk.php`), and you can manually add the `HAWK_TOKEN` in your `.env` file:
87+
This will create `config/hawk.php`.
88+
Then add your token in `.env`:
6589

6690
```env
67-
HAWK_TOKEN=<your integration token>
91+
HAWK_TOKEN=<your_integration_token>
92+
```
93+
94+
## Usage
95+
96+
---
97+
98+
### Adding User & Context Information
99+
100+
```php
101+
app(\HawkBundle\Catcher::class)->setUser([
102+
'name' => 'John Doe',
103+
'photo' => 'https://example.com/avatar.png',
104+
]);
105+
106+
app(\HawkBundle\Catcher::class)->setContext([
107+
'page' => 'checkout',
108+
'cart_id' => 123,
109+
]);
110+
```
111+
112+
### Sending Exceptions Manually
113+
114+
Inject `\HawkBundle\Catcher` and call `sendException`:
115+
116+
```php
117+
use HawkBundle\Catcher;
118+
119+
class TestController extends Controller
120+
{
121+
private Catcher $catcher;
122+
123+
public function __construct(Catcher $catcher)
124+
{
125+
$this->catcher = $catcher;
126+
}
127+
128+
public function test()
129+
{
130+
try {
131+
// Code that may fail
132+
} catch (\Exception $e) {
133+
$this->catcher->sendException($e);
134+
}
135+
}
136+
}
68137
```
69138

70-
## Issues and improvements
139+
### Sending Custom Messages
71140

72-
Feel free to ask questions or improve the project.
141+
```php
142+
app(\HawkBundle\Catcher::class)->sendMessage(
143+
'Checkout started',
144+
[
145+
'cart_id' => 123,
146+
'step' => 'payment',
147+
]
148+
);
149+
```
150+
151+
## BeforeSend Hook
152+
153+
---
154+
155+
If you want to modify or filter errors before they are sent to Hawk,
156+
implement the `BeforeSendServiceInterface`.
157+
158+
Example:
159+
160+
```php
161+
<?php
162+
163+
namespace App\Hawk;
164+
165+
use Hawk\EventPayload;
166+
use HawkBundle\Services\BeforeSendServiceInterface;
73167

74-
## Links
168+
class BeforeSendService implements BeforeSendServiceInterface
169+
{
170+
public function __invoke(EventPayload $eventPayload): ?EventPayload
171+
{
172+
$user = $eventPayload->getUser();
75173

76-
Repository: https://github.com/codex-team/hawk.laravel
174+
// Remove sensitive data
175+
if (!empty($user['email'])) {
176+
unset($user['email']);
177+
$eventPayload->setUser($user);
178+
}
77179

78-
Report a bug: https://github.com/codex-team/hawk.laravel/issues
180+
// Skip sending in specific cases
181+
if ($eventPayload->getContext()['skip_sending'] ?? false) {
182+
return null;
183+
}
79184

80-
Composer Package: https://packagist.org/packages/codex-team/hawk.laravel
185+
return $eventPayload;
186+
}
187+
}
188+
```
189+
190+
Register it in `config/hawk.php`:
191+
192+
```php
193+
return [
194+
'integration_token' => env('HAWK_TOKEN'),
195+
'before_send_service' => \App\Hawk\BeforeSendService::class,
196+
];
197+
```
198+
199+
## Issues & Contributions
81200

82-
CodeX Team: https://codex.so
201+
---
202+
203+
- Found a bug? [Open an issue](https://github.com/codex-team/hawk.laravel/issues)
204+
- Want to improve the package? Pull requests are welcome!
205+
206+
## Useful Links
207+
208+
---
209+
210+
- **Repository:** [github.com/codex-team/hawk.laravel](https://github.com/codex-team/hawk.laravel)
211+
- **Composer Package:** [packagist.org/packages/codex-team/hawk.laravel](https://packagist.org/packages/codex-team/hawk.laravel)
212+
- **CodeX Team:** [codex.so](https://codex.so)
83213

84214
## License
85215

86-
MIT
216+
---
217+
218+
[MIT](LICENSE)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Laravel errors Catcher module for Hawk.so",
44
"keywords": ["hawk", "php", "error", "catcher", "monolog", "laravel"],
55
"type": "library",
6-
"version": "0.1.0",
6+
"version": "0.1.1",
77
"license": "MIT",
88
"require": {
99
"php": "^7.2 || ^8.0",

config/hawk.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

33
return [
4-
'integration_token' => env('HAWK_TOKEN', '')
4+
'integration_token' => env('HAWK_TOKEN', ''),
5+
'before_send_service' => null
56
];

src/ErrorLoggerServiceProvider.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use HawkBundle\Console\Commands\PublishHawkConfig;
88
use HawkBundle\Handlers\ErrorHandler;
9+
use HawkBundle\Services\BeforeSendServiceInterface;
910
use HawkBundle\Services\BreadcrumbsCollector;
1011
use HawkBundle\Services\DataFilter;
1112
use HawkBundle\Services\ErrorLoggerService;
@@ -16,6 +17,7 @@
1617
use Illuminate\Queue\Events\JobProcessing;
1718
use Illuminate\Queue\Events\JobFailed;
1819
use Illuminate\Log\Events\MessageLogged;
20+
use InvalidArgumentException;
1921

2022
class ErrorLoggerServiceProvider extends ServiceProvider
2123
{
@@ -49,9 +51,31 @@ public function boot()
4951
$this->app->singleton(Catcher::class, function ($app) {
5052
$breadcrumbsCollector = $app->make(BreadcrumbsCollector::class);
5153

52-
return Catcher::init([
53-
'integrationToken' => config('hawk.integration_token') ?: ''
54-
], $breadcrumbsCollector);
54+
$options = [
55+
'integrationToken' => config('hawk.integration_token') ?: '',
56+
];
57+
58+
$beforeSendService = config('hawk.before_send_service');
59+
if (!empty($beforeSendService)) {
60+
if (!class_exists($beforeSendService)) {
61+
throw new InvalidArgumentException(sprintf(
62+
'The before_send_service class "%s" does not exist.',
63+
$beforeSendService
64+
));
65+
}
66+
67+
if (!is_subclass_of($beforeSendService, BeforeSendServiceInterface::class)) {
68+
throw new InvalidArgumentException(sprintf(
69+
'The service "%s" must implement "%s".',
70+
$beforeSendService,
71+
BeforeSendServiceInterface::class
72+
));
73+
}
74+
75+
$options['before_send'] = $app->make($beforeSendService);
76+
}
77+
78+
return Catcher::init($options, $breadcrumbsCollector);
5579
});
5680

5781
$this->app->singleton('Illuminate\Contracts\Debug\ExceptionHandler', function ($app) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HawkBundle\Services;
6+
7+
use Hawk\EventPayload;
8+
9+
interface BeforeSendServiceInterface
10+
{
11+
public function __invoke(EventPayload $eventPayload): ?EventPayload;
12+
}

0 commit comments

Comments
 (0)