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 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 +{ + // +}