Skip to content

Commit

Permalink
Merge pull request #303 from joubertredrat/refactor-shutdown
Browse files Browse the repository at this point in the history
Little refactor using early return
  • Loading branch information
l0gicgate committed Jul 23, 2023
2 parents 0de43e0 + c601932 commit 630e8c8
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions src/Application/Handlers/ShutdownHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,47 @@ public function __construct(
public function __invoke()
{
$error = error_get_last();
if ($error) {
$errorFile = $error['file'];
$errorLine = $error['line'];
$errorMessage = $error['message'];
$errorType = $error['type'];
$message = 'An error while processing your request. Please try again later.';

if ($this->displayErrorDetails) {
switch ($errorType) {
case E_USER_ERROR:
$message = "FATAL ERROR: {$errorMessage}. ";
$message .= " on line {$errorLine} in file {$errorFile}.";
break;

case E_USER_WARNING:
$message = "WARNING: {$errorMessage}";
break;

case E_USER_NOTICE:
$message = "NOTICE: {$errorMessage}";
break;

default:
$message = "ERROR: {$errorMessage}";
$message .= " on line {$errorLine} in file {$errorFile}.";
break;
}
}

$exception = new HttpInternalServerErrorException($this->request, $message);
$response = $this->errorHandler->__invoke(
$this->request,
$exception,
$this->displayErrorDetails,
false,
false,
);

$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
if (!$error) {
return;
}

$message = $this->getErrorMessage($error);
$exception = new HttpInternalServerErrorException($this->request, $message);
$response = $this->errorHandler->__invoke(
$this->request,
$exception,
$this->displayErrorDetails,
false,
false,
);

$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
}

private function getErrorMessage(array $error): string
{
if (!$this->displayErrorDetails) {
return 'An error while processing your request. Please try again later.';
}

$errorFile = $error['file'];
$errorLine = $error['line'];
$errorMessage = $error['message'];
$errorType = $error['type'];

if ($errorType === E_USER_ERROR) {
return "FATAL ERROR: {$errorMessage}. on line {$errorLine} in file {$errorFile}.";
}

if ($errorType === E_USER_WARNING) {
return "WARNING: {$errorMessage}";
}

if ($errorType === E_USER_NOTICE) {
return "NOTICE: {$errorMessage}";
}

return "FATAL ERROR: {$errorMessage}. on line {$errorLine} in file {$errorFile}.";
}
}

0 comments on commit 630e8c8

Please sign in to comment.