Skip to content
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
29 changes: 19 additions & 10 deletions formwork/src/Controllers/ErrorsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function error(ResponseStatus $responseStatus = ResponseStatus::InternalS

if ($this->config->getBool('system.debug.enabled') || $this->request->isLocalhost()) {
$data['throwable'] = $throwable;
$data['stackTrace'] = $throwable !== null ? $this->getTrace($throwable) : [];
$data['traceResolver'] = $this->getTrace(...);
}

if ($this->request->isXmlHttpRequest()) {
Expand Down Expand Up @@ -80,23 +80,32 @@ public function forbidden(): Response
*/
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()
));
// Log the throwable like the native PHP exception handler does
$messages = [];
do {
array_unshift($messages, sprintf(
"%s: %s in %s:%s\nStack trace:\n%s\n",
$throwable::class,
$throwable->getMessage(),
$throwable->getFile(),
$throwable->getLine(),
$throwable->getTraceAsString()
));
} while ($throwable = $throwable->getPrevious());
error_log('Uncaught ' . implode("\nNext ", $messages));
}
Comment thread
giuscris marked this conversation as resolved.

/**
* Get full trace including the exception origin
*
* @return array<array{file?: string, line?: int, function?: string, class?: string, object?: object, type?: string, args?: list<mixed>}>
*/
private function getTrace(Throwable $throwable): array
private function getTrace(?Throwable $throwable): array
{
if ($throwable === null) {
return [];
}

$trace = $throwable->getTrace();

$file = $throwable->getFile();
Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Debug/CodeDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class CodeDumper
}

.color-scheme-dark .__formwork-code {
background-color: #333;
background-color: #333638;
color: #f0f0f0;
}

Expand Down
29 changes: 19 additions & 10 deletions formwork/src/Panel/Controllers/ErrorsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private function makeErrorResponse(ResponseStatus $responseStatus, string $name,

if ($this->config->getBool('system.debug.enabled') || $this->request->isLocalhost()) {
$data['throwable'] = $throwable;
$data['stackTrace'] = $throwable !== null ? $this->getTrace($throwable) : [];
$data['traceResolver'] = $this->getTrace(...);
}

if ($this->request->isXmlHttpRequest()) {
Expand Down Expand Up @@ -103,23 +103,32 @@ private function makeErrorResponse(ResponseStatus $responseStatus, string $name,
*/
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()
));
// Log the throwable like the native PHP exception handler does
$messages = [];
do {
array_unshift($messages, sprintf(
"%s: %s in %s:%s\nStack trace:\n%s\n",
$throwable::class,
$throwable->getMessage(),
$throwable->getFile(),
$throwable->getLine(),
$throwable->getTraceAsString()
));
} while ($throwable = $throwable->getPrevious());
error_log('Uncaught ' . implode("\nNext ", $messages));
}
Comment thread
giuscris marked this conversation as resolved.

/**
* Get full trace including the exception origin
*
* @return array<array{file?: string, line?: int, function?: string, class?: string, object?: object, type?: string, args?: list<mixed>}>
*/
private function getTrace(Throwable $throwable): array
private function getTrace(?Throwable $throwable): array
{
if ($throwable === null) {
return [];
}

$trace = $throwable->getTrace();

$file = $throwable->getFile();
Expand Down
25 changes: 15 additions & 10 deletions formwork/views/errors/partials/debug.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
</div>
<div class="error-debug-details">
<h3>Uncaught <code><?= $throwable::class ?></code>: <?= $this->escape($throwable->getMessage()) ?></h3>
<?php foreach ($stackTrace as $i => $frame) : ?>
<?php if (isset($frame['file'], $frame['line'])): ?>
<details <?= $this->attr(['open' => $i === 0]) ?>>
<summary><a class="error-debug-editor-uri" href="<?= Formwork\Utils\Str::interpolate($app->config()->getString('system.debug.editorUri'), ['filename' => $frame['file'], 'line' => $frame['line']]) ?>"><span class="error-debug-filename"><?= preg_replace('/([^\/]+)$/', '<strong>$1</strong>', $frame['file']) ?></span><span class="error-debug-line">:<?= $frame['line'] ?></span></a></summary>
<?php Formwork\Debug\CodeDumper::dumpBacktraceFrame($frame, $app->config()->getInt('system.debug.contextLines', 5)) ?>
</details>
<?php endif ?>
<?php endforeach ?>
<?php while ($throwable): ?>
<div class="error-debug-details">
<h3>Uncaught <code><?= $throwable::class ?></code>: <?= $this->escape($throwable->getMessage()) ?></h3>
<?php foreach ($traceResolver($throwable) as $i => $frame) : ?>
<?php if (isset($frame['file'], $frame['line'])): ?>
<details <?= $this->attr(['open' => $i === 0]) ?>>
<summary><a class="error-debug-editor-uri" href="<?= Formwork\Utils\Str::interpolate($app->config()->getString('system.debug.editorUri'), ['filename' => $frame['file'], 'line' => $frame['line']]) ?>"><span class="error-debug-filename"><?= preg_replace('/([^\/]+)$/', '<strong>$1</strong>', $frame['file']) ?></span><span class="error-debug-line">:<?= $frame['line'] ?></span></a></summary>
<?php Formwork\Debug\CodeDumper::dumpBacktraceFrame($frame, $app->config()->getInt('system.debug.contextLines', 5)) ?>
</details>
<?php endif ?>
<?php endforeach ?>
</div>
<?php $throwable = $throwable->getPrevious() ?>
<?php endwhile ?>
Comment thread
giuscris marked this conversation as resolved.
<div>
4 changes: 2 additions & 2 deletions formwork/views/errors/partials/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
}

