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)); + } + } } /**