Skip to content

Commit e20aff3

Browse files
authored
Merge pull request #13 from Flowpack/bugfix/api-errors
BUGFIX: Handle response errors in api requests
2 parents 17bc743 + 9ae3b7f commit e20aff3

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

Classes/PrunnerApiService.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Flowpack\Prunner\ValueObject\JobId;
1010
use Flowpack\Prunner\ValueObject\PipelineName;
1111
use GuzzleHttp\Client;
12+
use GuzzleHttp\Exception\GuzzleException;
1213
use Neos\Flow\Annotations as Flow;
1314
use Psr\Http\Message\ResponseInterface;
1415
use Symfony\Component\Yaml\Exception\ParseException;
@@ -53,14 +54,25 @@ class PrunnerApiService
5354
public function loadPipelinesAndJobs(): PipelinesAndJobsResponse
5455
{
5556
$resultString = $this->apiCall('GET', 'pipelines/jobs', null)->getBody()->getContents();
56-
$result = json_decode($resultString, true);
57+
try {
58+
$result = json_decode($resultString, true, 512, JSON_THROW_ON_ERROR);
59+
} catch (\JsonException $e) {
60+
throw new \RuntimeException('Could not decode JSON response from prunner API: ' . $e->getMessage(), 1707485801);
61+
}
5762
return PipelinesAndJobsResponse::fromJsonArray($result);
5863
}
5964

65+
/**
66+
* @throws \JsonException
67+
*/
6068
public function loadJobDetail(JobId $jobId): ?Job
6169
{
6270
$resultString = $this->apiCall('GET', 'job/detail?' . http_build_query(['id' => $jobId->getId()]), null)->getBody()->getContents();
63-
$result = json_decode($resultString, true);
71+
try {
72+
$result = json_decode($resultString, true, 512, JSON_THROW_ON_ERROR);
73+
} catch (\JsonException $e) {
74+
throw new \RuntimeException('Could not decode JSON response from prunner API: ' . $e->getMessage(), 1707485815);
75+
}
6476
if (isset($result['error'])) {
6577
return null;
6678
}
@@ -70,22 +82,33 @@ public function loadJobDetail(JobId $jobId): ?Job
7082
public function loadJobLogs(JobId $jobId, string $taskName): JobLogs
7183
{
7284
$resultString = $this->apiCall('GET', 'job/logs?' . http_build_query(['id' => $jobId->getId(), 'task' => $taskName]), null)->getBody()->getContents();
73-
$result = json_decode($resultString, true);
85+
try {
86+
$result = json_decode($resultString, true, 512, JSON_THROW_ON_ERROR);
87+
} catch (\JsonException $e) {
88+
throw new \RuntimeException('Could not decode JSON response from prunner API: ' . $e->getMessage(), 1707485821);
89+
}
7490
return JobLogs::fromJsonArray($result);
7591
}
7692

93+
/**
94+
* @throws \JsonException
95+
*/
7796
public function schedulePipeline(PipelineName $pipeline, array $variables): JobId
7897
{
7998
$response = $this->apiCall('POST', 'pipelines/schedule', json_encode([
8099
'pipeline' => $pipeline->getName(),
81100
'variables' => $variables
82-
], JSON_FORCE_OBJECT));
101+
], JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT));
83102
if ($response->getStatusCode() !== 202) {
84103
throw new \RuntimeException('Scheduling a new pipeline run should have returned status code 202, but got: ' . $response->getStatusCode());
85104
}
86105
$contents = $response->getBody()->getContents();
87-
$tmp = json_decode($contents, true);
88-
return JobId::create($tmp['jobId']);
106+
try {
107+
$result = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);
108+
} catch (\JsonException $e) {
109+
throw new \RuntimeException('Could not decode JSON response from prunner API: ' . $e->getMessage(), 1707485793);
110+
}
111+
return JobId::create($result['jobId']);
89112
}
90113

91114
public function cancelJob(Job $job): void
@@ -99,9 +122,7 @@ public function cancelJob(Job $job): void
99122
/**
100123
* Low-Level method, handling only the authentication.
101124
*
102-
* @param string $method
103-
* @param string $subpath
104-
* @param string|null $body
125+
* @throws GuzzleException
105126
*/
106127
public function apiCall(string $method, string $subpath, ?string $body): ResponseInterface
107128
{
@@ -122,9 +143,6 @@ public function apiCall(string $method, string $subpath, ?string $body): Respons
122143
return $client->request($method, $url, ['headers' => ['Authorization' => 'Bearer ' . $authToken], 'body' => $body, 'http_errors' => false]);
123144
}
124145

125-
/**
126-
* @return string
127-
*/
128146
private function loadJwtSecretFromConfigFile(): string
129147
{
130148
if ($this->configFile && file_exists($this->configFile)) {

0 commit comments

Comments
 (0)