Title: NetworkException
Published: May 20, 2026

---

# class NetworkException {}

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-exception-networkexception/?output_format=md#description)
 * [Methods](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-exception-networkexception/?output_format=md#methods)
 * [Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-exception-networkexception/?output_format=md#source)
 * [Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-exception-networkexception/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-exception-networkexception/?output_format=md#wp--skip-link--target)

Exception thrown for network-related errors.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-exception-networkexception/?output_format=md#description)󠁿

This includes HTTP transport errors, connection failures, timeouts, and other network-
related issues.

## 󠀁[Methods](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-exception-networkexception/?output_format=md#methods)󠁿

| Name | Description | 
| [NetworkException::fromPsr18NetworkException](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-exception-networkexception/frompsr18networkexception/) | Creates a NetworkException from a PSR-18 network exception. | 
| [NetworkException::getRequest](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-exception-networkexception/getrequest/) | Returns the request that failed as our Request DTO. |

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-exception-networkexception/?output_format=md#source)󠁿

    ```php
    class NetworkException extends RuntimeException
    {
        /**
         * The request that failed.
         *
         * @var Request|null
         */
        protected ?Request $request = null;
        /**
         * Returns the request that failed as our Request DTO.
         *
         * @since 0.2.0
         *
         * @return Request
         * @throws \RuntimeException If no request is available
         */
        public function getRequest(): Request
        {
            if ($this->request === null) {
                throw new \RuntimeException('Request object not available. This exception was directly instantiated. ' . 'Use a factory method that provides request context.');
            }
            return $this->request;
        }
        /**
         * Creates a NetworkException from a PSR-18 network exception.
         *
         * @since 0.2.0
         *
         * @param RequestInterface $psrRequest The PSR-7 request that failed.
         * @param \Throwable $networkException The PSR-18 network exception.
         * @return self
         */
        public static function fromPsr18NetworkException(RequestInterface $psrRequest, \Throwable $networkException): self
        {
            $request = Request::fromPsrRequest($psrRequest);
            $message = sprintf('Network error occurred while sending request to %s: %s', $request->getUri(), $networkException->getMessage());
            $exception = new self($message, 0, $networkException);
            $exception->request = $request;
            return $exception;
        }
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/php-ai-client/src/providers/http/exception/networkexception.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/php-ai-client/src/Providers/Http/Exception/NetworkException.php#L17)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/php-ai-client/src/Providers/Http/Exception/NetworkException.php#L17-L57)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-exception-networkexception/?output_format=md#changelog)󠁿

| Version | Description | 
| [0.2.0](https://developer.wordpress.org/reference/since/0.2.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwordpress-aiclient-providers-http-exception-networkexception%2F)
before being able to contribute a note or feedback.