Skip to content

Commit e6ed4a4

Browse files
author
Grace Yim
committed
Try to fix ApiRawRequester.request
1 parent bb940b4 commit e6ed4a4

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

lib/ToopherApi.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,22 +179,21 @@ private function request($method, $endpoint, $parameters = array(), $rawRequest
179179
}
180180

181181
$err = json_decode($resultBody, true);
182-
if ($err === NULL) {
182+
if ($err) {
183183
$jsonError = $this->json_error_to_string(json_last_error());
184184
if (!empty($jsonError))
185185
{
186186
error_log(sprintf('Error parsing response body JSON: %s', $jsonError));
187187
error_log(sprintf('Response body: %s', $result->getBody()));
188188
throw new ToopherRequestException(sprintf('JSON Parsing Error: %s', $jsonError));
189189
}
190-
}
191-
192-
if(array_key_exists('error_message', $err))
193-
{
194-
throw new ToopherRequestException($err['error_message'], $err['error_code']);
195190
} else {
196-
throw new ToopherRequestException(sprintf('%s - %s', $result->getReasonPhrase(), $resultBody), $result->getStatus());
191+
if(array_key_exists('error_message', $err))
192+
{
193+
throw new ToopherRequestException($err['error_message'], $err['error_code']);
194+
}
197195
}
196+
throw new ToopherRequestException(sprintf('%s - %s', $result->getReasonPhrase(), $resultBody), $result->getStatus());
198197
}
199198

200199
if ($rawRequest) {
@@ -210,7 +209,7 @@ private function request($method, $endpoint, $parameters = array(), $rawRequest
210209
throw new ToopherRequestException(sprintf('JSON Parsing Error: %s', $jsonError));
211210
}
212211
}
213-
return $decoded;
212+
return $decoded;
214213
}
215214
}
216215

test/ToopherApiTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,9 @@ public function test403WithEmptyMessageRaisesToopherRequestException()
510510
* @expectedException ToopherRequestException
511511
* @expectedExceptionMessage JSON Parsing Error: Syntax error, malformed JSON
512512
*/
513-
public function test200WithEmptyMessageRaisesToopherRequestException()
513+
public function test403WithBadJsonRaisesToopherRequestException()
514514
{
515-
$resp = new HTTP_Request2_Response('HTTP/1.1 200 OK', false, 'https://api.toopher.com/v1/authentication_requests/1');
515+
$resp = new HTTP_Request2_Response('HTTP/1.1 403 Forbidden', false, 'https://api.toopher.com/v1/authentication_requests/1');
516516
$resp->appendBody('stuff');
517517
$this->mock->addResponse($resp);
518518
$toopher = $this->getToopherApi($this->mock);
@@ -523,9 +523,9 @@ public function test200WithEmptyMessageRaisesToopherRequestException()
523523
* @expectedException ToopherRequestException
524524
* @expectedExceptionMessage JSON Parsing Error: Syntax error, malformed JSON
525525
*/
526-
public function test403WithBadJsonRaisesToopherRequestException()
526+
public function test200WithEmptyMessageRaisesToopherRequestException()
527527
{
528-
$resp = new HTTP_Request2_Response('HTTP/1.1 403 Forbidden', false, 'https://api.toopher.com/v1/authentication_requests/1');
528+
$resp = new HTTP_Request2_Response('HTTP/1.1 200 OK', false, 'https://api.toopher.com/v1/authentication_requests/1');
529529
$resp->appendBody('stuff');
530530
$this->mock->addResponse($resp);
531531
$toopher = $this->getToopherApi($this->mock);

0 commit comments

Comments
 (0)