[VarDumper] Fix dumping ThrowingCasterException
This commit is contained in:
parent
2981d01baa
commit
9944589068
@ -71,27 +71,17 @@ class ExceptionCaster
|
||||
{
|
||||
$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"];
|
||||
}
|
||||
|
||||
|
@ -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']) {
|
||||
|
@ -267,7 +267,7 @@ abstract class AbstractCloner implements ClonerInterface
|
||||
$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;
|
||||
|
@ -16,21 +16,12 @@ namespace Symfony\Component\VarDumper\Exception;
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -102,6 +102,58 @@ array:25 [
|
||||
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
|
||||
|
Reference in New Issue
Block a user