Skip to content

Commit

Permalink
Api: errors property is optional in error responses [Closes #9]
Browse files Browse the repository at this point in the history
  • Loading branch information
milo committed Mar 16, 2015
1 parent f8dc2d4 commit beea88a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/Github/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,25 +264,22 @@ public function decode(Http\Response $response, array $okCodes = NULL)
/** @var $content \stdClass */
switch ($code) {
case Http\Response::S400_BAD_REQUEST:
throw new BadRequestException($content->message, $code, NULL, $response);
throw new BadRequestException(self::errorMessage($content), $code, NULL, $response);

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

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

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

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

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


/**
* @param \stdClass
* @return string
*/
private static function errorMessage($content)
{
$message = isset($content->message)
? $content->message
: 'Unknown error';

if (isset($content->errors)) {
$message .= implode(', ', array_map(function($error) {
return '[' . implode(':', (array) $error) . ']';
}, $content->errors));
}

return $message;
}

}
7 changes: 7 additions & 0 deletions tests/Github/Api.decode.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ $e = Assert::exception(function() use ($api) {
Assert::null($e->getPrevious());


$e = Assert::exception(function() use ($api) {
$response = new Milo\Github\Http\Response(422, ['Content-Type' => 'application/json'], '{"message":"error"}');
$api->decode($response);
}, 'Milo\Github\UnprocessableEntityException', 'error', 422);
Assert::null($e->getPrevious());


$e = Assert::exception(function() use ($api) {
$response = new Milo\Github\Http\Response(422, ['Content-Type' => 'application/json'], '{"message":"error", "errors":[{"a":"b","c":"d"}]}');
$api->decode($response);
Expand Down

0 comments on commit beea88a

Please sign in to comment.