[VarDumper] Fix dumping ThrowingCasterException
This commit is contained in:
parent
2981d01baa
commit
9944589068
@ -71,27 +71,17 @@ class ExceptionCaster
|
|||||||
{
|
{
|
||||||
$b = (array) $a["\0Exception\0previous"];
|
$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"])) {
|
if (isset($b["\0*\0message"])) {
|
||||||
$a["\0~\0message"] = $b["\0*\0message"];
|
$a["\0~\0message"] = $b["\0*\0message"];
|
||||||
}
|
}
|
||||||
if (isset($b["\0*\0file"])) {
|
|
||||||
$a["\0~\0file"] = $b["\0*\0file"];
|
if (isset($a["\0Exception\0trace"])) {
|
||||||
}
|
$b["\0Exception\0trace"][0] += array(
|
||||||
if (isset($b["\0*\0line"])) {
|
'file' => $b["\0*\0file"],
|
||||||
$a["\0~\0line"] = $b["\0*\0line"];
|
'line' => $b["\0*\0line"],
|
||||||
}
|
);
|
||||||
if (isset($b["\0Exception\0trace"])) {
|
array_splice($b["\0Exception\0trace"], -1 - count($a["\0Exception\0trace"]));
|
||||||
|
static::filterTrace($b["\0Exception\0trace"], false);
|
||||||
$a["\0~\0trace"] = $b["\0Exception\0trace"];
|
$a["\0~\0trace"] = $b["\0Exception\0trace"];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,30 +59,30 @@ class PdoCaster
|
|||||||
|
|
||||||
public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested)
|
public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested)
|
||||||
{
|
{
|
||||||
$a = array();
|
$attr = array();
|
||||||
$errmode = $c->getAttribute(\PDO::ATTR_ERRMODE);
|
$errmode = $c->getAttribute(\PDO::ATTR_ERRMODE);
|
||||||
$c->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
$c->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||||
|
|
||||||
foreach (self::$pdoAttributes as $attr => $values) {
|
foreach (self::$pdoAttributes as $k => $v) {
|
||||||
if (!isset($attr[0])) {
|
if (!isset($k[0])) {
|
||||||
$attr = $values;
|
$k = $v;
|
||||||
$values = array();
|
$v = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$a[$attr] = 'ERRMODE' === $attr ? $errmode : $c->getAttribute(constant("PDO::ATTR_{$attr}"));
|
$attr[$k] = 'ERRMODE' === $k ? $errmode : $c->getAttribute(constant('PDO::ATTR_'.$k));
|
||||||
if ($values && isset($values[$a[$attr]])) {
|
if ($v && isset($v[$attr[$k]])) {
|
||||||
$a[$attr] = new ConstStub($values[$a[$attr]], $a[$attr]);
|
$attr[$k] = new ConstStub($v[$attr[$k]], $attr[$k]);
|
||||||
}
|
}
|
||||||
} catch (\Exception $m) {
|
} catch (\Exception $e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$m = "\0~\0";
|
$m = "\0~\0";
|
||||||
$a = (array) $c + array(
|
$a += array(
|
||||||
$m.'inTransaction' => method_exists($c, 'inTransaction'),
|
$m.'inTransaction' => method_exists($c, 'inTransaction'),
|
||||||
$m.'errorInfo' => $c->errorInfo(),
|
$m.'errorInfo' => $c->errorInfo(),
|
||||||
$m.'attributes' => $a,
|
$m.'attributes' => $attr,
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($a[$m.'inTransaction']) {
|
if ($a[$m.'inTransaction']) {
|
||||||
|
@ -267,7 +267,7 @@ abstract class AbstractCloner implements ClonerInterface
|
|||||||
$a = $cast;
|
$a = $cast;
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$a["\0~\0⚠"] = new ThrowingCasterException($callback, $e);
|
$a[(Stub::TYPE_OBJECT === $stub->type ? "\0~\0" : '').'⚠'] = new ThrowingCasterException($callback, $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $a;
|
return $a;
|
||||||
|
@ -16,21 +16,12 @@ namespace Symfony\Component\VarDumper\Exception;
|
|||||||
*/
|
*/
|
||||||
class ThrowingCasterException extends \Exception
|
class ThrowingCasterException extends \Exception
|
||||||
{
|
{
|
||||||
private $caster;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param callable $caster The failing caster
|
* @param callable $caster The failing caster
|
||||||
* @param \Exception $prev The exception thrown from the caster
|
* @param \Exception $prev The exception thrown from the caster
|
||||||
*/
|
*/
|
||||||
public function __construct($caster, \Exception $prev)
|
public function __construct($caster, \Exception $prev)
|
||||||
{
|
{
|
||||||
if (is_array($caster)) {
|
parent::__construct('Unexpected exception thrown from a caster: '.get_class($prev), 0, $prev);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,6 +102,58 @@ array:25 [
|
|||||||
b"bin-key-é" => ""
|
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
|
EOTXT
|
||||||
,
|
,
|
||||||
$out
|
$out
|
||||||
|
Reference in New Issue
Block a user