Skip to content

Customizable display error flag #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2020
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
22 changes: 21 additions & 1 deletion lib/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ final class ErrorHandler
*/
private $logVariables = true;

/**
* @var null|bool
*/
private $displayErrors;

/**
* @var callable
*/
Expand Down Expand Up @@ -186,6 +191,21 @@ public function logVariables(): bool
return $this->logVariables;
}

public function setDisplayErrors(bool $displayErrors): void
{
$this->displayErrors = $displayErrors;
}

public function displayErrors(): bool
{
if (null === $this->displayErrors) {
$this->setDisplayErrors((bool) \ini_get('display_errors'));
\assert(null !== $this->displayErrors);
}

return $this->displayErrors;
}

/**
* @param array<int, bool> $scream
*/
Expand Down Expand Up @@ -287,7 +307,7 @@ public function exceptionHandler(Throwable $exception): void
}
$output .= '<h1>500: Errore interno</h1>';
$output .= \PHP_EOL;
if (true === (bool) \ini_get('display_errors')) {
if ($this->displayErrors()) {
$currentEx = $exception;
do {
$output .= \sprintf(
Expand Down
5 changes: 2 additions & 3 deletions tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ final class ErrorHandlerTest extends TestCase

protected function setUp(): void
{
\ini_set('display_errors', (string) false);
$this->backupErrorLog = (string) \ini_get('error_log');
$this->errorLog = __DIR__ . \DIRECTORY_SEPARATOR . 'error_log_test';
\touch($this->errorLog);
Expand Down Expand Up @@ -145,7 +144,7 @@ public function testHandleCliException(): void

public function testHandleWebExceptionWithDisplay(): void
{
\ini_set('display_errors', (string) true);
$this->errorHandler->setDisplayErrors(true);
$this->errorHandler->setCli(false);
$this->errorHandler->setLogErrors(true);

Expand All @@ -161,7 +160,7 @@ public function testHandleWebExceptionWithDisplay(): void

public function testHandleWebExceptionWithoutDisplay(): void
{
\ini_set('display_errors', (string) false);
$this->errorHandler->setDisplayErrors(false);
$this->errorHandler->setCli(false);
$this->errorHandler->setLogErrors(true);

Expand Down