From 0a8a530ce61283694411e707bb5319dc41cb27b1 Mon Sep 17 00:00:00 2001 From: Erik Gaal Date: Thu, 8 Jan 2026 11:40:40 +0100 Subject: [PATCH] feat: pass Throwable on Result::expect --- src/Result/Err.php | 4 ++++ src/Result/Ok.php | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Result/Err.php b/src/Result/Err.php index 257db0d..27e3bae 100644 --- a/src/Result/Err.php +++ b/src/Result/Err.php @@ -46,6 +46,10 @@ public function expect(string|Throwable $message): mixed throw $message; } + if ($this->err instanceof Throwable) { + throw new RuntimeException($message, previous: $this->err); + } + throw new RuntimeException($message); } diff --git a/src/Result/Ok.php b/src/Result/Ok.php index 83f61bb..7462bf7 100644 --- a/src/Result/Ok.php +++ b/src/Result/Ok.php @@ -47,7 +47,11 @@ public function expect(string|Throwable $message): mixed public function expectErr(Throwable|string $message): mixed { - throw $message instanceof Throwable ? $message : new RuntimeException($message); + if ($message instanceof Throwable) { + throw $message; + } + + throw new RuntimeException($message); } public function inspect(callable $f): Result