Skip to content

Commit fe8c4a9

Browse files
Merge pull request #19 from cleverage/17
#17 Improve errors handling using valid_response_code. Allow ClientEx…
2 parents af9de8d + 1755a1f commit fe8c4a9

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/Task/RequestTask.php

+26-12
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function execute(ProcessState $state): void
8989
// Handle empty results
9090
try {
9191
if (!\in_array($response->getStatusCode(), $options['valid_response_code'], false)) {
92-
$state->setErrorOutput($response->getContent());
92+
$state->setErrorOutput($response->getContent(false));
9393

9494
if (TaskConfiguration::STRATEGY_SKIP === $state->getTaskConfiguration()->getErrorStrategy()) {
9595
$state->setSkipped(true);
@@ -102,17 +102,31 @@ public function execute(ProcessState $state): void
102102

103103
$state->setOutput($response->getContent());
104104
} catch (\Throwable $e) {
105-
$this->logger->error(
106-
'REST request failed',
107-
[
108-
'client' => $options['client'],
109-
'options' => $options,
110-
'request_options' => $requestOptions,
111-
'message' => $e->getMessage(),
112-
'raw_headers' => $response->getHeaders(false),
113-
'raw_body' => $response->getContent(false),
114-
]
115-
);
105+
$allowRedirectionException = false;
106+
$allowClientException = false;
107+
foreach ($options['valid_response_code'] as $code) {
108+
if ($code >= 300 && $code < 400) {
109+
$allowRedirectionException = true;
110+
}
111+
if ($code >= 400 && $code < 500) {
112+
$allowClientException = true;
113+
}
114+
}
115+
if (!($allowRedirectionException && $e instanceof RedirectionExceptionInterface)
116+
&& !($allowClientException && $e instanceof ClientExceptionInterface)
117+
) {
118+
$this->logger->error(
119+
'REST request failed',
120+
[
121+
'client' => $options['client'],
122+
'options' => $options,
123+
'request_options' => $requestOptions,
124+
'message' => $e->getMessage(),
125+
'raw_headers' => $response->getHeaders(false),
126+
'raw_body' => $response->getContent(false),
127+
]
128+
);
129+
}
116130

117131
throw $e;
118132
}

0 commit comments

Comments
 (0)