mirror of
https://github.com/SkillDisplay/PHPToolKit.git
synced 2024-12-18 01:26:09 +01:00
Handle 404 and "Oops" API responses
The API is created by a TYPO3. It provides proper 404 for unavailable Skill IDs. An "Oops" is returned for unavailable SkillSet IDs.
This commit is contained in:
parent
8179104233
commit
94c2038658
3 changed files with 42 additions and 21 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue