Skip to content

Commit 4c84e2c

Browse files
lyrixxfabpot
authored andcommitted
[ErrorHander] Display exception properties in the HTML error page
1 parent be149c2 commit 4c84e2c

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Display exception properties in the HTML error page
8+
49
6.1
510
---
611

ErrorRenderer/HtmlErrorRenderer.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Symfony\Component\HttpFoundation\Response;
1818
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
1919
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
20+
use Symfony\Component\VarDumper\Cloner\VarCloner;
21+
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
2022

2123
/**
2224
* @author Yonel Ceruto <[email protected]>
@@ -147,6 +149,23 @@ private function renderException(FlattenException $exception, string $debugTempl
147149
]);
148150
}
149151

152+
private function dumpValue(mixed $value): string
153+
{
154+
$cloner = new VarCloner();
155+
$data = $cloner->cloneVar($value);
156+
157+
$dumper = new HtmlDumper();
158+
$dumper->setTheme('light');
159+
$dumper->setOutput($output = fopen('php://memory', 'r+'));
160+
$dumper->dump($data);
161+
162+
$dump = stream_get_contents($output, -1, 0);
163+
rewind($output);
164+
ftruncate($output, 0);
165+
166+
return $dump;
167+
}
168+
150169
private function formatArgs(array $args): string
151170
{
152171
$result = [];

Exception/FlattenException.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class FlattenException
3636
private string $file;
3737
private int $line;
3838
private ?string $asString = null;
39+
private array $properties = [];
3940

4041
public static function create(\Exception $exception, int $statusCode = null, array $headers = []): static
4142
{
@@ -77,6 +78,13 @@ public static function createFromThrowable(\Throwable $exception, int $statusCod
7778
$e->setPrevious(static::createFromThrowable($previous));
7879
}
7980

81+
if ((new \ReflectionClass($exception::class))->isUserDefined()) {
82+
$getProperties = \Closure::bind(fn (\Throwable $e) => get_object_vars($e), null, $exception::class);
83+
$properties = $getProperties($exception);
84+
unset($properties['message'], $properties['code'], $properties['file'], $properties['line']);
85+
$e->properties = $properties;
86+
}
87+
8088
return $e;
8189
}
8290

@@ -88,6 +96,7 @@ public function toArray(): array
8896
'message' => $exception->getMessage(),
8997
'class' => $exception->getClass(),
9098
'trace' => $exception->getTrace(),
99+
'properties' => $exception->getProperties(),
91100
];
92101
}
93102

@@ -221,6 +230,11 @@ public function setCode(int|string $code): static
221230
return $this;
222231
}
223232

233+
public function getProperties(): array
234+
{
235+
return $this->properties;
236+
}
237+
224238
public function getPrevious(): ?self
225239
{
226240
return $this->previous;

Resources/assets/css/exception.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
--card-label-color: var(--tab-active-color);
8484
}
8585

86-
html{font-family:sans-serif;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}body{margin:0}article,aside,details,figcaption,figure,footer,header,hgroup,main,menu,nav,section,summary{display:block}audio,canvas,progress,video{display:inline-block;vertical-align:baseline}audio:not([controls]){display:none;height:0}[hidden],template{display:none}a{background-color:transparent}a:active,a:hover{outline:0}abbr[title]{border-bottom:1px dotted}b,strong{font-weight:700}dfn{font-style:italic}h1{margin:.67em 0;font-size:2em}mark{color:#000;background:#ff0}small{font-size:80%}sub,sup{position:relative;font-size:75%;line-height:0;vertical-align:baseline}sup{top:-.5em}sub{bottom:-.25em}img{border:0}svg:not(:root){overflow:hidden}figure{margin:1em 40px}hr{height:0;-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box}pre{overflow:auto}code,kbd,pre,samp{font-family:monospace,monospace;font-size:1em}button,input,optgroup,select,textarea{margin:0;font:inherit;color:inherit}button{overflow:visible}button,select{text-transform:none}button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer}button[disabled],html input[disabled]{cursor:default}button::-moz-focus-inner,input::-moz-focus-inner{padding:0;border:0}input{line-height:normal}input[type="checkbox"],input[type="radio"]{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;padding:0}input[type="number"]::-webkit-inner-spin-button,input[type="number"]::-webkit-outer-spin-button{height:auto}input[type="search"]{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box;-webkit-appearance:textfield}input[type="search"]::-webkit-search-cancel-button,input[type="search"]::-webkit-search-decoration{-webkit-appearance:none}fieldset{padding:.35em .625em .75em;margin:0 2px;border:1px solid silver}legend{padding:0;border:0}textarea{overflow:auto}optgroup{font-weight:700}table{border-spacing:0;border-collapse:collapse}td,th{padding:0}
86+
html{font-family:sans-serif;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}body{margin:0}article,aside,details,figcaption,figure,footer,header,hgroup,main,menu,nav,section{display:block}audio,canvas,progress,video{display:inline-block;vertical-align:baseline}audio:not([controls]){display:none;height:0}[hidden],template{display:none}a{background-color:transparent}a:active,a:hover{outline:0}abbr[title]{border-bottom:1px dotted}b,strong{font-weight:700}dfn{font-style:italic}h1{margin:.67em 0;font-size:2em}mark{color:#000;background:#ff0}small{font-size:80%}sub,sup{position:relative;font-size:75%;line-height:0;vertical-align:baseline}sup{top:-.5em}sub{bottom:-.25em}img{border:0}svg:not(:root){overflow:hidden}figure{margin:1em 40px}hr{height:0;-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box}pre{overflow:auto}code,kbd,pre,samp{font-family:monospace,monospace;font-size:1em}button,input,optgroup,select,textarea{margin:0;font:inherit;color:inherit}button{overflow:visible}button,select{text-transform:none}button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer}button[disabled],html input[disabled]{cursor:default}button::-moz-focus-inner,input::-moz-focus-inner{padding:0;border:0}input{line-height:normal}input[type="checkbox"],input[type="radio"]{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;padding:0}input[type="number"]::-webkit-inner-spin-button,input[type="number"]::-webkit-outer-spin-button{height:auto}input[type="search"]{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box;-webkit-appearance:textfield}input[type="search"]::-webkit-search-cancel-button,input[type="search"]::-webkit-search-decoration{-webkit-appearance:none}fieldset{padding:.35em .625em .75em;margin:0 2px;border:1px solid silver}legend{padding:0;border:0}textarea{overflow:auto}optgroup{font-weight:700}table{border-spacing:0;border-collapse:collapse}td,th{padding:0}summary{cursor: pointer}
8787

8888
html {
8989
/* always display the vertical scrollbar to avoid jumps when toggling contents */
@@ -208,6 +208,10 @@ header .container { display: flex; justify-content: space-between; }
208208
.exception-message a { border-bottom: 1px solid rgba(255, 255, 255, 0.5); font-size: inherit; text-decoration: none; }
209209
.exception-message a:hover { border-bottom-color: #ffffff; }
210210

211+
.exception-properties-wrapper { margin: .8em 0; }
212+
.exception-properties { background: var(--base-0); border: var(--border); box-shadow: 0px 0px 1px rgba(128, 128, 128, .2); }
213+
.exception-properties pre { margin: 0; padding: 0.2em 0; }
214+
211215
.exception-illustration { flex-basis: 111px; flex-shrink: 0; height: 66px; margin-left: 15px; opacity: .7; }
212216

213217
.trace + .trace { margin-top: 30px; }

Resources/views/exception.html.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
</h2>
1414
</div>
1515
</div>
16-
1716
<div class="exception-message-wrapper">
1817
<div class="container">
1918
<h1 class="break-long-words exception-message<?= mb_strlen($exceptionMessage) > 180 ? ' long' : ''; ?>"><?= $this->formatFileFromText(nl2br($exceptionMessage)); ?></h1>

Resources/views/traces.html.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
<p class="break-long-words trace-message"><?= $this->escape($exception['message']); ?></p>
2626
<?php } ?>
2727
</div>
28+
<?php if ($exception['properties']) { ?>
29+
<details class="exception-properties-wrapper">
30+
<summary>Show exception properties</summary>
31+
<div class="exception-properties">
32+
<?= $this->dumpValue($exception['properties']) ?>
33+
</div>
34+
</details>
35+
<?php } ?>
2836
</div>
2937

3038
<div id="trace-html-<?= $index; ?>" class="sf-toggle-content">

0 commit comments

Comments
 (0)