Skip to content

Commit 9944589

Browse files
[VarDumper] Fix dumping ThrowingCasterException
1 parent 2981d01 commit 9944589

File tree

5 files changed

+73
-40
lines changed

5 files changed

+73
-40
lines changed

src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,17 @@ public static function castThrowingCasterException(ThrowingCasterException $e, a
7171
{
7272
$b = (array) $a["\0Exception\0previous"];
7373

74-
array_splice($b["\0Exception\0trace"], count($a["\0Exception\0trace"]));
75-
76-
$t = static::$traceArgs;
77-
static::$traceArgs = false;
78-
$b = static::castException($a["\0Exception\0previous"], $b, $stub, $isNested);
79-
static::$traceArgs = $t;
80-
81-
if (empty($a["\0*\0message"])) {
82-
$a["\0*\0message"] = "Unexpected exception thrown from a caster: ".get_class($a["\0Exception\0previous"]);
83-
}
84-
8574
if (isset($b["\0*\0message"])) {
8675
$a["\0~\0message"] = $b["\0*\0message"];
8776
}
88-
if (isset($b["\0*\0file"])) {
89-
$a["\0~\0file"] = $b["\0*\0file"];
90-
}
91-
if (isset($b["\0*\0line"])) {
92-
$a["\0~\0line"] = $b["\0*\0line"];
93-
}
94-
if (isset($b["\0Exception\0trace"])) {
77+
78+
if (isset($a["\0Exception\0trace"])) {
79+
$b["\0Exception\0trace"][0] += array(
80+
'file' => $b["\0*\0file"],
81+
'line' => $b["\0*\0line"],
82+
);
83+
array_splice($b["\0Exception\0trace"], -1 - count($a["\0Exception\0trace"]));
84+
static::filterTrace($b["\0Exception\0trace"], false);
9585
$a["\0~\0trace"] = $b["\0Exception\0trace"];
9686
}
9787

src/Symfony/Component/VarDumper/Caster/PdoCaster.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,30 @@ class PdoCaster
5959

6060
public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested)
6161
{
62-
$a = array();
62+
$attr = array();
6363
$errmode = $c->getAttribute(\PDO::ATTR_ERRMODE);
6464
$c->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
6565

66-
foreach (self::$pdoAttributes as $attr => $values) {
67-
if (!isset($attr[0])) {
68-
$attr = $values;
69-
$values = array();
66+
foreach (self::$pdoAttributes as $k => $v) {
67+
if (!isset($k[0])) {
68+
$k = $v;
69+
$v = array();
7070
}
7171

7272
try {
73-
$a[$attr] = 'ERRMODE' === $attr ? $errmode : $c->getAttribute(constant("PDO::ATTR_{$attr}"));
74-
if ($values && isset($values[$a[$attr]])) {
75-
$a[$attr] = new ConstStub($values[$a[$attr]], $a[$attr]);
73+
$attr[$k] = 'ERRMODE' === $k ? $errmode : $c->getAttribute(constant('PDO::ATTR_'.$k));
74+
if ($v && isset($v[$attr[$k]])) {
75+
$attr[$k] = new ConstStub($v[$attr[$k]], $attr[$k]);
7676
}
77-
} catch (\Exception $m) {
77+
} catch (\Exception $e) {
7878
}
7979
}
8080

8181
$m = "\0~\0";
82-
$a = (array) $c + array(
82+
$a += array(
8383
$m.'inTransaction' => method_exists($c, 'inTransaction'),
8484
$m.'errorInfo' => $c->errorInfo(),
85-
$m.'attributes' => $a,
85+
$m.'attributes' => $attr,
8686
);
8787

8888
if ($a[$m.'inTransaction']) {

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private function callCaster($callback, $obj, $a, $stub, $isNested)
267267
$a = $cast;
268268
}
269269
} catch (\Exception $e) {
270-
$a["\0~\0"] = new ThrowingCasterException($callback, $e);
270+
$a[(Stub::TYPE_OBJECT === $stub->type ? "\0~\0" : '').''] = new ThrowingCasterException($callback, $e);
271271
}
272272

273273
return $a;

src/Symfony/Component/VarDumper/Exception/ThrowingCasterException.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,12 @@
1616
*/
1717
class ThrowingCasterException extends \Exception
1818
{
19-
private $caster;
20-
2119
/**
2220
* @param callable $caster The failing caster
2321
* @param \Exception $prev The exception thrown from the caster
2422
*/
2523
public function __construct($caster, \Exception $prev)
2624
{
27-
if (is_array($caster)) {
28-
if (isset($caster[0]) && is_object($caster[0])) {
29-
$caster[0] = get_class($caster[0]);
30-
}
31-
$caster = implode('::', $caster);
32-
}
33-
$this->caster = $caster;
34-
parent::__construct(null, 0, $prev);
25+
parent::__construct('Unexpected exception thrown from a caster: '.get_class($prev), 0, $prev);
3526
}
3627
}

src/Symfony/Component/VarDumper/Tests/CliDumperTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,58 @@ public function testGet()
102102
b"bin-key-é" => ""
103103
]
104104
105+
EOTXT
106+
,
107+
$out
108+
);
109+
}
110+
111+
public function testThrowingCaster()
112+
{
113+
$out = fopen('php://memory', 'r+b');
114+
115+
$dumper = new CliDumper();
116+
$dumper->setColors(false);
117+
$cloner = new VarCloner();
118+
$cloner->addCasters(array(
119+
':stream' => function () {
120+
throw new \Exception('Foobar');
121+
},
122+
));
123+
$line = __LINE__ - 3;
124+
$file = __FILE__;
125+
$ref = (int) $out;
126+
127+
$data = $cloner->cloneVar($out);
128+
$dumper->dump($data, $out);
129+
rewind($out);
130+
$out = stream_get_contents($out);
131+
132+
$this->assertStringMatchesFormat(
133+
<<<EOTXT
134+
:stream {@{$ref}
135+
wrapper_type: "PHP"
136+
stream_type: "MEMORY"
137+
mode: "w+b"
138+
unread_bytes: 0
139+
seekable: true
140+
uri: "php://memory"
141+
timed_out: false
142+
blocked: true
143+
eof: false
144+
options: []
145+
⚠: Symfony\Component\VarDumper\Exception\ThrowingCasterException {#%d
146+
#message: "Unexpected exception thrown from a caster: Exception"
147+
message: "Foobar"
148+
trace: array:1 [
149+
0 => array:2 [
150+
"call" => "%s{closure}()"
151+
"file" => "{$file}:{$line}"
152+
]
153+
]
154+
}
155+
}
156+
105157
EOTXT
106158
,
107159
$out

0 commit comments

Comments
 (0)