Lines
100.00%
4 / 4
Functions and Methods
100.00%
4 / 4
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ExceptionDetails | 100.00% | 4 / 4 | 100.00% | 4 / 4 | 5 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| fromException | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| fromThrowable | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | |||||
| toArray | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Model; | |
| 6 | ||
| 7 | use Stewart\Contracts\Exception\ExceptionReason; | |
| 8 | use Stewart\Contracts\Exception\StewartException; | |
| 9 | use Throwable; | |
| 10 | ||
| 11 | /** @phpstan-import-type ExceptionContext from StewartException */ | |
| 12 | final readonly class ExceptionDetails | |
| 13 | { | |
| 14 | public const string CONTEXT_KEY = 'stewartExceptionDetails'; | |
| 15 | ||
| 16 | /** @param ExceptionContext|null $context */ | |
| 17 | public function __construct( | |
| 18 | public string $class, | |
| 19 | public string $reason, | |
| 20 | public ?array $context, | |
| 21 | ) {} | |
| 22 | ||
| 23 | /** @param StewartException<ExceptionReason> $error */ | |
| 24 | public static function fromException(StewartException $error): self | |
| 25 | { | |
| 26 | return new self($error::class, (string) $error->reason->value, $error->context); | |
| 27 | } | |
| 28 | ||
| 29 | public static function fromThrowable(Throwable $error): ?self | |
| 30 | { | |
| 31 | return $error instanceof StewartException ? self::fromException($error) : null; | |
| 32 | } | |
| 33 | ||
| 34 | /** @return array{class: string, reason: string, context: ExceptionContext|null} */ | |
| 35 | public function toArray(): array | |
| 36 | { | |
| 37 | return ['class' => $this->class, 'reason' => $this->reason, 'context' => $this->context]; | |
| 38 | } | |
| 39 | } |