From 4df76c4ff1eae5568a34d55504094ebe61727b36 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 9 Jun 2024 11:17:10 +0200 Subject: [PATCH] Render exceptions in developer console --- src/main/php/xp/web/dev/Console.class.php | 16 ++++++--- .../web/unittest/server/ConsoleTest.class.php | 35 ++++++++++++++++++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/main/php/xp/web/dev/Console.class.php b/src/main/php/xp/web/dev/Console.class.php index 0b0088a..6763cc6 100755 --- a/src/main/php/xp/web/dev/Console.class.php +++ b/src/main/php/xp/web/dev/Console.class.php @@ -1,6 +1,8 @@ proceed($req, $res->streaming(function($res, $length) use($capture) { return $capture->length($length); })); - } finally { - $capture->end($res); $debug= ob_get_clean(); if (0 === strlen($debug)) return $capture->drain($res); + } catch (Any $e) { + $res->answer($e instanceof Error ? $e->status() : 500); + $debug= ob_get_clean()."\n".Throwable::wrap($e)->toString(); + } finally { + $capture->end($res); } $console= sprintf( @@ -67,7 +72,10 @@ public function filter($req, $res, $invocation) { ); $target= $res->output()->stream(strlen($console)); try { - $target->begin(200, 'Debug', ['Content-Type' => ['text/html; charset=utf-8']]); + $target->begin(200, 'Debug', [ + 'Content-Type' => ['text/html; charset='.\xp::ENCODING], + 'Cache-Control' => ['no-cache, no-store'], + ]); $target->write($console); } finally { $target->close(); diff --git a/src/test/php/web/unittest/server/ConsoleTest.class.php b/src/test/php/web/unittest/server/ConsoleTest.class.php index f216b9e..f85c499 100755 --- a/src/test/php/web/unittest/server/ConsoleTest.class.php +++ b/src/test/php/web/unittest/server/ConsoleTest.class.php @@ -1,8 +1,9 @@ output()->bytes() ); } + + #[Test] + public function uncaught_exceptions() { + $res= $this->handle(function($req, $res) { + throw new IllegalArgumentException('Test'); + }); + + Assert::matches( + '/HTTP\/1.1 500 Internal Server Error<\/span>/', + $res->output()->bytes() + ); + Assert::matches( + '/Exception lang.IllegalArgumentException \(Test\)/', + $res->output()->bytes() + ); + } + + #[Test] + public function uncaught_errors() { + $res= $this->handle(function($req, $res) { + throw new Error(404); + }); + + Assert::matches( + '/HTTP\/1.1 404 Not Found<\/span>/', + $res->output()->bytes() + ); + Assert::matches( + '/Error web.Error\(#404: Not Found\)/', + $res->output()->bytes() + ); + } } \ No newline at end of file