diff --git a/app/Dto/EnvironmentLog.php b/app/Dto/EnvironmentLog.php index bdd0be71..b29c9da9 100644 --- a/app/Dto/EnvironmentLog.php +++ b/app/Dto/EnvironmentLog.php @@ -40,6 +40,11 @@ public function isExceptionLog(): bool return $this->type === LogType::EXCEPTION; } + public function isErrorLog(): bool + { + return $this->type === LogType::ERROR; + } + public function isSystemLog(): bool { return $this->type === LogType::SYSTEM; @@ -87,6 +92,23 @@ public function getExceptionData(): ?array 'code' => $this->data['code'] ?? null, 'file' => $this->data['file'] ?? null, 'trace' => $this->data['trace'] ?? null, + 'context' => $this->data['context'] ?? null, + 'extra' => $this->data['extra'] ?? null, + ]; + } + + public function getErrorData(): ?array + { + if (! $this->isErrorLog() || ! is_array($this->data)) { + return null; + } + + return [ + 'file' => $this->data['file'] ?? null, + 'trace' => $this->data['trace'] ?? null, + 'channel' => $this->data['channel'] ?? null, + 'context' => $this->data['context'] ?? null, + 'extra' => $this->data['extra'] ?? null, ]; } @@ -110,6 +132,10 @@ public function __toString(): string } } + if ($this->isErrorLog() && ($errorData = $this->getErrorData()) && $errorData['file']) { + $output .= " | {$errorData['file']}"; + } + return $output; } diff --git a/app/Enums/LogType.php b/app/Enums/LogType.php index 7d4131f1..0b9c1e1d 100644 --- a/app/Enums/LogType.php +++ b/app/Enums/LogType.php @@ -7,6 +7,7 @@ enum LogType: string case ACCESS = 'access'; case APPLICATION = 'application'; case EXCEPTION = 'exception'; + case ERROR = 'error'; case SYSTEM = 'system'; public function label(): string @@ -20,6 +21,7 @@ public function color(): string self::ACCESS => 'cyan', self::APPLICATION => 'green', self::EXCEPTION => 'red', + self::ERROR => 'red', self::SYSTEM => 'blue', }; } diff --git a/app/Prompts/EnvironmentLogsPromptRenderer.php b/app/Prompts/EnvironmentLogsPromptRenderer.php index a68b0b88..3c204cc8 100644 --- a/app/Prompts/EnvironmentLogsPromptRenderer.php +++ b/app/Prompts/EnvironmentLogsPromptRenderer.php @@ -116,6 +116,15 @@ protected function getBoxParams(EnvironmentLog $log): array return ['body' => $body, 'info' => $info]; } + if ($log->isErrorLog()) { + $errorData = $log->getErrorData() ?? []; + + return [ + 'body' => $this->getBody($log->message), + 'info' => $this->getFooter($errorData['channel'] ?? null, $errorData['file'] ?? null), + ]; + } + if ($log->isApplicationLog()) { $json = $this->tryToDecodeJson($log->message); diff --git a/tests/Feature/EnvironmentLogsTest.php b/tests/Feature/EnvironmentLogsTest.php index 6d3c65de..ea193e8c 100644 --- a/tests/Feature/EnvironmentLogsTest.php +++ b/tests/Feature/EnvironmentLogsTest.php @@ -5,6 +5,7 @@ use App\Client\Resources\Environments\ListEnvironmentLogsRequest; use App\Client\Resources\Meta\GetOrganizationRequest; use App\ConfigRepository; +use App\Dto\EnvironmentLog; use App\Git; use Illuminate\Support\Facades\Artisan; use Saloon\Http\Faking\MockClient; @@ -133,3 +134,48 @@ function runEnvironmentLogs(array $options = []): array expect($this->query)->toBeNull(); }); + +it('reads error logs and the context the API sends with exceptions', function () { + $entries = [ + [ + 'message' => 'Undefined variable $order', + 'level' => 'error', + 'type' => 'error', + 'logged_at' => '2026-10-15T12:00:00Z', + 'data' => [ + 'file' => '/var/www/html/app/Http/Controllers/CheckoutController.php:42', + 'trace' => ['#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54)'], + 'channel' => 'production', + 'context' => ['user_id' => 7], + 'extra' => null, + ], + ], + [ + 'message' => 'Call to a member function format() on null', + 'level' => 'error', + 'type' => 'exception', + 'logged_at' => '2026-10-15T12:00:01Z', + 'data' => [ + 'class' => 'Error', + 'code' => 0, + 'file' => '/var/www/html/app/Models/Order.php:18', + 'trace' => [], + 'context' => ['order_id' => 12], + 'extra' => ['url' => '/checkout'], + ], + ], + ]; + + mockEnvironmentLogs($entries, ''); + + $output = runEnvironmentLogs(); + + expect(collect($output['logs'])->pluck('type')->all())->toBe(['error', 'exception']) + ->and($output['logs'][0]['data']['file'])->toBe('/var/www/html/app/Http/Controllers/CheckoutController.php:42'); + + $logs = array_map(EnvironmentLog::createFromResponse(...), $entries); + + expect($logs[0]->isErrorLog())->toBeTrue() + ->and($logs[0]->getErrorData()['channel'])->toBe('production') + ->and($logs[1]->getExceptionData()['context'])->toBe(['order_id' => 12]); +});