Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: check response always, not only through makeRequest #674

Open
wants to merge 7 commits into
base: v3-v2021-02-25
Choose a base branch
from
1 change: 1 addition & 0 deletions lib/recurly/base_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ private function _getResponse(\Recurly\Request $request): \Recurly\Response
'response_headers' => $response->getHeaders()
]
);
$response->assertValid(); // throws \Recurly\RecurlyError if response is bad

return $response;
}
Expand Down
16 changes: 10 additions & 6 deletions lib/recurly/recurly_error.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ public static function fromResponse(\Recurly\Response $response): \Recurly\Recur
$api_error = \Recurly\Resources\ErrorMayHaveTransaction::fromResponse($response, $error);
return new $klass($error->message, $api_error);
}
} else {
$error_type = static::errorFromStatus($response->getStatusCode());
$klass = static::titleize($error_type, '\\Recurly\\Errors\\');
if (class_exists($klass)) {
return new $klass('An unexpected error has occurred');
}
}

// "Content-type: application/json" may appear without a body after a HEAD request.
// If the above failed, try guessing from the status code.
$error_type = static::errorFromStatus($response->getStatusCode());
$klass = static::titleize($error_type, '\\Recurly\\Errors\\');
if (class_exists($klass)) {
return new $klass('An unexpected error has occurred');
}

// No valid error type was found, sorry.
$klass = static::_defaultErrorType($response);
return new $klass('An unexpected error has occurred');

Expand Down
24 changes: 15 additions & 9 deletions lib/recurly/response.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,29 @@ public function getRequest(): \Recurly\Request
return $this->_request;
}

/**
* Makes sure the response is valid. Throws RecurlyError otherwise.
*/
public function assertValid(): void
{
if ($this->_status_code < 200 || $this->_status_code >= 300) {
throw \Recurly\RecurlyError::fromResponse($this);
}
}

/**
* Converts the Response into a \Recurly\RecurlyResource
*
* @return \Recurly\RecurlyResource
*/
public function toResource(): \Recurly\RecurlyResource
{
if ($this->_status_code >= 200 && $this->_status_code < 300) {
if (empty($this->_response)) {
return \Recurly\RecurlyResource::fromEmpty($this);
} elseif (in_array($this->getContentType(), static::BINARY_TYPES)) {
return \Recurly\RecurlyResource::fromBinary($this->_response, $this);
} else {
return \Recurly\RecurlyResource::fromResponse($this);
}
if (empty($this->_response)) {
return \Recurly\RecurlyResource::fromEmpty($this);
} elseif (in_array($this->getContentType(), static::BINARY_TYPES)) {
return \Recurly\RecurlyResource::fromBinary($this->_response, $this);
} else {
throw \Recurly\RecurlyError::fromResponse($this);
return \Recurly\RecurlyResource::fromResponse($this);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Response_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ public function testToResourceEmpty(): void
$this->assertInstanceOf(\Recurly\EmptyResource::class, $result);
}

public function testToResourceError(): void
public function testAssertValid(): void
{
$this->expectException(\Recurly\RecurlyError::class);
$response = new Response('', $this->request);
$response->setHeaders(['HTTP/1.1 403 Forbidden']);
$result = $response->toResource();
$result = $response->assertValid();
}

public function testGetRawResponse(): void
Expand Down