Skip to content

Commit beea88a

Browse files
committed
Api: errors property is optional in error responses [Closes #9]
1 parent f8dc2d4 commit beea88a

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/Github/Api.php

+25-8
Original file line numberDiff line numberDiff line change
@@ -264,25 +264,22 @@ public function decode(Http\Response $response, array $okCodes = NULL)
264264
/** @var $content \stdClass */
265265
switch ($code) {
266266
case Http\Response::S400_BAD_REQUEST:
267-
throw new BadRequestException($content->message, $code, NULL, $response);
267+
throw new BadRequestException(self::errorMessage($content), $code, NULL, $response);
268268

269269
case Http\Response::S401_UNAUTHORIZED:
270-
throw new UnauthorizedException($content->message, $code, NULL, $response);
270+
throw new UnauthorizedException(self::errorMessage($content), $code, NULL, $response);
271271

272272
case Http\Response::S403_FORBIDDEN:
273273
if ($response->getHeader('X-RateLimit-Remaining') === '0') {
274-
throw new RateLimitExceedException($content->message, $code, NULL, $response);
274+
throw new RateLimitExceedException(self::errorMessage($content), $code, NULL, $response);
275275
}
276-
throw new ForbiddenException($content->message, $code, NULL, $response);
276+
throw new ForbiddenException(self::errorMessage($content), $code, NULL, $response);
277277

278278
case Http\Response::S404_NOT_FOUND:
279279
throw new NotFoundException('Resource not found or not authorized to access.', $code, NULL, $response);
280280

281281
case Http\Response::S422_UNPROCESSABLE_ENTITY:
282-
$message = $content->message . implode(', ', array_map(function($error) {
283-
return '[' . implode(':', (array) $error) . ']';
284-
}, $content->errors));
285-
throw new UnprocessableEntityException($message, $code, NULL, $response);
282+
throw new UnprocessableEntityException(self::errorMessage($content), $code, NULL, $response);
286283
}
287284

288285
$message = $okCodes === NULL ? '< 300' : implode(' or ', $okCodes);
@@ -546,4 +543,24 @@ private function walk(array $array, $cb)
546543
}
547544
}
548545

546+
547+
/**
548+
* @param \stdClass
549+
* @return string
550+
*/
551+
private static function errorMessage($content)
552+
{
553+
$message = isset($content->message)
554+
? $content->message
555+
: 'Unknown error';
556+
557+
if (isset($content->errors)) {
558+
$message .= implode(', ', array_map(function($error) {
559+
return '[' . implode(':', (array) $error) . ']';
560+
}, $content->errors));
561+
}
562+
563+
return $message;
564+
}
565+
549566
}

tests/Github/Api.decode.phpt

+7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ $e = Assert::exception(function() use ($api) {
7272
Assert::null($e->getPrevious());
7373

7474

75+
$e = Assert::exception(function() use ($api) {
76+
$response = new Milo\Github\Http\Response(422, ['Content-Type' => 'application/json'], '{"message":"error"}');
77+
$api->decode($response);
78+
}, 'Milo\Github\UnprocessableEntityException', 'error', 422);
79+
Assert::null($e->getPrevious());
80+
81+
7582
$e = Assert::exception(function() use ($api) {
7683
$response = new Milo\Github\Http\Response(422, ['Content-Type' => 'application/json'], '{"message":"error", "errors":[{"a":"b","c":"d"}]}');
7784
$api->decode($response);

0 commit comments

Comments
 (0)