There are two important debugging strategies to use when encountering problems with HTTP transactions.
If the API returns a 4xx or 5xx status code, it indicates that there was an
error with the sent request, meaning that the transaction cannot be adequately
completed.
The Guzzle HTTP component, which forms the basis of our SDK's transport layer, utilizes numerous exception classes to handle this error logic.
The two most common exception classes are:
-
Guzzle\Http\Exception\ClientErrorResponseException, which is thrown when a4xxresponse occurs -
Guzzle\Http\Exception\ServerErrorResponseException, which is thrown when a5xxresponse occurs
Both of these classes extend the base BadResponseException class.
This provides you with the granularity you need to debug and handle exceptions.
If you're trying to retrieve a Swift resource, such as a Data Object, and you're not completely certain that it exists, it makes sense to wrap your call in a try/catch block:
use Guzzle\Http\Exception\ClientErrorResponseException;
try {
return $service->getObject('foo.jpg');
} catch (ClientErrorResponseException $e) {
// Okay, the resource probably does not exist
return false;
} catch (\Exception $e) {
// Some other exception was thrown, probably critical
$this->logException($e);
$this->alertDevs();
}Both ClientErrorResponseException and ServerErrorResponseException have
two methods that allow you to access the HTTP transaction:
// Find out the faulty request
$request = $e->getRequest();
// Display everything by casting as string
echo (string) $request;
// Find out the HTTP response
$response = $e->getResponse();
// Output that too
echo (string) $response;Guzzle provides a Log plugin that allows you to log everything over the wire, which is useful if you don't know what's going on.
Here's how you enable it:
php composer.phar install guzzle/plugin-log:~3.8use Guzzle\Plugin\Log\LogPlugin;
$client->addSubscriber(LogPlugin::getDebugPlugin());The above will add a generic logging subscriber to your client, which will be notified every time a relevant HTTP event is fired off.