Skip to content

Commit

Permalink
Customizable display error flag (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk authored Mar 18, 2020
1 parent 7712198 commit 1a948d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
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

0 comments on commit 1a948d0

Please sign in to comment.