diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 777db69..b8f2f7d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,6 +43,7 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ env.PHP_VERSION }} + ini-values: zend.exception_ignore_args=Off,zend.exception_string_param_max_len=15 tools: composer - name: Install composer dependencies diff --git a/src/Pages/InternalErrorPage.php b/src/Pages/InternalErrorPage.php new file mode 100644 index 0000000..6a07950 --- /dev/null +++ b/src/Pages/InternalErrorPage.php @@ -0,0 +1,143 @@ +head->append( + FluentHTML::fromTag( 'title' )->addChild( 'Internal Error' ) + ); + $this->error = $error; + $this->setResponseCode( 500 ); + } + + protected function build(): void { + $this->addStyleSheet( 'error-styles.css' ); + $this->head->append( + FluentHTML::make( + 'style', + [], + [ + 'pre { overflow-x: auto; padding-bottom: 10px; }', + '.error-box { width: fit-content; max-width: 100%; }', + ] + ) + ); + $error = $this->error; + $file = self::formatFile( $error->getFile() ); + $this->contentWrapper->append( + FluentHTML::make( + 'div', + [ 'class' => 'error-box' ], + [ + FluentHTML::make( 'h1', [], 'Internal Error' ), + FluentHTML::make( + 'p', + [], + '[' . get_class( $error ) . '] ' . $error->getMessage(), + ), + FluentHTML::make( + 'p', + [], + [ + 'From ', + FluentHTML::make( 'code', [], $file ), + ' line ', + FluentHTML::make( 'code', [], (string)$error->getLine() ), + ] + ), + FluentHTML::make( 'p', [], 'Backtrace:' ), + FluentHTML::make( + 'pre', + [], + self::formatTrace( $error ) + ), + ] + ) + ); + } + + public static function handleException( Throwable $error ) { + try { + $page = new InternalErrorPage( $error ); + $page->getResponse()->applyResponse(); + } catch ( Throwable $error2 ) { + self::handleManually( $error, $error2 ); + } + } + + public static function handleManually( Throwable $error1, Throwable $error2 ) { + http_response_code( 500 ); + echo "\n"; + echo "\n"; + echo "
\n"; + echo "[' . get_class( $error1 ) . '] ' . $error1->getMessage() . "
\n"; + $file = self::formatFile( $error1->getFile() ); + echo "From: $file line " . $error1->getLine() . "
Backtrace:
\n"; + echo "" . self::formatTrace( $error1 ) . "\n"; + + echo "
While trying to handle that error, the handler also had an error:
\n"; + echo '[' . get_class( $error2 ) . '] ' . $error2->getMessage() . "
\n"; + $file = self::formatFile( $error2->getFile() ); + echo "From: $file line " . $error2->getLine() . "
Backtrace:
\n"; + echo "" . self::formatTrace( $error2 ) . "\n"; + + echo "
[Exception] first
+From: .../tests/StaticOutputTest.php line 130
Backtrace:
+#0 .../vendor/phpunit/phpunit/src/Framework/TestCase.php({line}): DanielWebsite\Tests\StaticOutputTest->testErrorManual()
+#1 .../vendor/phpunit/phpunit/src/Framework/TestCase.php({line}): PHPUnit\Framework\TestCase->runTest()
+#2 .../vendor/phpunit/phpunit/src/Framework/TestRunner/TestRunner.php({line}): PHPUnit\Framework\TestCase->runBare()
+#3 .../vendor/phpunit/phpunit/src/Framework/TestCase.php({line}): PHPUnit\Framework\TestRunner->run(Object(DanielWebsite\Tests\StaticOutputTest))
+#4 .../vendor/phpunit/phpunit/src/Framework/TestSuite.php({line}): PHPUnit\Framework\TestCase->run()
+#5 .../vendor/phpunit/phpunit/src/Framework/TestSuite.php({line}): PHPUnit\Framework\TestSuite->run()
+#6 .../vendor/phpunit/phpunit/src/Framework/TestSuite.php({line}): PHPUnit\Framework\TestSuite->run()
+#7 .../vendor/phpunit/phpunit/src/TextUI/TestRunner.php({line}): PHPUnit\Framework\TestSuite->run()
+#8 .../vendor/phpunit/phpunit/src/TextUI/Application.php({line}): PHPUnit\TextUI\TestRunner->run(Object(PHPUnit\TextUI\Configuration\Configuration), Object(PHPUnit\Runner\ResultCache\DefaultResultCache), Object(PHPUnit\Framework\TestSuite))
+#9 .../vendor/phpunit/phpunit/phpunit({line}): PHPUnit\TextUI\Application->run(Array)
+#10 .../vendor/bin/phpunit(122): include({path})
+#11 {main}
+While trying to handle that error, the handler also had an error:
+[Exception] second
+From: .../tests/StaticOutputTest.php line 131
Backtrace:
+#0 .../vendor/phpunit/phpunit/src/Framework/TestCase.php({line}): DanielWebsite\Tests\StaticOutputTest->testErrorManual()
+#1 .../vendor/phpunit/phpunit/src/Framework/TestCase.php({line}): PHPUnit\Framework\TestCase->runTest()
+#2 .../vendor/phpunit/phpunit/src/Framework/TestRunner/TestRunner.php({line}): PHPUnit\Framework\TestCase->runBare()
+#3 .../vendor/phpunit/phpunit/src/Framework/TestCase.php({line}): PHPUnit\Framework\TestRunner->run(Object(DanielWebsite\Tests\StaticOutputTest))
+#4 .../vendor/phpunit/phpunit/src/Framework/TestSuite.php({line}): PHPUnit\Framework\TestCase->run()
+#5 .../vendor/phpunit/phpunit/src/Framework/TestSuite.php({line}): PHPUnit\Framework\TestSuite->run()
+#6 .../vendor/phpunit/phpunit/src/Framework/TestSuite.php({line}): PHPUnit\Framework\TestSuite->run()
+#7 .../vendor/phpunit/phpunit/src/TextUI/TestRunner.php({line}): PHPUnit\Framework\TestSuite->run()
+#8 .../vendor/phpunit/phpunit/src/TextUI/Application.php({line}): PHPUnit\TextUI\TestRunner->run(Object(PHPUnit\TextUI\Configuration\Configuration), Object(PHPUnit\Runner\ResultCache\DefaultResultCache), Object(PHPUnit\Framework\TestSuite))
+#9 .../vendor/phpunit/phpunit/phpunit({line}): PHPUnit\TextUI\Application->run(Array)
+#10 .../vendor/bin/phpunit(122): include({path})
+#11 {main}
+[Exception] testing
From .../tests/StaticOutputTest.php line 106
Backtrace:
#0 .../vendor/phpunit/phpunit/src/Framework/TestCase.php({line}): DanielWebsite\Tests\StaticOutputTest->testErrorNice()
+#1 .../vendor/phpunit/phpunit/src/Framework/TestCase.php({line}): PHPUnit\Framework\TestCase->runTest()
+#2 .../vendor/phpunit/phpunit/src/Framework/TestRunner/TestRunner.php({line}): PHPUnit\Framework\TestCase->runBare()
+#3 .../vendor/phpunit/phpunit/src/Framework/TestCase.php({line}): PHPUnit\Framework\TestRunner->run(Object(DanielWebsite\Tests\StaticOutputTest))
+#4 .../vendor/phpunit/phpunit/src/Framework/TestSuite.php({line}): PHPUnit\Framework\TestCase->run()
+#5 .../vendor/phpunit/phpunit/src/Framework/TestSuite.php({line}): PHPUnit\Framework\TestSuite->run()
+#6 .../vendor/phpunit/phpunit/src/Framework/TestSuite.php({line}): PHPUnit\Framework\TestSuite->run()
+#7 .../vendor/phpunit/phpunit/src/TextUI/TestRunner.php({line}): PHPUnit\Framework\TestSuite->run()
+#8 .../vendor/phpunit/phpunit/src/TextUI/Application.php({line}): PHPUnit\TextUI\TestRunner->run(Object(PHPUnit\TextUI\Configuration\Configuration), Object(PHPUnit\Runner\ResultCache\DefaultResultCache), Object(PHPUnit\Framework\TestSuite))
+#9 .../vendor/phpunit/phpunit/phpunit({line}): PHPUnit\TextUI\Application->run(Array)
+#10 .../vendor/bin/phpunit(122): include({path})
+#11 {main}