{"record":{"id":"bf8bfa04a2a88201","repo":"guzzle/promises","slug":"exceptionfor-this-result","errorCode":null,"errorMessage":"exceptionFor($this->result)","messagePattern":"exceptionFor\\(\\$this->result\\)","errorType":"exception","errorClass":"dynamic (Create::exceptionFor)","httpStatus":null,"severity":"error","filePath":"src/Promise.php","lineNumber":119,"sourceCode":"     */\n    public function otherwise(callable $onRejected): PromiseInterface\n    {\n        return $this->then(null, $onRejected);\n    }\n\n    public function wait(bool $unwrap = true)\n    {\n        $this->waitIfPending();\n\n        if ($this->result instanceof PromiseInterface) {\n            return $this->result->wait($unwrap);\n        }\n        if ($unwrap) {\n            if ($this->state === self::FULFILLED) {\n                return $this->result;\n            }\n            // It's rejected so \"unwrap\" and throw an exception.\n            throw Create::exceptionFor($this->result);\n        }\n\n        return null;\n    }\n\n    public function getState(): string\n    {\n        return $this->state;\n    }\n\n    public function cancel(): void\n    {\n        if ($this->state !== self::PENDING) {\n            return;\n        }\n\n        $this->waitFn = $this->waitList = null;\n","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/guzzle/promises/blob/42118e66a53c492effaf92bc357e931985d5c6f9/src/Promise.php#L101-L137","documentation":"Promise::wait(true) synchronously resolves the promise, then returns the fulfillment value. If the promise settled as rejected, the rejection reason is passed through Create::exceptionFor() and thrown. This is how Guzzle Promises surfaces asynchronous failures to synchronous call sites: the rejection reason becomes a normal PHP exception at the wait() call.","triggerScenarios":"Calling $promise->wait() (or wait(true), the default) on a Promise that has been or becomes rejected - e.g. an inner promise rejected with an exception, a coroutine thrown, or rejection() invoked before wait. The thrown value is whatever the promise was rejected with, normalized via Create::exceptionFor().","commonSituations":"Blocking on an HTTP request with Guzzle that failed (4xx/5xx mapped to exceptions like ConnectException or BadResponseException); a promise chain where an earlier then() handler threw; calling wait() on a promise rejected because a nested coroutine failed; tests that reject promises and expect wait() to propagate the reason.","solutions":["Wrap the $promise->wait() call in try/catch and handle the rejection reason exception.","Attach an ->otherwise() or ->then(null, $onRejected) handler before wait() so rejection is handled in the promise chain instead of thrown.","Check $promise->getState() === Promise::FULFILLED and call wait(false) or inspect the state to avoid the unwrap throw when you only need settlement.","Fix the root cause of the rejection (network error, invalid request, upstream failure) rather than only catching it."],"exampleFix":"// before\n$value = $promise->wait(); // throws if promise was rejected\n\n// after\ntry {\n    $value = $promise->wait();\n} catch (\\Throwable $e) {\n    // handle the rejection reason surfaced by wait(true)\n    $value = null;\n}","handlingStrategy":"try-catch","validationCode":"// Before waiting, only unwrap when the promise is settled and fulfilled:\nuse GuzzleHttp\\Promise\\Promise;\nif ($promise->getState() === Promise::FULFILLED) {\n    $value = $promise->wait(); // safe: will not throw\n}","typeGuard":"// Narrow by settlement state before unwrapping\nfunction fulfilledValue(\\GuzzleHttp\\Promise\\PromiseInterface $p) {\n    if ($p->getState() === \\GuzzleHttp\\Promise\\Promise::FULFILLED) {\n        return $p->wait(); // returns value, never throws\n    }\n    return null; // pending or rejected - do not unwrap\n}","tryCatchPattern":"try {\n    $value = $promise->wait();\n} catch (\\GuzzleHttp\\Exception\\ConnectException $e) {\n    // network/connection failure\n} catch (\\GuzzleHttp\\Exception\\RequestException $e) {\n    // HTTP-level failure\n} catch (\\Throwable $e) {\n    // any other rejection reason thrown by wait(true)\n}","preventionTips":["Always treat wait() as throwing: wrap it in try/catch wherever you block on a promise.","Attach ->otherwise() handlers to chains before calling wait() so rejections are handled in-promise.","Check getState() === FULFILLED before unwrapping when rejection is not expected.","Pass wait(false) when you only need the promise settled, not its value.","In tests, assert rejection via caught exceptions from wait() rather than letting them bubble."],"tags":["promise","rejection","unhandled-rejection","wait","guzzle-promises"],"backgroundTag":"unhandled-promise-rejection","analyzedSha":"42118e66a53c492effaf92bc357e931985d5c6f9","analyzedAt":"2026-09-14T00:10:14.865Z","contentChangedAt":"2026-09-14T00:10:14.865Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}