diff options
Diffstat (limited to 'inc/mailgun/php-http/httplug/src/Exception')
4 files changed, 145 insertions, 0 deletions
diff --git a/inc/mailgun/php-http/httplug/src/Exception/HttpException.php b/inc/mailgun/php-http/httplug/src/Exception/HttpException.php new file mode 100644 index 0000000..f4f32a4 --- /dev/null +++ b/inc/mailgun/php-http/httplug/src/Exception/HttpException.php @@ -0,0 +1,74 @@ +<?php + +namespace Http\Client\Exception; + +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; + +/** + * Thrown when a response was received but the request itself failed. + * + * In addition to the request, this exception always provides access to the response object. + * + * @author Márk Sági-Kazár <mark.sagikazar@gmail.com> + */ +class HttpException extends RequestException +{ + /** + * @var ResponseInterface + */ + protected $response; + + /** + * @param string $message + * @param RequestInterface $request + * @param ResponseInterface $response + * @param \Exception|null $previous + */ + public function __construct( + $message, + RequestInterface $request, + ResponseInterface $response, + \Exception $previous = null + ) { + parent::__construct($message, $request, $previous); + + $this->response = $response; + $this->code = $response->getStatusCode(); + } + + /** + * Returns the response. + * + * @return ResponseInterface + */ + public function getResponse() + { + return $this->response; + } + + /** + * Factory method to create a new exception with a normalized error message. + * + * @param RequestInterface $request + * @param ResponseInterface $response + * @param \Exception|null $previous + * + * @return HttpException + */ + public static function create( + RequestInterface $request, + ResponseInterface $response, + \Exception $previous = null + ) { + $message = sprintf( + '[url] %s [http method] %s [status code] %s [reason phrase] %s', + $request->getRequestTarget(), + $request->getMethod(), + $response->getStatusCode(), + $response->getReasonPhrase() + ); + + return new self($message, $request, $response, $previous); + } +} diff --git a/inc/mailgun/php-http/httplug/src/Exception/NetworkException.php b/inc/mailgun/php-http/httplug/src/Exception/NetworkException.php new file mode 100644 index 0000000..f2198e5 --- /dev/null +++ b/inc/mailgun/php-http/httplug/src/Exception/NetworkException.php @@ -0,0 +1,14 @@ +<?php + +namespace Http\Client\Exception; + +/** + * Thrown when the request cannot be completed because of network issues. + * + * There is no response object as this exception is thrown when no response has been received. + * + * @author Márk Sági-Kazár <mark.sagikazar@gmail.com> + */ +class NetworkException extends RequestException +{ +} diff --git a/inc/mailgun/php-http/httplug/src/Exception/RequestException.php b/inc/mailgun/php-http/httplug/src/Exception/RequestException.php new file mode 100644 index 0000000..cdce14b --- /dev/null +++ b/inc/mailgun/php-http/httplug/src/Exception/RequestException.php @@ -0,0 +1,43 @@ +<?php + +namespace Http\Client\Exception; + +use Psr\Http\Message\RequestInterface; + +/** + * Exception for when a request failed, providing access to the failed request. + * + * This could be due to an invalid request, or one of the extending exceptions + * for network errors or HTTP error responses. + * + * @author Márk Sági-Kazár <mark.sagikazar@gmail.com> + */ +class RequestException extends TransferException +{ + /** + * @var RequestInterface + */ + private $request; + + /** + * @param string $message + * @param RequestInterface $request + * @param \Exception|null $previous + */ + public function __construct($message, RequestInterface $request, \Exception $previous = null) + { + $this->request = $request; + + parent::__construct($message, 0, $previous); + } + + /** + * Returns the request. + * + * @return RequestInterface + */ + public function getRequest() + { + return $this->request; + } +} diff --git a/inc/mailgun/php-http/httplug/src/Exception/TransferException.php b/inc/mailgun/php-http/httplug/src/Exception/TransferException.php new file mode 100644 index 0000000..a858cf5 --- /dev/null +++ b/inc/mailgun/php-http/httplug/src/Exception/TransferException.php @@ -0,0 +1,14 @@ +<?php + +namespace Http\Client\Exception; + +use Http\Client\Exception; + +/** + * Base exception for transfer related exceptions. + * + * @author Márk Sági-Kazár <mark.sagikazar@gmail.com> + */ +class TransferException extends \RuntimeException implements Exception +{ +} |
