How to get JSON data from exception in httpClient() public function myLogin() {
    try {
      $client = \Drupal::httpClient();
      $request = $client-&gt;post('https://targetApi.com/auth/login?fx=1', [
             'json' =&gt; [
                 'login' =&gt; 'demo',
                 'password' =&gt; 'demo1234567',
                 'locale' =&gt; 'en'
              ]
      ]);
      $response = json_decode($request-&gt;getBody());
      return $respone;
    } catch (\Exception $e) {
      \Drupal::logger('autoapp')-&gt;error($e-&gt;getMessage());
    }
  }


When I set wrong password I can catch this in $e->getMessage(). The message is:

Client error: `POST https://targetApi.com/auth/login?fx=1` resulted in a `404 Not Found` response:
{"error":"auth.user.notfound"}


So I can catch error, this is fine. But I'd like to get JSON data from It. And if in the try part I have it in $request-&gt;getBody(), how can I get it from $e ?
This should work 

if ($e-&gt;hasResponse()) {
    $response = $e-&gt;getResponse()-&gt;getBody()-&gt;getContents();
}