|
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
|
|
|
public function __construct(string $message = "", int $code = 0, ?\Throwable $previous = null) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->originalMessage = $message; |
|
20
|
|
|
parent::__construct($message, $code, $previous); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public static function createWithTrace( |
|
24
|
|
|
string $message, |
|
25
|
|
|
array $traces, |
|
26
|
|
|
?\Throwable $previous = null, |
|
27
|
|
|
): static { |
|
28
|
|
|
$result = new static( |
|
29
|
|
|
$message . ($traces === [] ? '' : "\nResolving trace:\n" . Tracer::renderTraceList($traces)), |
|
30
|
|
|
previous: $previous, |
|
31
|
|
|
); |
|
32
|
|
|
$result->originalMessage = $message; |
|
33
|
|
|
$result->containerTrace = $traces; |
|
34
|
|
|
return $result; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function extendTracedException(string $message, array $traces, self $exception): static |
|
38
|
|
|
{ |
|
39
|
|
|
$merge = $exception->containerTrace; |
|
40
|
|
|
if ($traces !== [] && $merge !== []) { |
|
41
|
|
|
// merge lat element of $traces with first element of $merge |
|
42
|
|
|
\array_push($traces[\count($traces) - 1], ...$merge[0]); |
|
43
|
|
|
unset($merge[0]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$traces = \array_merge($traces, $merge); |
|
47
|
|
|
return static::createWithTrace("$message\n{$exception->originalMessage}", $traces, previous: $exception); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|