I want to make a request to retrieve user info from OAUTH server with Symfony HttpClient but I can't fetch fetch the response directly when encountering an error response, because the client throws an exception.
My UserProvider:
public function loadUserByUsername($username)
{
try {
$response = $this->httpClient->request(
'GET',
$this->baseUrl . '/userinfo',
[
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'auth_bearer' => $username
]
);
var_dump($response->toArray());die;
} catch (\Exception $e) {
var_dump($e->getMessage());die;
throw new UsernameNotFoundException($e->getMessage());
}
}
When I call $response->toArray()
or $response->getContent()
, an exception is thrown and I get the following error message on Postman
<pre class='xdebug-var-dump' dir='ltr'>
<small>/var/www/html/backend/src/Security/Provider/KeycloakUserProvider.php:50:</small><small>string</small> <font color='#cc0000'>'HTTP/2 401 returned for "https://oauth_server/userinfo".'</font> <i>(length=127)</i>
</pre>
An example of the response received on the browser:
{"error":"invalid_request","error_description":"Token not provided"}
Why can't I access the response directly by calling $response->toArray()
?
Whenever the status code of the response is not "good" (e.g. in the 300-599 range), the response is considered exceptional, and you are supposed to handle it.
This is clearly documented here:
If you do not want the client to throw an exception when encountering a non-OK response, you need to pass
false
togetContent()
ortoArray()
.E.g.
There is nothing wrong in handling the exception explicitly in your code, and will make your application better express the conditions it encounters. A "non-good" should be treated as anomalous, and handled accordingly.