Exceptions

Exceptions – Handling package errors

Exceptions overview

All exceptions thrown in HTTP_Request2 are instances of HTTP_Request2_Exception. Since release 2.0.0beta1, HTTP_Request2 tries to throw a specialized subclass of that class and provide an error code when possible.

Checking for exception subclass and error code can help your application identify transient failures (e.g. HTTP_Request2_MessageException with error code HTTP_Request2_Exception::TIMEOUT). You can also use this information to display a user friendly error message instead of displaying Exception message that is more programmer friendly.

The following HTTP_Request2_Exception subclasses are available:

HTTP_Request2_NotImplementedException
Exception thrown in case of missing package features.
HTTP_Request2_LogicException
Exception that represents error in the program logic. These are usually thrown before request even starts. This exception implies an error on part of the programmer, like passing an invalid argument to a method or trying to use Curl Adapter when curl extension is disabled. They are unlikely to happen in production except for those dealing with local files.
HTTP_Request2_ConnectionException
Exception thrown when connection to a web or proxy server fails.
HTTP_Request2_MessageException
Thrown when sending or receiving HTTP message fails (this implies that connection succeeded, at least). Can be caused by network problems (e.g. timeout) or remote server sending invalid data.

Error codes

Subclasses of HTTP_Request2_Exception can contain two error codes:

The following package error codes are currently used:

Error codes used by HTTP_Request2_LogicException
Constant Meaning
HTTP_Request2_Exception::INVALID_ARGUMENT An invalid argument was passed to a method.
HTTP_Request2_Exception::MISSING_VALUE Some required value was not available.
HTTP_Request2_Exception::MISCONFIGURATION Request cannot be processed due to errors in PHP configuration (e.g. trying to use disabled PHP extension).
HTTP_Request2_Exception::READ_ERROR Error reading the local file.
Error codes used by HTTP_Request2_MessageException
Constant Meaning
HTTP_Request2_Exception::MALFORMED_RESPONSE Server returned a response that does not conform to HTTP protocol. This means that even status line of response message could not be parsed.
HTTP_Request2_Exception::DECODE_ERROR Failure decoding Content-Encoding or Transfer-Encoding of response.
HTTP_Request2_Exception::TIMEOUT Operation timed out.
HTTP_Request2_Exception::TOO_MANY_REDIRECTS Number of redirects exceeded 'max_redirects' configuration parameter.
HTTP_Request2_Exception::NON_HTTP_REDIRECT Redirect to a protocol other than http(s)://.
    Поддержать сайт на родительском проекте КГБ