Skip to content

Commit 8248601

Browse files
authored
Allow exceptions to be manually reported to Pulse (#21)
* Allow exceptions to be manually reported to Pulse * wip
1 parent a4fcba3 commit 8248601

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ parameters:
22
paths:
33
- src
44
level: 8
5+
reportUnmatchedIgnoredErrors: false
56
includes:
67
- phpstan-baseline.neon

src/Events/ExceptionReported.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Laravel\Pulse\Events;
4+
5+
use Throwable;
6+
7+
class ExceptionReported
8+
{
9+
/**
10+
* Create a new event instance.
11+
*/
12+
public function __construct(public Throwable $exception)
13+
{
14+
//
15+
}
16+
}

src/Pulse.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Illuminate\Support\Collection;
1212
use Illuminate\Support\Lottery;
1313
use Laravel\Pulse\Contracts\Ingest;
14+
use Laravel\Pulse\Events\ExceptionReported;
1415
use Laravel\Pulse\Recorders\Concerns\ConfiguresAfterResolving;
16+
use Laravel\Pulse\Recorders\Exceptions;
1517
use RuntimeException;
1618
use Symfony\Component\HttpFoundation\Response;
1719
use Throwable;
@@ -146,6 +148,16 @@ public function record(Entry|Update $entry): self
146148
return $this;
147149
}
148150

151+
/**
152+
* Report the throwable exception to Pulse.
153+
*/
154+
public function report(Throwable $e): self
155+
{
156+
$this->app['events']->dispatch(new ExceptionReported($e));
157+
158+
return $this;
159+
}
160+
149161
/**
150162
* Start recording entries.
151163
*/

src/Recorders/Exceptions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use Carbon\CarbonImmutable;
66
use Illuminate\Contracts\Debug\ExceptionHandler;
7+
use Illuminate\Contracts\Events\Dispatcher;
78
use Illuminate\Foundation\Application;
89
use Illuminate\Support\Str;
910
use Laravel\Pulse\Entry;
11+
use Laravel\Pulse\Events\ExceptionReported;
1012
use Laravel\Pulse\Pulse;
1113
use Throwable;
1214

@@ -37,6 +39,8 @@ public function __construct(
3739
public function register(callable $record, Application $app): void
3840
{
3941
$this->afterResolving($app, ExceptionHandler::class, fn (ExceptionHandler $handler) => $handler->reportable(fn (Throwable $e) => $record($e)));
42+
43+
$this->afterResolving($app, Dispatcher::class, fn (Dispatcher $events) => $events->listen(fn (ExceptionReported $event) => $record($event->exception)));
4044
}
4145

4246
/**

tests/Feature/ExceptionsTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,21 @@ public function hasUser()
133133
expect($exceptions[1]->user_id)->toBe('567');
134134
expect($exceptions[2]->user_id)->toBe('789');
135135
});
136+
137+
it('can manually report exceptions', function () {
138+
Carbon::setTestNow('2000-01-01 00:00:00');
139+
140+
Pulse::report(new MyReportedException('Hello, Pulse!'));
141+
Pulse::store(app(Ingest::class));
142+
143+
$exceptions = Pulse::ignore(fn () => DB::table('pulse_exceptions')->get());
144+
145+
expect($exceptions)->toHaveCount(1);
146+
expect($exceptions[0]->date)->toBe('2000-01-01 00:00:00');
147+
expect($exceptions[0]->class)->toBe('MyReportedException');
148+
});
149+
150+
class MyReportedException extends Exception
151+
{
152+
//
153+
}

0 commit comments

Comments
 (0)