Skip to content

Commit

Permalink
Merge pull request #142 from spatie/add-exception-payload
Browse files Browse the repository at this point in the history
Add exception watcher
  • Loading branch information
freekmurze authored Feb 22, 2021
2 parents 9774088 + f08dd89 commit c7fbdda
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Watchers/ExceptionWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
namespace Spatie\LaravelRay\Watchers;

use Exception;
use Facade\FlareClient\Flare;
use Facade\FlareClient\Truncation\ReportTrimmer;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Support\Facades\Event;
use Spatie\LaravelRay\Ray;
use Throwable;

class ExceptionWatcher extends Watcher
{
public function register(): void
{
$this->enable();

Event::listen(MessageLogged::class, function (MessageLogged $message) {
if (! $this->enabled()) {
return;
Expand All @@ -22,10 +27,16 @@ public function register(): void

$exception = $message->context['exception'];

$meta = [];

if ($flareReport = $this->getFlareReport($exception)) {
$meta['flare_report'] = $flareReport;
}

/** @var Ray $ray */
$ray = app(Ray::class);

$ray->exception($exception);
$ray->exception($exception, $meta);
});
}

Expand All @@ -41,4 +52,18 @@ public function concernsException(MessageLogged $messageLogged): bool

return true;
}

public function getFlareReport(Throwable $exception): ?array
{
if (! class_exists(Flare::class)) {
return null;
}

/** @var \Facade\FlareClient\Flare $flare */
$flare = app(Flare::class);

$report = $flare->createReport($exception);

return (new ReportTrimmer())->trim($report->toArray());
}
}

0 comments on commit c7fbdda

Please sign in to comment.