private function fetchDirectory(): Promise {
        return call(function () {
            try {
                $this->logger->debug('Fetching directory from {uri}', [
                    'uri' => $this->directoryUri
                ]);

                /** @var Response $response */
                $response = yield $this->http->request($this->directoryUri);
                $directory = json_decode(yield $response->getBody(), true);

                if ($response->getStatus() !== 200) {
                    $error = $directory;

                    if (isset($error['type'], $error['detail'])) {
                        throw new AcmeException("Invalid directory response: {$error['detail']}", $error['type']);
                    }

                    throw new AcmeException('Invalid directory response. HTTP response code: ' . $response->getStatus());
                }

                if (empty($directory)) {
                    throw new AcmeException('Invalid empty directory.');
                }

                $this->directory = $directory;
                $this->saveNonce($response);
            } catch (Throwable $e) {
                throw new AcmeException('Could not obtain directory: ' . $e->getMessage(), null, $e);
            }
        });
    }