Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApiGatewayManagementApiException does not parse actual AWS error code from response #2285

Closed
3 tasks done
georgeboot opened this issue Aug 6, 2021 · 7 comments
Closed
3 tasks done
Assignees

Comments

@georgeboot
Copy link

georgeboot commented Aug 6, 2021

Describe the bug
When a cal to the ApiGatewayManagementApi results in an error, the corresponding exception is not properly set-up.

Version of AWS SDK for PHP?
3.186.1

Version of PHP (php -v)?
8.0

To Reproduce (observed behavior)

use Aws\ApiGatewayManagementApi\ApiGatewayManagementApiClient;

$client = new ApiGatewayManagementApiClient([/*options*/]);

try {
    $client->postToConnection([
        'ConnectionId' => 'connection-id-that-is-gone',
        'Data' => 'my data',
    ]);
} catch (ApiGatewayManagementApiException $exception) {
    if (($exception->getAwsErrorCode() === 'GoneException') {
        // handle gone
        return;
    }

    throw $exception;
}

Will output the following exception:

Aws\ApiGatewayManagementApi\Exception\ApiGatewayManagementApiException Error executing "PostToConnection" on "https://xxxxx.execute-api.us-east-1.amazonaws.com/yyyyy/@connections/connection-id-that-is-gone"; AWS HTTP error: Client error: `POST https://xxxxx.execute-api.us-east-1.amazonaws.com/yyyyy/@connections/connection-id-that-is-gone` resulted in a `410 Gone` response Unable to parse error information from response - Error parsing JSON: Syntax error
     vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php:195 Aws\WrappedHttpHandler::parseError
     vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php:97 Aws\WrappedHttpHandler::Aws\{closure}

Expected behavior

use Aws\ApiGatewayManagementApi\Exception\ApiGatewayManagementApiException;

assert($exception instanceof ApiGatewayManagementApiException);
assert($exception->getAwsErrorCode() === 'GoneException');
@georgeboot georgeboot added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 6, 2021
@georgeboot georgeboot changed the title ApiGatewayManagementApiException ApiGatewayManagementApiException does not parse actual AWS error code from response Aug 6, 2021
@SamRemis
Copy link
Member

SamRemis commented Aug 6, 2021

@georgeboot Thanks for bringing this to our attention! I was able to reproduce this, and it looks like they don't send back the service doesn't send back a mandatory header which causes our parsing to fail. I will open a ticket with the team and discuss with them to see how fast they can add this header

@SamRemis SamRemis self-assigned this Aug 6, 2021
@SamRemis SamRemis removed bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 6, 2021
@SamRemis
Copy link
Member

@georgeboot Looked into it a bit further, and needed a bit more info: is your API that you're using to test a WebSocket API (as opposed to REST or HTTP)?

@georgeboot
Copy link
Author

Yes indeed, a websocket api.

I'm sending messages to a websocket but need to handle the connection being gone (and the disconnect event not being executed yet).

I am currently unable to catch the gone exception as demonstrated above.

I've replicated the above scenario on a real websocket APi as I was unable to mock the error in the SDK. When appending to a Aws\MockHandler I still have to create the exception by hand, there is no way to get a 'real' AWS error.

@SamRemis
Copy link
Member

@georgeboot Thanks for responding so quickly! Do you mind setting debug => true in your client constructor and showing me the raw response that comes out? It should be somewhere in the middle of the log and look something like this:

["response"]=>
  array(4) {
    ["instance"]=>
    string(32) "000000005d59d45f00000000359e6e7b"
    ["statusCode"]=>
    int(400)
    ["headers"]=>
    array(6) {
      ["X-Amz-Security-Token"]=>
      string(7) "[TOKEN]"
      ["Server"]=>
      array(1) {
        [0]=>
        string(10) "awselb/2.0"
      }
      ["Date"]=>
      array(1) {
        [0]=>
        string(29) "Tue, 24 Aug 2021 18:10:34 GMT"
      }
      ["Content-Type"]=>
      array(1) {
        [0]=>
        string(31) "application/json; charset=utf-8"
      }
      ["Content-Length"]=>
      array(1) {
        [0]=>
        string(2) "11"
      }
      ["Connection"]=>
      array(1) {
        [0]=>
        string(10) "keep-alive"
      }
    }
    ["body"]=>
    string(11) "Bad Request"
  }
}

@stobrien89
Copy link
Member

Hi @georgeboot,

Closing this for staleness, but let us know if you're still having issues!

@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@nixilla
Copy link

nixilla commented Jan 16, 2024

Workaround:

<?php
use Aws\ApiGatewayManagementApi\Exception\ApiGatewayManagementApiException;

assert($exception instanceof ApiGatewayManagementApiException);
assert($exception->getResponse()->getStatusCode() === 410);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants