Skip to content

Commit

Permalink
[VarDumper] Fix dumping ThrowingCasterException
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Mar 26, 2015
1 parent 2981d01 commit 9944589
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 40 deletions.
26 changes: 8 additions & 18 deletions src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php
Expand Up @@ -71,27 +71,17 @@ public static function castThrowingCasterException(ThrowingCasterException $e, a
{
$b = (array) $a["\0Exception\0previous"];

array_splice($b["\0Exception\0trace"], count($a["\0Exception\0trace"]));

$t = static::$traceArgs;
static::$traceArgs = false;
$b = static::castException($a["\0Exception\0previous"], $b, $stub, $isNested);
static::$traceArgs = $t;

if (empty($a["\0*\0message"])) {
$a["\0*\0message"] = "Unexpected exception thrown from a caster: ".get_class($a["\0Exception\0previous"]);
}

if (isset($b["\0*\0message"])) {
$a["\0~\0message"] = $b["\0*\0message"];
}
if (isset($b["\0*\0file"])) {
$a["\0~\0file"] = $b["\0*\0file"];
}
if (isset($b["\0*\0line"])) {
$a["\0~\0line"] = $b["\0*\0line"];
}
if (isset($b["\0Exception\0trace"])) {

if (isset($a["\0Exception\0trace"])) {
$b["\0Exception\0trace"][0] += array(
'file' => $b["\0*\0file"],
'line' => $b["\0*\0line"],
);
array_splice($b["\0Exception\0trace"], -1 - count($a["\0Exception\0trace"]));
static::filterTrace($b["\0Exception\0trace"], false);
$a["\0~\0trace"] = $b["\0Exception\0trace"];
}

Expand Down
22 changes: 11 additions & 11 deletions src/Symfony/Component/VarDumper/Caster/PdoCaster.php
Expand Up @@ -59,30 +59,30 @@ class PdoCaster

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

foreach (self::$pdoAttributes as $attr => $values) {
if (!isset($attr[0])) {
$attr = $values;
$values = array();
foreach (self::$pdoAttributes as $k => $v) {
if (!isset($k[0])) {
$k = $v;
$v = array();
}

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

$m = "\0~\0";
$a = (array) $c + array(
$a += array(
$m.'inTransaction' => method_exists($c, 'inTransaction'),
$m.'errorInfo' => $c->errorInfo(),
$m.'attributes' => $a,
$m.'attributes' => $attr,
);

if ($a[$m.'inTransaction']) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Expand Up @@ -267,7 +267,7 @@ private function callCaster($callback, $obj, $a, $stub, $isNested)
$a = $cast;
}
} catch (\Exception $e) {
$a["\0~\0"] = new ThrowingCasterException($callback, $e);
$a[(Stub::TYPE_OBJECT === $stub->type ? "\0~\0" : '').'⚠'] = new ThrowingCasterException($callback, $e);
}

return $a;
Expand Down
Expand Up @@ -16,21 +16,12 @@
*/
class ThrowingCasterException extends \Exception
{
private $caster;

/**
* @param callable $caster The failing caster
* @param \Exception $prev The exception thrown from the caster
*/
public function __construct($caster, \Exception $prev)
{
if (is_array($caster)) {
if (isset($caster[0]) && is_object($caster[0])) {
$caster[0] = get_class($caster[0]);
}
$caster = implode('::', $caster);
}
$this->caster = $caster;
parent::__construct(null, 0, $prev);
parent::__construct('Unexpected exception thrown from a caster: '.get_class($prev), 0, $prev);
}
}
52 changes: 52 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/CliDumperTest.php
Expand Up @@ -102,6 +102,58 @@ public function testGet()
b"bin-key-é" => ""
]
EOTXT
,
$out
);
}

public function testThrowingCaster()
{
$out = fopen('php://memory', 'r+b');

$dumper = new CliDumper();
$dumper->setColors(false);
$cloner = new VarCloner();
$cloner->addCasters(array(
':stream' => function () {
throw new \Exception('Foobar');
},
));
$line = __LINE__ - 3;
$file = __FILE__;
$ref = (int) $out;

$data = $cloner->cloneVar($out);
$dumper->dump($data, $out);
rewind($out);
$out = stream_get_contents($out);

$this->assertStringMatchesFormat(
<<<EOTXT
:stream {@{$ref}
wrapper_type: "PHP"
stream_type: "MEMORY"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://memory"
timed_out: false
blocked: true
eof: false
options: []
⚠: Symfony\Component\VarDumper\Exception\ThrowingCasterException {#%d
#message: "Unexpected exception thrown from a caster: Exception"
message: "Foobar"
trace: array:1 [
0 => array:2 [
"call" => "%s{closure}()"
"file" => "{$file}:{$line}"
]
]
}
}
EOTXT
,
$out
Expand Down

0 comments on commit 9944589

Please sign in to comment.