Skip to content

Instantly share code, notes, and snippets.

@doapp-ryanp
Created May 22, 2012 13:27
Show Gist options
  • Save doapp-ryanp/2769047 to your computer and use it in GitHub Desktop.
Save doapp-ryanp/2769047 to your computer and use it in GitHub Desktop.
dynamo exception class
class DynamoDbException extends Exception
{
//DyanmoDB exceptions. @see http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/ErrorHandling.html
const AccessDeniedException = 1;
const ConditionalCheckFailedException = 2;
const IncompleteSignatureException = 3;
const LimitExceededException = 4;
const MissingAuthenticationTokenException = 5;
const ProvisionedThroughputExceededException = 6;
const ResourceInUseException = 7;
const ResourceNotFoundException = 8;
const ThrottlingException = 9;
const ValidationException = 10;
const InternalFailure = 11;
const InternalServerError = 12;
const ServiceUnavailableException = 13;
//Custom types
const TimeOutExecption = 14;
const Unknown = 15;
// Redefine the exception so message isn't optional
public function __construct($message, $code = self::Unknown, Exception $previous = null) {
// make sure everything is assigned properly
parent::__construct($message, $code, $previous);
}
// custom string representation of object
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
/**
* Get the error code from the response.
* @see http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/ErrorHandling.html
*
* @param CFResponse $responseObject
* @return mixed false if no error else String of error code
*/
static function getErrorCode($responseObject)
{
if (!empty($responseObject->body->__type))
{
$code = explode('#',$responseObject->body->__type,2);
return (isset($code[1]))?$code[1]:false;
}
else return false;
}
static function mapCode($codeString) {
switch ($codeString) {
case 'AccessDeniedException':
return self::AccessDeniedException;
break;
case 'ConditionalCheckFailedException':
return self::ConditionalCheckFailedException;
break;
case 'IncompleteSignatureException':
return self::IncompleteSignatureException;
break;
case 'LimitExceededException':
return self::LimitExceededException;
break;
case 'MissingAuthenticationTokenException':
return self::MissingAuthenticationTokenException;
break;
case 'ProvisionedThroughputExceededException':
return self::ProvisionedThroughputExceededException;
break;
case 'ResourceInUseException':
return self::ResourceInUseException;
break;
case 'ResourceNotFoundException':
return self::ResourceNotFoundException;
break;
case 'ThrottlingException':
return self::ThrottlingException;
break;
case 'ValidationException':
return self::ValidationException;
break;
case 'InternalFailure':
return self::InternalFailure;
break;
case 'InternalServerError':
return self::InternalServerError;
break;
case 'ServiceUnavailableException':
return self::ServiceUnavailableException;
break;
case 'TimeOutExecption':
return self::TimeOutExecption;
break;
default:
return self::Unknown;
break;
}
}
}
//Usage:
if(!$response->isOK()) {
throw New DynamoDbException("Error creating capacity alarm for table: $tableName", DynamoDbException::mapCode(DynamoDbException::getErrorCode($response)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment