I implemented a command and use the requestFactory like given in the example HTTP requests to external sources — TYPO3 Explained main documentation
in the example code there is a handling regarding return codes other than 200 and I thought there I could handle every responses. But my call returned with a 401 and the command crashed immediately so I need to add an additional try {} catch {}
Have I missed some configuration?
or should the example be enhanced for such cases?
I mean these lines, but my code does not reach it, as the execution is terminated before the condition $response->getStatusCode() !== 200 can be evaluated. So I can’t return a string (or similar)
my solution so far:
add a try{} as the following lines would not be reached.
try {
$response = @$this->requestFactory->request($url, 'GET', $additionalOptions);
} catch (\Exception $exception) {
// continue in case of a 401
return '';
}
// in case of an 401 error this is not reached without try{}.
if ($response->getStatusCode() !== 200) {
$result = '';
} else {
if ($response->getHeaderLine('Content-Length') == '0') {
$result = '';
} else {
$result = $response->getBody()->getContents();
if (substr($result,0,15) == '<!DOCTYPE html>') {
// some HTML instead of jpeg
$result = '';
}
}
}
return $result;
Since Guzzle is throwing an exception instead of returning a response object, you’ll need to handle the response code within the catch. Something like this: