Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
"require-dev": {
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-phpunit": "^0.12",
"phpunit/phpunit": "^8.5",
"roave/security-advisories": "dev-master",
"phpunit/phpunit": "^9.2",
"slam/php-cs-fixer-extensions": "^1.19",
"slam/php-debug-r": "^1.6",
"slam/phpstan-extensions": "^4.0",
"symfony/console": "^5.0",
"symfony/console": "^5.1",
"thecodingmachine/phpstan-strict-rules": "^0.12"
},
"autoload": {
Expand Down
39 changes: 34 additions & 5 deletions lib/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ final class ErrorHandler
\E_WARNING => 'E_WARNING',
];

/**
* @var array<int, class-string<Throwable>>
*/
private $exceptionsTypesFor404 = [];

public function __construct(callable $emailCallback)
{
$this->emailCallback = $emailCallback;
Expand Down Expand Up @@ -296,7 +301,11 @@ public function exceptionHandler(Throwable $exception): void

// @codeCoverageIgnoreStart
if (! \headers_sent()) {
\header('HTTP/1.1 500 Internal Server Error');
$header = 'HTTP/1.1 500 Internal Server Error';
if (\in_array(\get_class($exception), $this->exceptionsTypesFor404, true)) {
$header = 'HTTP/1.1 404 Not Found';
}
\header($header);
}
// @codeCoverageIgnoreEnd

Expand All @@ -305,12 +314,16 @@ public function exceptionHandler(Throwable $exception): void

public function renderHtmlException(Throwable $exception): string
{
$ajax = (isset($_SERVER) && isset($_SERVER['X_REQUESTED_WITH']) && 'XMLHttpRequest' === $_SERVER['X_REQUESTED_WITH']);
$output = '';
$ajax = (isset($_SERVER) && isset($_SERVER['X_REQUESTED_WITH']) && 'XMLHttpRequest' === $_SERVER['X_REQUESTED_WITH']);
$output = '';
$errorType = '500: Internal Server Error';
if (\in_array(\get_class($exception), $this->exceptionsTypesFor404, true)) {
$errorType = '404: Not Found';
}
if (! $ajax) {
$output .= '<!DOCTYPE html><html><head><title>500: Internal Server Error</title></head><body>';
$output .= \sprintf('<!DOCTYPE html><html><head><title>%s</title></head><body>', $errorType);
}
$output .= '<h1>500: Internal Server Error</h1>';
$output .= \sprintf('<h1>%s</h1>', $errorType);
$output .= \PHP_EOL;
if ($this->displayErrors()) {
$currentEx = $exception;
Expand Down Expand Up @@ -457,4 +470,20 @@ private function purgeTrace(string $trace): string
{
return \defined('ROOT_PATH') ? \str_replace(ROOT_PATH, '.', $trace) : $trace;
}

/**
* @param array<int, class-string<Throwable>> $exceptionsTypesFor404
*/
public function set404ExceptionTypes(array $exceptionsTypesFor404): void
{
$this->exceptionsTypesFor404 = $exceptionsTypesFor404;
}

/**
* @return array<int, class-string<Throwable>>
*/
public function get404ExceptionTypes(): array
{
return $this->exceptionsTypesFor404;
}
}
2 changes: 0 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ parameters:
- lib/
- tests/
ignoreErrors:
- '#Class Doctrine\\Common\\Util\\Debug not found#'
- '#Call to static method export\(\) on an unknown class Doctrine\\Common\\Util\\Debug#'
- '#Function \w+ is unsafe to use, rely on .+ instead#'
- '#Variable \$_\w+ in isset\(\) always exists and is not nullable#'
- '#Parameter \#1 \$error_handler of function set_error_handler expects#'
Expand Down
17 changes: 16 additions & 1 deletion tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ErrorException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Slam\ErrorHandler\ErrorHandler;
use Symfony\Component\Console\Terminal;

Expand Down Expand Up @@ -124,7 +125,7 @@ public function testScream(): void

$warningMessage = \uniqid('warning_');
$this->expectException(ErrorException::class);
$this->expectExceptionMessageRegExp(\sprintf('/%s/', \preg_quote($warningMessage)));
$this->expectExceptionMessageMatches(\sprintf('/%s/', \preg_quote($warningMessage)));

@ \trigger_error($warningMessage, \E_USER_WARNING);
}
Expand Down Expand Up @@ -291,4 +292,18 @@ public function testTerminalWidthByEnv(): void
$terminal = new Terminal();
self::assertSame($terminal->getWidth(), $errorHandler->getTerminalWidth());
}

public function test404SpecificExceptionForHeaders(): void
{
self::assertEmpty($this->errorHandler->get404ExceptionTypes());

self::assertStringNotContainsString('404: Not Found', $this->errorHandler->renderHtmlException(new RuntimeException()));

$exceptionTypes = [RuntimeException::class];
$this->errorHandler->set404ExceptionTypes($exceptionTypes);

self::assertSame($exceptionTypes, $this->errorHandler->get404ExceptionTypes());

self::assertStringContainsString('404: Not Found', $this->errorHandler->renderHtmlException(new RuntimeException()));
}
}