-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ExceptionWrapper.php
138 lines (113 loc) · 3.61 KB
/
ExceptionWrapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Framework;
use const PHP_VERSION_ID;
use function array_keys;
use function get_class;
use function spl_object_hash;
use PHPUnit\Util\Filter;
use Throwable;
use WeakReference;
/**
* Wraps Exceptions thrown by code under test.
*
* Re-instantiates Exceptions thrown by user-space code to retain their original
* class names, properties, and stack traces (but without arguments).
*
* Unlike PHPUnit\Framework\Exception, the complete stack of previous Exceptions
* is processed.
*
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class ExceptionWrapper extends Exception
{
/**
* @var string
*/
protected $className;
/**
* @var null|ExceptionWrapper
*/
protected $previous;
/**
* @var null|WeakReference<Throwable>
*/
private $originalException;
public function __construct(Throwable $t)
{
// PDOException::getCode() is a string.
// @see https://php.net/manual/en/class.pdoexception.php#95812
parent::__construct($t->getMessage(), (int) $t->getCode());
$this->setOriginalException($t);
}
public function __toString(): string
{
$string = TestFailure::exceptionToString($this);
if ($trace = Filter::getFilteredStacktrace($this)) {
$string .= "\n" . $trace;
}
if ($this->previous) {
$string .= "\nCaused by\n" . $this->previous;
}
return $string;
}
public function getClassName(): string
{
return $this->className;
}
public function getPreviousWrapped(): ?self
{
return $this->previous;
}
public function setClassName(string $className): void
{
$this->className = $className;
}
public function setOriginalException(Throwable $t): void
{
$this->originalException($t);
$this->className = get_class($t);
$this->file = $t->getFile();
$this->line = $t->getLine();
$this->serializableTrace = $t->getTrace();
foreach (array_keys($this->serializableTrace) as $key) {
unset($this->serializableTrace[$key]['args']);
}
if ($t->getPrevious()) {
$this->previous = new self($t->getPrevious());
}
}
public function getOriginalException(): ?Throwable
{
return $this->originalException();
}
/**
* Method to contain static originalException to exclude it from stacktrace to prevent the stacktrace contents,
* which can be quite big, from being garbage-collected, thus blocking memory until shutdown.
*
* Approach works both for var_dump() and var_export() and print_r().
*/
private function originalException(Throwable $exceptionToStore = null): ?Throwable
{
// drop once PHP 7.3 support is removed
if (PHP_VERSION_ID < 70400) {
static $originalExceptions;
$instanceId = spl_object_hash($this);
if ($exceptionToStore) {
$originalExceptions[$instanceId] = $exceptionToStore;
}
return $originalExceptions[$instanceId] ?? null;
}
if ($exceptionToStore) {
$this->originalException = WeakReference::create($exceptionToStore);
}
return $this->originalException !== null ? $this->originalException->get() : null;
}
}