diff --git a/src/Context/ProcessContext.php b/src/Context/ProcessContext.php index 64fca30..c0c97ed 100644 --- a/src/Context/ProcessContext.php +++ b/src/Context/ProcessContext.php @@ -281,11 +281,16 @@ public function join(?Cancellation $cancellation = null): mixed $data = $this->receiveExitResult($cancellation); $code = $this->process->join(); - if ($code !== 0) { - 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)); + } + } } /**