Skip to content

Commit

Permalink
text handling of error response from providers (AthenZ#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
havetisyan authored Jul 16, 2018
1 parent ab0637d commit 7e47217
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ void setBase(WebTarget base) {
this.base = base;
}

/**
* If we're given any response in our rejected provider
* confirmation or refresh request, we're going to include
* that as part of the resource exception text so it can
* be logged. If there is no response or any exception
* while trying to read the response, we'll just return
* N/A as the response text.
* @param response client response object
* @return response text normalized.
*/
private String responseText(final Response response) {
String data = null;
try {
data = response.readEntity(String.class);
} catch (Exception ignored) {
}
if (data == null) {
return "N/A";
}
return data.replace('\n', ' ');
}

public InstanceConfirmation postInstanceConfirmation(InstanceConfirmation confirmation) {
WebTarget target = base.path("/instance");
Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON);
Expand All @@ -69,7 +91,7 @@ public InstanceConfirmation postInstanceConfirmation(InstanceConfirmation confir
case 200:
return response.readEntity(InstanceConfirmation.class);
default:
throw new ResourceException(code, response.readEntity(ResourceError.class));
throw new ResourceException(code, responseText(response));
}
}

Expand All @@ -83,7 +105,7 @@ public InstanceConfirmation postRefreshConfirmation(InstanceConfirmation confirm
case 200:
return response.readEntity(InstanceConfirmation.class);
default:
throw new ResourceException(code, response.readEntity(ResourceError.class));
throw new ResourceException(code, responseText(response));
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,13 @@ public class ResourceException extends RuntimeException {
public final static int NOT_FOUND = 404;

final private int code;
final private Object data;

public ResourceException(int code, Object data) {
super("ResourceException (" + code + "): " + data);
public ResourceException(int code, String message) {
super("ResourceException (" + code + "): " + message);
this.code = code;
this.data = data;
}

public int getCode() {
return code;
}

public Object getData() {
return data;
}

public <T> T getData(Class<T> cl) {
return cl.cast(data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,35 @@ public void testInstanceProviderClientFailure() {
Response response = Mockito.mock(Response.class);
Mockito.when(builder.post(entity)).thenReturn(response);
Mockito.when(response.getStatus()).thenReturn(401);
Mockito.when(response.readEntity(String.class))
.thenReturn(null)
.thenReturn("Bad request" + '\n' + "Bad data")
.thenThrow(new RuntimeException("Bad request"));

try {
provClient.postInstanceConfirmation(confirmation);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 401);
assertEquals(ex.getMessage(), "ResourceException (401): N/A");
}

try {
provClient.postRefreshConfirmation(confirmation);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 401);
assertEquals(ex.getMessage(), "ResourceException (401): Bad request Bad data");
}


try {
provClient.postInstanceConfirmation(confirmation);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 401);
assertEquals(ex.getMessage(), "ResourceException (401): N/A");
}

provClient.close();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,10 @@
public class ResourceExceptionTest {

@Test
public void testCodeOnly() {
public void testResourceException() {

ResourceError resError = new ResourceError();
resError.setCode(400);
resError.setMessage("Bad Request");
ResourceException exc = new ResourceException(400, resError);
assertEquals(exc.getData().toString(), "{code: 400, message: \"Bad Request\"}");
}

@Test
public void testGetData() {

ResourceException exc = new ResourceException(400, "Invalid domain name");
assertEquals(exc.getData(), "Invalid domain name");
}

@Test
public void testGetDataCast() {

ResourceException exc = new ResourceException(400, 5000);
assertEquals(exc.getData(Integer.class), new Integer(5000));
ResourceException exc = new ResourceException(400, "Bad Request");
assertEquals(exc.getMessage(), "ResourceException (400): Bad Request");
assertEquals(exc.getCode(), 400);
}
}

0 comments on commit 7e47217

Please sign in to comment.