Skip to content

Commit

Permalink
Move debug info exposure logic to errors controller
Browse files Browse the repository at this point in the history
  • Loading branch information
giuscris committed Mar 1, 2025
1 parent 9cf127b commit 2ce9012
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
46 changes: 31 additions & 15 deletions formwork/src/Controllers/ErrorsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,32 @@ final class ErrorsController extends AbstractController implements ErrorsControl
{
/**
* ErrorsController@error action
*
* @param array<string, mixed> $data
*/
public function error(ResponseStatus $responseStatus = ResponseStatus::InternalServerError, ?Throwable $throwable = null): Response
public function error(ResponseStatus $responseStatus = ResponseStatus::InternalServerError, ?Throwable $throwable = null, array $data = []): Response
{
Response::cleanOutputBuffers();

$response = $this->request->isXmlHttpRequest()
? JsonResponse::error('Error', $responseStatus)
: new Response($this->view(
if ($this->config->get('system.debug.enabled') || $this->request->isLocalhost()) {
$data['throwable'] = $throwable;
}

if ($this->request->isXmlHttpRequest()) {
$response = JsonResponse::error('Error', $responseStatus);
} else {
$response = new Response($this->view(
'errors.error',
[
'status' => $responseStatus->code(),
'message' => $responseStatus->message(),
'throwable' => $throwable,
'status' => $responseStatus->code(),
'message' => $responseStatus->message(),
...$data,
]
), $responseStatus);
}

if ($throwable !== null) {
error_log(sprintf(
"Uncaught %s: %s in %s:%s\nStack trace:\n%s\n",
$throwable::class,
$throwable->getMessage(),
$throwable->getFile(),
$throwable->getLine(),
$throwable->getTraceAsString()
));
$this->logThrowable($throwable);
}

return $response;
Expand Down Expand Up @@ -64,4 +65,19 @@ public function forbidden(): Response
{
return $this->error(ResponseStatus::Forbidden);
}

/**
* Log a throwable to the error log
*/
private function logThrowable(Throwable $throwable): void
{
error_log(sprintf(
"Uncaught %s: %s in %s:%s\nStack trace:\n%s\n",
$throwable::class,
$throwable->getMessage(),
$throwable->getFile(),
$throwable->getLine(),
$throwable->getTraceAsString()
));
}
}
14 changes: 9 additions & 5 deletions formwork/src/Panel/Controllers/ErrorsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function error(ResponseStatus $responseStatus = ResponseStatus::InternalS
return $this->makeErrorResponse($responseStatus, 'internalServerError', [
'href' => $this->makeGitHubIssueUri($throwable),
'label' => $this->translate('panel.errors.action.reportToGithub'),
], ['throwable' => $throwable]);
], $throwable);
}

/**
Expand All @@ -40,7 +40,7 @@ public function internalServerError(Throwable $throwable): Response
return $this->makeErrorResponse(ResponseStatus::InternalServerError, 'internalServerError', [
'href' => $this->makeGitHubIssueUri($throwable),
'label' => $this->translate('panel.errors.action.reportToGithub'),
], ['throwable' => $throwable]);
], $throwable);
}

/**
Expand All @@ -60,10 +60,14 @@ public function forbidden(): Response
* @param array<mixed> $action
* @param array<string, mixed> $data
*/
private function makeErrorResponse(ResponseStatus $responseStatus, string $name, array $action, array $data = []): Response
private function makeErrorResponse(ResponseStatus $responseStatus, string $name, array $action, ?Throwable $throwable = null, array $data = []): Response
{
Response::cleanOutputBuffers();

if ($this->config->get('system.debug.enabled') || $this->request->isLocalhost()) {
$data['throwable'] = $throwable;
}

if ($this->request->isXmlHttpRequest()) {
$response = JsonResponse::error('Error', $responseStatus);
} else {
Expand All @@ -78,8 +82,8 @@ private function makeErrorResponse(ResponseStatus $responseStatus, string $name,
]), $responseStatus);
}

if ($data['throwable'] !== null) {
$this->logThrowable($data['throwable']);
if ($throwable !== null) {
$this->logThrowable($throwable);
}

return $response;
Expand Down
4 changes: 2 additions & 2 deletions formwork/views/errors/error.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h2>Oops, something went wrong!</h2>
<p>Formwork encountered an error while serving your request.<br>If you are the maintainer of this site, please check Formwork configuration or the server log for errors.</p>
<p><a href="https://github.com/getformwork/formwork/issues" target="_blank">Report an issue to GitHub</a></p>
<?php if ($throwable && ($app->config()->get('system.debug.enabled', false) || $app->request()->isLocalhost())) : ?>
<?php if (isset($throwable)) : ?>
<?php $this->insert('errors.partials.debug') ?>
<?php endif ?>
<?php $this->insert('errors.partials.footer') ?>
<?php $this->insert('errors.partials.footer') ?>
4 changes: 2 additions & 2 deletions panel/views/errors/error.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<?php if (isset($action)) : ?><a class="action" href="<?= $action['href'] ?>"><?= $action['label'] ?></a><?php endif ?>
</div>
</div>
<?php if (isset($throwable) && ($app->config()->get('system.debug.enabled') || $app->request()->isLocalhost())) : ?>
<?php if (isset($throwable)) : ?>
<div class="container-full">
<div class="error-debug-details">
<h3>Uncaught <code><?= $throwable::class ?></code>: <?= $throwable->getMessage() ?></h3>
Expand All @@ -46,4 +46,4 @@
</main>
</body>

</html>
</html>

0 comments on commit 2ce9012

Please sign in to comment.