requestFactory and retrurncode 401 crashes command

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?

(TYPO3 11.5.38)

Hi @piphi!

I guess you’re referring to this section of the example:

        if ($response->getStatusCode() !== 200) {
            throw new \RuntimeException(
                'Returned status code is ' . $response->getStatusCode(),
            );
        }

This will indeed throw an exception. In real-life code you’ll have to either put the call within a try-catch like this:

try {
    $response = $this->requestFactory->request($url, 'POST', $additionalOptions);
} catch (\RuntimeException $e) {
    // Do something else.
}

Or you have to make the request() method return a string with a return statement.

        if ($response->getStatusCode() !== 200) {
            return 'The request did not return a 200 status.';
        }

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;

Do you know what exception is triggering the catch?

GuzzleHttp\Exception\ClientException prototype object
   message => protected "Client error: `GET https://msexchange.*******/EWS/Exchange.asmx/s/GetUserPhoto?email=anyone@example.com&size=HR648X648` resulted in a `401
       Unauthorized` response" (175 chars)
   code => protected 401 (integer)
   file => protected "/srv/******/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php" (105 chars)
   line => protected 111 (integer)

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:

As mentioned: I found my solution.

I edited the manual and made a pull request.

1 Like

Thank you for contributing your fix! Please feel free to link to the documentation PR here.

which gets forwarded to Update MeowInformationRequester.rst.txt by pi-phi · Pull Request #4675 · TYPO3-Documentation/TYPO3CMS-Reference-CoreApi · GitHub

1 Like