Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Solving: Guzzle errors truncated

David Carr

Development

I'm a big fan of Guzzle it's great for making API calls. 

My typical code when making calls looks like this:

use GuzzleHttp\Client;
use Exception;


try {
    $client = new Client;
    $response = $client->post($url, ['form_params' => $params]);
    return json_decode($response->getBody()->getContents(), true);
} catch (Exception $e) {
    return $e->getMessage();
}

That all works but when there is an error returned from the API the errors are truncated. Calling the usually $e->getMessage is not the correct way with Guzzle instead throw an exception with the same response call used for returned data.

throw new Exception($e->getResponse()->getBody()->getContents());

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.