diff --git a/src/Api/Skill.php b/src/Api/Skill.php index 730abaa..44ec213 100644 --- a/src/Api/Skill.php +++ b/src/Api/Skill.php @@ -24,6 +24,7 @@ namespace SkillDisplay\PHPToolKit\Api; */ use GuzzleHttp\Client; +use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Psr7\Request; use SkillDisplay\PHPToolKit\Configuration\Settings; use SkillDisplay\PHPToolKit\Entity\Skill as Entity; @@ -55,14 +56,21 @@ class Skill } $url = $this->settings->getAPIUrl() . '/api/v1/skill/' . $id; - $result = $this->client->send(new Request( - 'GET', - $url, - [ - 'Content-Type' => 'application/json', - 'x-api-key' => $this->settings->getApiKey() - ] - )); + try { + $result = $this->client->send(new Request( + 'GET', + $url, + [ + 'Content-Type' => 'application/json', + 'x-api-key' => $this->settings->getApiKey() + ] + )); + } catch (ClientException $e) { + if ($e->getCode() === 404) { + throw new \InvalidArgumentException('Given Skill with id "' . $id . '" not available.', 1601881616); + } + throw $e; + } if ($result->getStatusCode() !== 200) { throw new \Exception('Did not get proper response for skill.', 1600694312); diff --git a/src/Api/SkillSet.php b/src/Api/SkillSet.php index f54b225..1bb29f1 100644 --- a/src/Api/SkillSet.php +++ b/src/Api/SkillSet.php @@ -55,19 +55,32 @@ class SkillSet } $url = $this->settings->getAPIUrl() . '/api/v1/skillset/' . $id; - $result = $this->client->send(new Request( - 'GET', - $url, - [ - 'Content-Type' => 'application/json', - 'x-api-key' => $this->settings->getApiKey() - ] - )); - - if ($result->getStatusCode() !== 200) { - throw new \Exception('Did not get proper response for skill.', 1600694312); + try { + $result = $this->client->send(new Request( + 'GET', + $url, + [ + 'Content-Type' => 'application/json', + 'x-api-key' => $this->settings->getApiKey() + ] + )); + } catch (ClientException $e) { + if ($e->getCode() === 404) { + throw new \InvalidArgumentException('Given SkillSet with id "' . $id . '" not available.', 1601881616); + } + throw $e; } - return Entity::createFromJson((string) $result->getBody()); + if ($result->getStatusCode() !== 200) { + throw new \Exception('Did not get proper response for SkillSet.', 1600694312); + } + + $body = (string) $result->getBody(); + + if (strpos($body, 'Oops, an error occurred') !== false) { + throw new \Exception('Did not get proper response for SkillSet. SkillSet with id "' . $id . '" does probably not exist.', 1600694312); + } + + return Entity::createFromJson($body); } } diff --git a/tests/Unit/Api/SkillSetTest.php b/tests/Unit/Api/SkillSetTest.php index 2368c77..cb4ff9e 100644 --- a/tests/Unit/Api/SkillSetTest.php +++ b/tests/Unit/Api/SkillSetTest.php @@ -108,7 +108,7 @@ class SkillSetTest extends TestCase $client->reveal() ); - $this->expectExceptionMessage('Did not get proper response for skill.'); + $this->expectExceptionMessage('Did not get proper response for SkillSet.'); $this->expectExceptionCode(1600694312); $result = $subject->getById(10);