Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
5 / 5 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ResultException | |
100.00% |
5 / 5 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getErrors | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Eco\Exceptions; |
| 6 | |
| 7 | use Eco\Error; |
| 8 | |
| 9 | /** |
| 10 | * Thrown when a {@see Result} is unwrapped in a failed state. |
| 11 | * |
| 12 | * Extends RuntimeException so callers can catch either this specific |
| 13 | * class or the broader RuntimeException — whichever fits their context. |
| 14 | */ |
| 15 | final class ResultException extends \RuntimeException |
| 16 | { |
| 17 | /** @var Error[] */ |
| 18 | private array $errors; |
| 19 | |
| 20 | /** |
| 21 | * @param Error[] $errors |
| 22 | */ |
| 23 | public function __construct(array $errors, string $message = '', int $code = 0, ?\Throwable $previous = null) |
| 24 | { |
| 25 | $this->errors = $errors; |
| 26 | |
| 27 | if ($message === '') { |
| 28 | $message = implode(', ', array_map(fn(Error $e) => $e->message, $errors)); |
| 29 | } |
| 30 | |
| 31 | parent::__construct($message, $code, $previous); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Returns the errors that caused this exception. |
| 36 | * |
| 37 | * @return Error[] |
| 38 | */ |
| 39 | public function getErrors(): array |
| 40 | { |
| 41 | return $this->errors; |
| 42 | } |
| 43 | } |