Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling errors in AWS PHP SDK 2

If I try to get an object from my S3 bucket that doesn't exist, the Amazon PHP SDK 2 gives me a pretty ugly error. Handy for me but means nothing to the end user...

E.g:

$s3 = $aws->get('s3');

$result = $s3->getObject(array(
    'Bucket' => 'my bucket',
    'Key'    => 'path/to/file'
));

The error:

Fatal error: Uncaught Aws\S3\Exception\NoSuchKeyException: AWS Error Code: NoSuchKey, Status Code: 404, AWS Request ID: xxxxxxxxxxxxx, AWS Error Type: client, AWS Error Message: The specified key does not exist. thrown in AWS/vendor/aws/aws-sdk-php/src/Aws/Common/Exception/NamespaceExceptionFactory.php on line 89

Is there a way that I can determine if there is an error and print a message that makes sense rather than the above?

like image 612
Ben Sinclair Avatar asked Apr 10 '13 06:04

Ben Sinclair


1 Answers

It suddenly occurred to me to try this:

try {
    $result = $s3->getObject(array(
        'Bucket' => 'my bucket',
        'Key'    => 'path/to/file'
    ));
} catch (Exception $e) {
   // I can put a nicer error message here  
}
like image 155
Ben Sinclair Avatar answered Oct 16 '22 09:10

Ben Sinclair