From 812c69f994f7376a23adc0a9373d268299343a49 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 25 Sep 2025 13:55:25 +0530 Subject: [PATCH 1/2] chore: update error handler had an error to print stacktrace --- src/App.php | 10 ++++----- tests/AppTest.php | 53 ++++++++++++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/App.php b/src/App.php index d041bc3..1302d35 100755 --- a/src/App.php +++ b/src/App.php @@ -642,7 +642,7 @@ public function execute(Route $route, Request $request, Response $response): sta $arguments = $this->getArguments($error, $pathValues, $request->getParams()); \call_user_func_array($error->getAction(), $arguments); } catch (\Throwable $e) { - throw new Exception('Error handler had an error: ' . $e->getMessage(), 500, $e); + throw new Exception('Error handler had an error: ' . $e->getMessage() . "\nStack trace: " . $e->getTraceAsString(), 500, $e); } } } @@ -654,7 +654,7 @@ public function execute(Route $route, Request $request, Response $response): sta $arguments = $this->getArguments($error, $pathValues, $request->getParams()); \call_user_func_array($error->getAction(), $arguments); } catch (\Throwable $e) { - throw new Exception('Error handler had an error: ' . $e->getMessage(), 500, $e); + throw new Exception('Error handler had an error: ' . $e->getMessage() . "\nStack trace: " . $e->getTraceAsString(), 500, $e); } } } @@ -811,7 +811,7 @@ private function runInternal(Request $request, Response $response): static $arguments = $this->getArguments($error, [], $request->getParams()); \call_user_func_array($error->getAction(), $arguments); } catch (\Throwable $e) { - throw new Exception('Error handler had an error: ' . $e->getMessage(), 500, $e); + throw new Exception('Error handler had an error: ' . $e->getMessage() . "\nStack trace: " . $e->getTraceAsString(), 500, $e); } } } @@ -854,7 +854,7 @@ private function runInternal(Request $request, Response $response): static $arguments = $this->getArguments($error, [], $request->getParams()); \call_user_func_array($error->getAction(), $arguments); } catch (\Throwable $e) { - throw new Exception('Error handler had an error: ' . $e->getMessage(), 500, $e); + throw new Exception('Error handler had an error: ' . $e->getMessage() . "\nStack trace: " . $e->getTraceAsString(), 500, $e); } } } @@ -869,7 +869,7 @@ private function runInternal(Request $request, Response $response): static $arguments = $this->getArguments($error, [], $request->getParams()); \call_user_func_array($error->getAction(), $arguments); } catch (\Throwable $e) { - throw new Exception('Error handler had an error: ' . $e->getMessage(), 500, $e); + throw new Exception('Error handler had an error: ' . $e->getMessage() . "\nStack trace: " . $e->getTraceAsString(), 500, $e); } } } diff --git a/tests/AppTest.php b/tests/AppTest.php index 7a3c4f2..2c9d136 100755 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -718,22 +718,23 @@ public function testErrorHandlerFailure(): void ->error() ->inject('error') ->action(function ($error) { - throw new \Exception('Error handler failed'); + throw new Exception('Error handler failed'); }); $route = new Route('GET', '/path'); $route ->action(function () { - throw new \Exception('Route action failed'); + throw new Exception('Route action failed'); }); try { $this->app->execute($route, new Request(), new Response()); $this->fail('Should have thrown an exception'); - } catch (\Exception $e) { - $this->assertEquals('Error handler had an error: Error handler failed', $e->getMessage()); + } catch (Exception $e) { + $this->assertStringStartsWith('Error handler had an error: Error handler failed', $e->getMessage()); + $this->assertStringContainsString('Stack trace:', $e->getMessage()); $this->assertEquals(500, $e->getCode()); - $this->assertInstanceOf(\Exception::class, $e->getPrevious()); + $this->assertInstanceOf(Exception::class, $e->getPrevious()); $this->assertEquals('Error handler failed', $e->getPrevious()->getMessage()); } } @@ -744,13 +745,13 @@ public function testOptionsHandlerFailure(): void ->error() ->inject('error') ->action(function ($error) { - throw new \Exception('Options error handler failed'); + throw new Exception('Options error handler failed'); }); // Set up an options handler that throws App::options() ->action(function () { - throw new \Exception('Options handler failed'); + throw new Exception('Options handler failed'); }); $request = new UtopiaRequestTest(); @@ -759,8 +760,9 @@ public function testOptionsHandlerFailure(): void try { $this->app->run($request, new Response()); $this->fail('Should have thrown an exception'); - } catch (\Exception $e) { - $this->assertEquals('Error handler had an error: Options error handler failed', $e->getMessage()); + } catch (Exception $e) { + $this->assertStringStartsWith('Error handler had an error: Options error handler failed', $e->getMessage()); + $this->assertStringContainsString('Stack trace:', $e->getMessage()); $this->assertEquals(500, $e->getCode()); } } @@ -771,7 +773,7 @@ public function testNotFoundErrorHandlerFailure(): void $this->app ->error() ->action(function () { - throw new \Exception('404 error handler failed'); + throw new Exception('404 error handler failed'); }); $request = new UtopiaRequestTest(); @@ -781,10 +783,11 @@ public function testNotFoundErrorHandlerFailure(): void try { $this->app->run($request, new Response()); $this->fail('Should have thrown an exception'); - } catch (\Exception $e) { - $this->assertEquals('Error handler had an error: 404 error handler failed', $e->getMessage()); + } catch (Exception $e) { + $this->assertStringStartsWith('Error handler had an error: 404 error handler failed', $e->getMessage()); + $this->assertStringContainsString('Stack trace:', $e->getMessage()); $this->assertEquals(500, $e->getCode()); - $this->assertInstanceOf(\Exception::class, $e->getPrevious()); + $this->assertInstanceOf(Exception::class, $e->getPrevious()); $this->assertEquals('404 error handler failed', $e->getPrevious()->getMessage()); } } @@ -796,23 +799,24 @@ public function testGroupErrorHandlerFailure(): void ->error() ->groups(['api']) ->action(function () { - throw new \Exception('Group error handler failed'); + throw new Exception('Group error handler failed'); }); $route = new Route('GET', '/api/test'); $route ->groups(['api']) ->action(function () { - throw new \Exception('Route action failed'); + throw new Exception('Route action failed'); }); try { $this->app->execute($route, new Request(), new Response()); $this->fail('Should have thrown an exception'); - } catch (\Exception $e) { - $this->assertEquals('Error handler had an error: Group error handler failed', $e->getMessage()); + } catch (Exception $e) { + $this->assertStringStartsWith('Error handler had an error: Group error handler failed', $e->getMessage()); + $this->assertStringContainsString('Stack trace:', $e->getMessage()); $this->assertEquals(500, $e->getCode()); - $this->assertInstanceOf(\Exception::class, $e->getPrevious()); + $this->assertInstanceOf(Exception::class, $e->getPrevious()); $this->assertEquals('Group error handler failed', $e->getPrevious()->getMessage()); } } @@ -824,31 +828,32 @@ public function testErrorHandlerChaining(): void ->error() ->groups(['api']) ->action(function () { - throw new \Exception('First error handler failed'); + throw new Exception('First error handler failed'); }); $this->app ->error() ->action(function () { - throw new \Exception('Second error handler failed'); + throw new Exception('Second error handler failed'); }); $route = new Route('GET', '/api/test'); $route ->groups(['api']) ->action(function () { - throw new \Exception('Original error'); + throw new Exception('Original error'); }); try { $this->app->execute($route, new Request(), new Response()); $this->fail('Should have thrown an exception'); - } catch (\Exception $e) { - $this->assertEquals('Error handler had an error: First error handler failed', $e->getMessage()); + } catch (Exception $e) { + $this->assertStringStartsWith('Error handler had an error: First error handler failed', $e->getMessage()); + $this->assertStringContainsString('Stack trace:', $e->getMessage()); $this->assertEquals(500, $e->getCode()); // Verify the error chain - $this->assertInstanceOf(\Exception::class, $e->getPrevious()); + $this->assertInstanceOf(Exception::class, $e->getPrevious()); $this->assertEquals('First error handler failed', $e->getPrevious()->getMessage()); } } From 69d1452314213183ef7b7a98ca9bf178027160a9 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 25 Sep 2025 16:11:32 +0530 Subject: [PATCH 2/2] Trigger Build