.error-debug-details {
margin: 0 auto 4rem;
margin: 0 auto 2rem;
max-width: 87.5rem;
text-align: left;
border-radius: 4px;
Expand Down Expand Up @@ -133,4 +133,4 @@
<h1>
<span class="error-code"><?= $status ?? 500 ?></span>
<span class="error-status"><?= $this->escape($message ?? 'Internal Server Error') ?></span>
</h1>
</h1>
6 changes: 5 additions & 1 deletion panel/src/scss/components/_errors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@
max-width: 87.5rem;
padding: 1.5rem 2rem;
border-radius: var(--border-radius);
margin: 0 auto 4rem;
margin: 0 auto 2rem;
background-color: var(--color-base-900);
text-align: left;

.color-scheme-dark & {
background-color: var(--color-base-800);
}
}

.error-debug-details h3 {
Expand Down
29 changes: 16 additions & 13 deletions panel/views/errors/error.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,27 @@
<?php if (isset($action)) : ?><a class="action" href="<?= $action['href'] ?>"><?= $this->escape($action['label']) ?></a><?php endif ?>
</div>
</div>
<?php if (isset($throwable, $stackTrace)) : ?>
<?php if (isset($throwable)) : ?>
<div class="container-full">
<div class="error-debug-details">
<h3>Uncaught <code><?= $throwable::class ?></code>: <?= $this->escape($throwable->getMessage()) ?></h3>
<?php foreach ($stackTrace as $i => $frame) : ?>
<?php if (isset($frame['file'], $frame['line'])): ?>
<details <?= $this->attr(['open' => $i === 0]) ?>>
<summary><a class="error-debug-editor-uri" href="<?= Formwork\Utils\Str::interpolate($app->config()->getString('system.debug.editorUri'), ['filename' => $frame['file'], 'line' => $frame['line']]) ?>"><span class="error-debug-filename"><?= preg_replace('/([^\/]+)$/', '<strong>$1</strong>', $frame['file']) ?></span><span class="error-debug-line">:<?= $frame['line'] ?></span></a></summary>
<?php Formwork\Debug\CodeDumper::dumpBacktraceFrame($frame, $app->config()->getInt('system.debug.contextLines', 5)) ?>
</details>
<?php endif ?>
<?php endforeach ?>
</div>
<?php while ($throwable): ?>
<div class="error-debug-details">
<h3>Uncaught <code><?= $throwable::class ?></code>: <?= $this->escape($throwable->getMessage()) ?></h3>
<?php foreach ($traceResolver($throwable) as $i => $frame) : ?>
<?php if (isset($frame['file'], $frame['line'])): ?>
<details <?= $this->attr(['open' => $i === 0]) ?>>
<summary><a class="error-debug-editor-uri" href="<?= Formwork\Utils\Str::interpolate($app->config()->getString('system.debug.editorUri'), ['filename' => $frame['file'], 'line' => $frame['line']]) ?>"><span class="error-debug-filename"><?= preg_replace('/([^\/]+)$/', '<strong>$1</strong>', $frame['file']) ?></span><span class="error-debug-line">:<?= $frame['line'] ?></span></a></summary>
<?php Formwork\Debug\CodeDumper::dumpBacktraceFrame($frame, $app->config()->getInt('system.debug.contextLines', 5)) ?>
</details>
<?php endif ?>
<?php endforeach ?>
</div>
<?php $throwable = $throwable->getPrevious() ?>
Comment thread
giuscris marked this conversation as resolved.
<?php endwhile ?>
</div>
<?php endif ?>
</main>
<?php $this->assets()->add('@panel/js/app.min.js', ['module' => true]) ?>
<?php $this->insert('@panel._scripts') ?>
</body>

</html>
</html>