All errors that occur when invoking the AWS SDK methods are indicated by throwing exceptions. You can catch these exceptions if you want to handle errors.
In the simplest case, you can just catch an Exception :
try { $result = $s3->getObject(array( 'Bucket' => 'my bucket', 'Key' => 'path/to/file' )); } catch (Exception $e) { echo 'Oops, something went wrong'; }
If you want to handle certain expected exceptions, though, while letting others bubble up and crash your application, then things get a little more subtle.
First, each of several dozen namespaces in the AWS namespace contains an Exception namespace in which it defines exception classes. One of these classes in each namespace is what Amazon calls the default service exception class for the namespace from which all other exceptions are inherited.
For example, S3 has an Aws\S3\Exception namespace and S3Exception class . EC2 has an Aws\Ec2\Exception namespace and an Ec2Exception class .
Note that catching one of these exceptions instead of the Exception base class immediately stops us, catching certain errors! Service-specific exceptions are thrown as a result of server error responses; Connection failure exceptions are not inherited from them. For example, if you try to run the following code without an internet connection ...
try { $result = $s3->getObject(array( 'Bucket' => 'my bucket', 'Key' => 'path/to/file' )); } catch (S3Exception $e) { echo 'Oops, something went wrong'; }
... then the exception will not be detected (since it will be Guzzle\Http\Exception\CurlException , not the S3Exception), and the program will crash. For this reason, if you take advantage of these exceptions, just to provide users with general error messages, you should probably catch an Exception .
Let's get back to the question of how to handle a specific error. For most namespaces, the answer is that an exception class will be created for this error, and you have to catch it. For example, let's say we use the S3 getObject method getObject and want to do something when the bucket we ask does not exist. In the S3 Exception namespace docs , we see that there is a NoSuchBucketException we can catch:
try { $result = $s3->getObject(array( 'Bucket' => 'my bucket', 'Key' => 'path/to/file' )); } catch (NoSuchBucketException $e) { echo 'There is no such bucket.'; }
(In practice, it may be easier to figure out what exceptions may be caused by what happens through trial and error than by carefully reading the documents.)
Finally, it is worth mentioning the EC2 API. Unlike all other services, the EC2 namespace includes only one exception class, Ec2Exception . If you want to catch and handle a specific error, you need to check the exception object to find out what error you are facing. You can do this by checking the value returned by the getExceptionCode() exception method.
For example, a (modified) fragment from a script I recently wrote that gives the specified IP addresses access to our MySQL server:
try { $result = $ec2->authorizeSecurityGroupIngress([ 'GroupName' => 'mygroup', 'IpProtocol' => 'tcp', 'ToPort' => 3306, 'CidrIp' => $ip . "/32", ]); } catch (Ec2Exception $e) { if ($e->getExceptionCode() == 'InvalidPermission.Duplicate') { echo "IP already has requested permission."; } else {
Please note that possible exception codes - for example, InvalidPermission.Duplicate in this case - are not listed in the AWS PHP SDK documentation, but you can find them by trial and error or from the EC2 API itself , in which each page of the API action contains a section " Errors ", which lists the error codes that it may return.