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

AWS client does not retrieve error reasons #56

Closed
vertex-github opened this issue Feb 27, 2018 · 3 comments
Closed

AWS client does not retrieve error reasons #56

vertex-github opened this issue Feb 27, 2018 · 3 comments
Assignees
Milestone

Comments

@vertex-github
Copy link

When AWS issues an error they do so under the 503 error code. The current AWS client does not retrieve the text content of error messages as an Exception is thrown when the response code is not 2xx or 3xx. The following patch attempts to retrieve and log the error reason:

Index: src/main/java/com/hazelcast/aws/impl/DescribeInstances.java

IDEA additional info:

Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP

<+>UTF-8

===================================================================

--- src/main/java/com/hazelcast/aws/impl/DescribeInstances.java            (revision 2b2fa219a4cf12892a1b914bef98f2caca10d24a)

+++ src/main/java/com/hazelcast/aws/impl/DescribeInstances.java         (date 1519741021000)

@@ -23,10 +23,14 @@

import com.hazelcast.com.eclipsesource.json.JsonObject;

import com.hazelcast.config.AwsConfig;

import com.hazelcast.config.InvalidConfigurationException;

+import com.hazelcast.logging.ILogger;

+import com.hazelcast.logging.Logger;

+import com.hazelcast.nio.IOUtil;


 import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

+import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.text.SimpleDateFormat;

@@ -52,7 +56,9 @@

  */

public class DescribeInstances {


-    /**

+             private static final ILogger LOGGER = Logger.getLogger( DescribeInstances.class );

+

+             /**

      * URI to fetch container credentials (when IAM role is enabled)

      *

      * see http://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html

@@ -241,7 +247,50 @@

         httpConnection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(awsConfig.getConnectionTimeoutSeconds()));

         httpConnection.setDoOutput(false);

         httpConnection.connect();

-        return httpConnection.getInputStream();

+

+                 InputStream is = null;

+                 try

+                 {

+                                 is = httpConnection.getInputStream();

+                 }

+                 catch( IOException e )

+                 {

+                                 LOGGER.warning( "Unable to retrieve info from URL: " + httpConnection.getResponseCode() );

+                                 InputStream errorStream = httpConnection.getErrorStream();

+                                 String errorMsg = inputStreamToString( errorStream );

+                                             LOGGER.warning( "Error Message: " + errorMsg );

+

+                                             throw e;

+                 }

+

+                 return is;

+    }

+

+    private String inputStreamToString( InputStream is )

+    {

+                 StringBuilder buf = new StringBuilder();

+

+                 BufferedReader br = null;

+                 try

+                 {

+                                 br = new BufferedReader( new InputStreamReader( is, "UTF-8" ) );

+

+                                 String line;

+                                 while( (line = br.readLine()) != null )

+                                 {

+                                                 buf.append( line );

+                                 }

+                 }

+                 catch( IOException e )

+                 {

+                                 LOGGER.warning( "Unable to retrieve error stream response", e );

+                 }

+                 finally

+                 {

+                                 IOUtil.closeResource( br );

+                 }

+

+                 return buf.toString();

     }


     public EC2RequestSigner getRequestSigner() {
@vertex-github
Copy link
Author

Server side error codes and message format detailed here: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/errors-overview.html#api-error-codes-table-server

@leszko
Copy link

leszko commented Mar 20, 2018

Added the feature in #60

Closing the issue.

1 similar comment
@leszko
Copy link

leszko commented Mar 20, 2018

Added the feature in #60

Closing the issue.

@leszko leszko closed this as completed Mar 20, 2018
@mesutcelik mesutcelik added this to the 2.2 milestone Apr 5, 2018
@degerhz degerhz modified the milestones: 2.2, 2.1.1 Apr 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants