/** Generate a response object from the API call response. * * @param JsonApi_Http_Response $response * * @return JsonApi_Response */ public static function factory(JsonApi_Http_Response $response) { try { switch ($response->getStatus()) { case JsonApi_Http_Response::STATUS_OK: $class = 'JsonApi_Response_Success'; break; case JsonApi_Http_Response::STATUS_FAIL: $class = 'JsonApi_Response_Failure'; break; default: throw new JsonApi_Response_Exception(sprintf('Unrecognized HTTP status code: %s (%s).', $response->getStatus(), $response->getStatus(true))); break; } return new $class($response); } catch (JsonApi_Exception $e) { /* Note that we only catch JsonApi_Exceptions; an error response is only * appropriate if the request succeeded, but something exploded on the * server. If JsonApi fails to send the request, that is a super-mega * exception case and must not be caught by JsonApi. */ $result = new JsonApi_Response_Error($response); $result->attachException($e); return $result; } }
/** Init the class instance. * * @param JsonApi_Http_Response $response * @param JsonApi_Exception $exception */ public function __construct(JsonApi_Http_Response $response, JsonApi_Exception $exception) { $this->_response = $response; $this->_exception = $exception; parent::__construct(sprintf('Got %s "%s" when requesting %s (%d %s).', get_class($exception), $exception->getMessage(), $response->getUri(), $response->getStatus(), $response->getStatus(true)), $response->getStatus(), $exception); }