From f1cdbdfe966de16a1f91e1d0f26bafe3d80532cd Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Fri, 20 Dec 2024 19:46:22 -0600 Subject: [PATCH] Attach ExitFailure exception as previous exception --- src/Context/ProcessContext.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Context/ProcessContext.php b/src/Context/ProcessContext.php index 30658ff..7ddc33c 100644 --- a/src/Context/ProcessContext.php +++ b/src/Context/ProcessContext.php @@ -282,11 +282,16 @@ public function join(?Cancellation $cancellation = null): mixed $data = $this->receiveExitResult($cancellation); $code = $this->process->join(); - if ($code !== 0 && !($data instanceof ExitFailure)) { - throw new ContextException(\sprintf("Context exited with code %d", $code)); - } - return $data->getResult(); + try { + return $data->getResult(); + } finally { + if ($code !== 0) { + // If an ExitFailure throws above, the exception will be automatically attached as the previous + // exception on the instance thrown below. + throw new ContextException(\sprintf("Context exited with code %d", $code)); + } + } } /**