Aws-sdk-php: Exception Handling

Created on 4 Aug 2016  路  8Comments  路  Source: aws/aws-sdk-php

@trevorrowe @claylo @skyzyx @danielcosta @rlerdorf I am creating a service using Amazon SNS and I am stuck at exception handling with the latest SDK. Basically I am not sure how to know the specific details of an exception thrown: For example, the documentation says that when a notification is published (eg. $snsClient->publish(...);), it throws errors like:

  • InvalidParameterException
  • InvalidParameterValueException
  • InternalErrorException
  • NotFoundException
  • EndpointDisabledException
  • PlatformApplicationDisabledException
  • AuthorizationErrorException

However there are no real classes like these in the SDK. I suppose the publish() method would throw an Aws\Sns\Exception\SnsException. I'd like specifically know how to grab EndpointDisabledException and NotFoundException. Am I missing anything in the documentation?

documentation pr-under-review

Most helpful comment

Correct.

I think the docs are a bit confusing right now as-is. They should really make it more clear that those errors listed are just values that are returned from getAwsErrorCode() and not concrete classes. We will take an action item to make this more clear.

All 8 comments

@schristiaans , you could try catch SnsException by Aws\Sns\Exception\SnsException, then call getAwsErrorCode() to get the error information.

@cjyclaire Thanks for your reply. So the getAwsErrorCode() will give me one of the exceptions listed above depending on error?

Correct.

I think the docs are a bit confusing right now as-is. They should really make it more clear that those errors listed are just values that are returned from getAwsErrorCode() and not concrete classes. We will take an action item to make this more clear.

@mtdowling @cjyclaire @claylo @trevorrowe @skyzyx Thank you so much for your help. Special thanks to @mtdowling and @cjyclaire. I would suggest you guys make the documentation clearer on exception handling. I am assuming it's basically because you use "unchecked exceptions"

@schristiaans No problem at all :) , I agree that our documentation should make it clear on exception handling. And I'll open a PR to address that in our SDK guide shortly

@cjyclaire So am i doing right? Just wanted to double check:

try {
    $this->snsClient->publish(...);
} catch (SnsException $e) {
    switch ($e->getAwsErrorCode()) {
        case 'EndpointDisabledException':
        case 'NotFoundException':
            /// do something
            break;
    }
}

@schristiaans apology for not pointing this out at first place, but I think you need to remove the "Exception" in case condition, something like:

try {
    $this->snsClient->publish(...);
} catch (SnsException $e) {
    switch ($e->getAwsErrorCode()) {
        case 'EndpointDisabled':
        case 'NotFound':
            /// do something
            break;
    }
}

Closing, tracking it with the opening PR, yet feel free to reopen with further questions :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

liquorvicar picture liquorvicar  路  5Comments

chiukit picture chiukit  路  4Comments

GeekLad picture GeekLad  路  5Comments

muratsplat picture muratsplat  路  4Comments

sm2017 picture sm2017  路  4Comments