Skip to content
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

Little refactor using early return #303

Merged
merged 2 commits into from
Jul 23, 2023
Merged
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
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}.";
}
}