Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ parameters:
paths:
- src
level: 8
reportUnmatchedIgnoredErrors: false
includes:
- phpstan-baseline.neon
16 changes: 16 additions & 0 deletions src/Events/ExceptionReported.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Laravel\Pulse\Events;

use Throwable;

class ExceptionReported
{
/**
* Create a new event instance.
*/
public function __construct(public Throwable $exception)
{
//
}
}
12 changes: 12 additions & 0 deletions src/Pulse.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Lottery;
use Laravel\Pulse\Contracts\Ingest;
use Laravel\Pulse\Events\ExceptionReported;
use Laravel\Pulse\Recorders\Concerns\ConfiguresAfterResolving;
use Laravel\Pulse\Recorders\Exceptions;
use RuntimeException;
use Symfony\Component\HttpFoundation\Response;
use Throwable;
Expand Down Expand Up @@ -146,6 +148,16 @@ public function record(Entry|Update $entry): self
return $this;
}

/**
* Report the throwable exception to Pulse.
*/
public function report(Throwable $e): self
{
$this->app['events']->dispatch(new ExceptionReported($e));

return $this;
}

/**
* Start recording entries.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Recorders/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)));
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/Feature/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
//
}