Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.00% |
34 / 40 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| WrappedError | |
85.00% |
34 / 40 |
|
75.00% |
3 / 4 |
12.49 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getStatusCode | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| jsonSerialize | |
77.78% |
21 / 27 |
|
0.00% |
0 / 1 |
6.40 | |||
| getTrace | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\HtmlBuilders\ErrorHandler; |
| 3 | |
| 4 | use Apie\Core\Exceptions\HttpStatusCodeException; |
| 5 | use Apie\TypeConverter\Exceptions\GetMultipleChainedExceptionInterface; |
| 6 | use JsonSerializable; |
| 7 | use Symfony\Component\HttpKernel\Exception\HttpException; |
| 8 | use Throwable; |
| 9 | |
| 10 | final class WrappedError implements JsonSerializable |
| 11 | { |
| 12 | public function __construct(public readonly Throwable $wrappedError) |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | public function getStatusCode(): int |
| 17 | { |
| 18 | if ($this->wrappedError instanceof HttpStatusCodeException || $this->wrappedError instanceof HttpException) { |
| 19 | return $this->wrappedError->getStatusCode(); |
| 20 | } |
| 21 | |
| 22 | return 500; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @return array<string, mixed> |
| 27 | */ |
| 28 | public function jsonSerialize(): array |
| 29 | { |
| 30 | $trace = $this->getTrace(); |
| 31 | $data = [ |
| 32 | [ |
| 33 | 'message' => $this->wrappedError->getMessage(), |
| 34 | 'code' => $this->wrappedError->getCode(), |
| 35 | 'class' => get_class($this->wrappedError), |
| 36 | 'trace' => $trace, |
| 37 | ] |
| 38 | ]; |
| 39 | $files = $trace->getFiles(); |
| 40 | if ($this->wrappedError instanceof GetMultipleChainedExceptionInterface) { |
| 41 | foreach ($this->wrappedError->getChainedExceptions() as $exception) { |
| 42 | $next = new WrappedError($exception); |
| 43 | $json = $next->jsonSerialize(); |
| 44 | if (isset($json['exceptions'])) { |
| 45 | $data = [...$data, ...$json['exceptions']]; |
| 46 | } |
| 47 | $files = array_merge($files, $json['files']); |
| 48 | } |
| 49 | } elseif ($this->wrappedError->getPrevious()) { |
| 50 | $previous = new WrappedError($this->wrappedError->getPrevious()); |
| 51 | $json = $previous->jsonSerialize(); |
| 52 | if (isset($json['exceptions'])) { |
| 53 | $data = [...$data, ...$json['exceptions']]; |
| 54 | } |
| 55 | $files = array_merge($files, $json['files']); |
| 56 | } |
| 57 | return [ |
| 58 | 'exceptions' => $data, |
| 59 | 'files' => $files, |
| 60 | ]; |
| 61 | } |
| 62 | |
| 63 | public function getTrace(): WrappedErrorTraceList |
| 64 | { |
| 65 | $stacktrace = $this->wrappedError->getTrace(); |
| 66 | $list = []; |
| 67 | $list[] = WrappedErrorTrace::fromNative([ |
| 68 | 'file' => $this->wrappedError->getFile(), |
| 69 | 'line' => $this->wrappedError->getLine(), |
| 70 | ]); |
| 71 | foreach ($stacktrace as $stacktraceItem) { |
| 72 | $list[]= WrappedErrorTrace::fromNative($stacktraceItem); |
| 73 | } |
| 74 | return new WrappedErrorTraceList($list); |
| 75 | } |
| 76 | } |