From e08b64abe564747320cc2c0eec419aaf0ac8291b Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 23 Nov 2023 14:45:16 +0000 Subject: [PATCH] [1.x] Fixes exception as string (#22) * Fixes exception as string * Fixes static analysis --- src/Handler.php | 9 +++++---- tests/Features/CommandTest.php | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Handler.php b/src/Handler.php index 90d3f04..cf412ba 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -8,6 +8,7 @@ use Illuminate\Queue\Events\JobExceptionOccurred; use Illuminate\Queue\Events\JobProcessing; use Illuminate\Support\Facades\Auth; +use Throwable; class Handler { @@ -99,10 +100,10 @@ protected function context(MessageLogged $messageLogged): array } $context['__pail']['origin']['trace'] = isset($messageLogged->context['exception']) - ? collect($messageLogged->context['exception']->getTrace()) // @phpstan-ignore-line - ->filter(fn (array $frame) => isset($frame['file'])) // @phpstan-ignore-line - ->map(fn (array $frame) => [ // @phpstan-ignore-line - 'file' => $frame['file'], + && $messageLogged->context['exception'] instanceof Throwable ? collect($messageLogged->context['exception']->getTrace()) + ->filter(fn (array $frame) => isset($frame['file'])) + ->map(fn (array $frame) => [ + 'file' => $frame['file'], // @phpstan-ignore-line 'line' => $frame['line'] ?? null, ])->values() : null; diff --git a/tests/Features/CommandTest.php b/tests/Features/CommandTest.php index d1ad911..b61327c 100644 --- a/tests/Features/CommandTest.php +++ b/tests/Features/CommandTest.php @@ -174,3 +174,22 @@ EOF, verbose: true); }); + +test('exception key as string', function () { + expect([ + 'Log::error("log message", ["exception" => "an exception occured"])', + 'throw new Exception("my exception message")', + ])->toPail(<<<'EOF' + ┌ 2024-01-01 03:04:05 ERROR ────────────────────── + │ log message + │ 1. app/MyClass.php:12 + │ 2. app/MyClass.php:34 + └──────────────────────────────────────── artisan + ┌ 2024-01-01 03:04:05 Exception app/MyClass.php:12 + │ my exception message + │ 1. app/MyClass.php:12 + │ 2. app/MyClass.php:34 + └──────────────────────────────────────── artisan + + EOF, verbose: true); +});