1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Core\Exception\Container; |
6
|
|
|
|
7
|
|
|
use Spiral\Core\Internal\Tracer; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Exception that contains trace of resolving. |
11
|
|
|
*/ |
12
|
|
|
class TracedContainerException extends ContainerException |
13
|
|
|
{ |
14
|
|
|
protected array $containerTrace = []; |
15
|
|
|
protected string $originalMessage = ''; |
16
|
|
|
|
17
|
791 |
|
public function __construct(string $message = "", int $code = 0, ?\Throwable $previous = null) |
18
|
|
|
{ |
19
|
791 |
|
$this->originalMessage = $message; |
20
|
791 |
|
parent::__construct($message, $code, $previous); |
21
|
|
|
} |
22
|
|
|
|
23
|
791 |
|
public static function createWithTrace( |
24
|
|
|
string $message, |
25
|
|
|
array $traces, |
26
|
|
|
?\Throwable $previous = null, |
27
|
|
|
): static { |
28
|
791 |
|
$result = new static( |
29
|
791 |
|
$message . ($traces === [] ? '' : "\nResolving trace:\n" . Tracer::renderTraceList($traces)), |
30
|
791 |
|
previous: $previous, |
31
|
791 |
|
); |
32
|
791 |
|
$result->originalMessage = $message; |
33
|
791 |
|
$result->containerTrace = $traces; |
34
|
791 |
|
return $result; |
35
|
|
|
} |
36
|
|
|
|
37
|
15 |
|
public static function extendTracedException(string $message, array $traces, self $exception): static |
38
|
|
|
{ |
39
|
15 |
|
$merge = $exception->containerTrace; |
40
|
15 |
|
if ($traces !== [] && $merge !== []) { |
41
|
|
|
// merge lat element of $traces with first element of $merge |
42
|
15 |
|
\array_push($traces[\count($traces) - 1], ...$merge[0]); |
43
|
15 |
|
unset($merge[0]); |
44
|
|
|
} |
45
|
|
|
|
46
|
15 |
|
$traces = \array_merge($traces, $merge); |
47
|
15 |
|
return static::createWithTrace("$message\n{$exception->originalMessage}", $traces, previous: $exception); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|