From c712354eaa18148e64e2c57b78882896e353662a Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 5 Oct 2023 14:54:14 +1100 Subject: [PATCH 1/2] Allow exceptions to be manually reported to Pulse --- src/Events/ExceptionReported.php | 16 ++++++++++++++++ src/Pulse.php | 12 ++++++++++++ src/Recorders/Exceptions.php | 4 ++++ tests/Feature/ExceptionsTest.php | 18 ++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 src/Events/ExceptionReported.php diff --git a/src/Events/ExceptionReported.php b/src/Events/ExceptionReported.php new file mode 100644 index 00000000..57f80a2f --- /dev/null +++ b/src/Events/ExceptionReported.php @@ -0,0 +1,16 @@ +app['events']->dispatch(new ExceptionReported($e)); + + return $this; + } + /** * Start recording entries. */ diff --git a/src/Recorders/Exceptions.php b/src/Recorders/Exceptions.php index 370aecfb..c4af3886 100644 --- a/src/Recorders/Exceptions.php +++ b/src/Recorders/Exceptions.php @@ -4,9 +4,11 @@ use Carbon\CarbonImmutable; use Illuminate\Contracts\Debug\ExceptionHandler; +use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Foundation\Application; use Illuminate\Support\Str; use Laravel\Pulse\Entry; +use Laravel\Pulse\Events\ExceptionReported; use Laravel\Pulse\Pulse; use Throwable; @@ -37,6 +39,8 @@ public function __construct( public function register(callable $record, Application $app): void { $this->afterResolving($app, ExceptionHandler::class, fn (ExceptionHandler $handler) => $handler->reportable(fn (Throwable $e) => $record($e))); + + $this->afterResolving($app, Dispatcher::class, fn (Dispatcher $events) => $events->listen(fn (ExceptionReported $event) => $record($event->exception))); } /** diff --git a/tests/Feature/ExceptionsTest.php b/tests/Feature/ExceptionsTest.php index d15068bf..008daadc 100644 --- a/tests/Feature/ExceptionsTest.php +++ b/tests/Feature/ExceptionsTest.php @@ -133,3 +133,21 @@ public function hasUser() expect($exceptions[1]->user_id)->toBe('567'); expect($exceptions[2]->user_id)->toBe('789'); }); + +it('can manually report exceptions', function () { + Carbon::setTestNow('2000-01-01 00:00:00'); + + Pulse::report(new MyReportedException('Hello, Pulse!')); + Pulse::store(app(Ingest::class)); + + $exceptions = Pulse::ignore(fn () => DB::table('pulse_exceptions')->get()); + + expect($exceptions)->toHaveCount(1); + expect($exceptions[0]->date)->toBe('2000-01-01 00:00:00'); + expect($exceptions[0]->class)->toBe('MyReportedException'); +}); + +class MyReportedException extends Exception +{ + // +} From 5b6adee8850846010440c38ac2aeb7ffc8b47386 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 5 Oct 2023 17:09:00 +1100 Subject: [PATCH 2/2] wip --- phpstan.neon.dist | 1 + 1 file changed, 1 insertion(+) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 06bee27c..74b09c3c 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,5 +2,6 @@ parameters: paths: - src level: 8 + reportUnmatchedIgnoredErrors: false includes: - phpstan-baseline.neon