From 6adf982f6d43fc86b21366881e823f404b27807e Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Tue, 4 Jun 2024 21:29:30 +0800 Subject: [PATCH] [11.x] Avoid using Laravel new error page if `app.debug` changes to `true` at runtime (#51705) * [11.x] Avoid using Laravel new renderer if `app.debug` changes to `true` at runtime Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki --------- Signed-off-by: Mior Muhammad Zaki --- .../Foundation/Exceptions/Handler.php | 8 ++-- .../Foundation/Exceptions/RendererTest.php | 40 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 tests/Integration/Foundation/Exceptions/RendererTest.php diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index 0dd49e7f9e1e..057d7af429d6 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -833,9 +833,11 @@ protected function renderExceptionContent(Throwable $e) { try { if (config('app.debug')) { - return app()->has(ExceptionRenderer::class) - ? $this->renderExceptionWithCustomRenderer($e) - : $this->container->make(Renderer::class)->render(request(), $e); + if (app()->has(ExceptionRenderer::class)) { + return $this->renderExceptionWithCustomRenderer($e); + } elseif ($this->container->bound(Renderer::class)) { + return $this->container->make(Renderer::class)->render(request(), $e); + } } return $this->renderExceptionWithSymfony($e, config('app.debug')); diff --git a/tests/Integration/Foundation/Exceptions/RendererTest.php b/tests/Integration/Foundation/Exceptions/RendererTest.php new file mode 100644 index 000000000000..0b63869eb83f --- /dev/null +++ b/tests/Integration/Foundation/Exceptions/RendererTest.php @@ -0,0 +1,40 @@ +get('failed', fn () => throw new RuntimeException('Bad route!')); + } + + #[WithConfig('app.debug', true)] + public function testItCanRenderExceptionPage() + { + $this->assertTrue($this->app->bound(Renderer::class)); + + $this->get('/failed') + ->assertInternalServerError() + ->assertSee('RuntimeException') + ->assertSee('Bad route!'); + } + + #[WithConfig('app.debug', false)] + public function testItCanRenderExceptionPageUsingSymfonyIfRendererIsNotDefined() + { + config(['app.debug' => true]); + + $this->assertFalse($this->app->bound(Renderer::class)); + + $this->get('/failed') + ->assertInternalServerError() + ->assertSee('RuntimeException') + ->assertSee('Bad route!'); + } +}