diff --git a/lib/Github/HttpClient/Plugin/GithubExceptionThrower.php b/lib/Github/HttpClient/Plugin/GithubExceptionThrower.php index 21e6dd3434f..7a4bc1e0ba8 100644 --- a/lib/Github/HttpClient/Plugin/GithubExceptionThrower.php +++ b/lib/Github/HttpClient/Plugin/GithubExceptionThrower.php @@ -59,7 +59,7 @@ public function doHandleRequest(RequestInterface $request, callable $next, calla if (422 === $response->getStatusCode() && isset($content['errors'])) { $errors = []; foreach ($content['errors'] as $error) { - switch ($error['code']) { + switch ($error['code'] ?? null) { case 'missing': $errors[] = sprintf('The %s %s does not exist, for resource "%s"', $error['field'], $error['value'], $error['resource']); break; @@ -81,6 +81,12 @@ public function doHandleRequest(RequestInterface $request, callable $next, calla break; default: + if (is_string($error)) { + $errors[] = $error; + + break; + } + if (isset($error['message'])) { $errors[] = $error['message']; } diff --git a/test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php b/test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php index b367fe15779..e57f2cce70e 100644 --- a/test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php +++ b/test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php @@ -37,7 +37,7 @@ public function testHandleRequest(ResponseInterface $response, ExceptionInterfac if ($exception) { $this->expectException(get_class($exception)); $this->expectExceptionCode($exception->getCode()); - $this->expectExceptionMessage($exception->getMessage()); + $this->expectExceptionMessageRegExp('/'.preg_quote($exception->getMessage(), '/').'$/'); } $plugin->doHandleRequest( @@ -196,6 +196,22 @@ public static function responseProvider() ), 'exception' => new \Github\Exception\RuntimeException('This endpoint requires you to be authenticated.', 401), ], + 'Cannot delete active deployment' => [ + 'response' => new Response( + 422, + [ + 'content-type' => 'application/json', + ], + json_encode( + [ + 'message' => 'Validation Failed', + 'errors' => ['We cannot delete an active deployment unless it is the only deployment in a given environment.'], + 'documentation_url' => 'https://docs.github.com/rest/reference/repos#delete-a-deployment', + ] + ) + ), + 'exception' => new \Github\Exception\ValidationFailedException('Validation Failed: We cannot delete an active deployment unless it is the only deployment in a given environment.', 422), + ], ]; } }