Skip to content

Commit

Permalink
fix: Add logging to DynamicEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jan 9, 2025
1 parent 42b9afb commit 82b66e2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
22 changes: 19 additions & 3 deletions src/main/java/com/vonage/client/DynamicEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Enables convenient declaration of endpoints without directly implementing {@link AbstractMethod}.
Expand All @@ -42,6 +44,8 @@
*/
@SuppressWarnings("unchecked")
public class DynamicEndpoint<T, R> extends AbstractMethod<T, R> {
protected final Logger logger = Logger.getLogger(getClass().getName());

protected Set<Class<? extends AuthMethod>> authMethods;
protected String contentType, accept;
protected HttpMethod requestMethod;
Expand Down Expand Up @@ -242,6 +246,7 @@ else if (requestBody instanceof byte[]) {
@Override
protected final R parseResponse(HttpResponse response) throws IOException {
int statusCode = response.getStatusLine().getStatusCode();
logger.fine(() -> "Response status: " + statusCode);
try {
if (statusCode >= 200 && statusCode < 300) {
return parseResponseSuccess(response);
Expand All @@ -255,6 +260,7 @@ else if (statusCode >= 300 && statusCode < 400) {
}
catch (InvocationTargetException ex) {
Throwable wrapped = ex.getTargetException();
logger.log(Level.SEVERE, "Internal SDK error", ex);
if (wrapped instanceof RuntimeException) {
throw (RuntimeException) wrapped;
}
Expand All @@ -263,6 +269,7 @@ else if (statusCode >= 300 && statusCode < 400) {
}
}
catch (ReflectiveOperationException ex) {
logger.log(Level.SEVERE, "Internal SDK error", ex);
throw new VonageUnexpectedException(ex);
}
finally {
Expand All @@ -276,6 +283,7 @@ protected R parseResponseFromString(String response) {

private R parseResponseRedirect(HttpResponse response) throws ReflectiveOperationException, IOException {
final String location = response.getFirstHeader("Location").getValue();
logger.fine(() -> "Redirect: " + location);

if (java.net.URI.class.equals(responseType)) {
return (R) URI.create(location);
Expand All @@ -290,13 +298,17 @@ else if (String.class.equals(responseType)) {

private R parseResponseSuccess(HttpResponse response) throws IOException, ReflectiveOperationException {
if (Void.class.equals(responseType)) {
logger.fine(() -> "No response body.");
return null;
}
else if (byte[].class.equals(responseType)) {
return (R) EntityUtils.toByteArray(response.getEntity());
byte[] result = EntityUtils.toByteArray(response.getEntity());
logger.fine(() -> "Binary response body of length " + result.length);
return (R) result;
}
else {
String deser = EntityUtils.toString(response.getEntity());
logger.fine(() -> deser);

if (responseType.equals(String.class)) {
return (R) deser;
Expand All @@ -316,7 +328,9 @@ else if (Collection.class.isAssignableFrom(responseType) || isJsonableArrayRespo
else {
R customParsedResponse = parseResponseFromString(deser);
if (customParsedResponse == null) {
throw new IllegalStateException("Unhandled return type: " + responseType);
String errorMsg = "Unhandled return type: " + responseType;
logger.warning(errorMsg);
throw new IllegalStateException(errorMsg);
}
else {
return customParsedResponse;
Expand Down Expand Up @@ -345,7 +359,9 @@ private R parseResponseFailure(HttpResponse response) throws IOException, Reflec
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
throw (RuntimeException) constructor.newInstance(exMessage);
RuntimeException ex = (RuntimeException) constructor.newInstance(exMessage);
logger.log(Level.SEVERE, "Internal SDK error", ex);
throw ex;
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/com/vonage/client/AbstractMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,8 @@
import java.util.logging.LogManager;
import java.util.stream.Collectors;

@Execution(ExecutionMode.SAME_THREAD)
public class AbstractMethodTest {

static {
LogManager.getLogManager().getLogger(AbstractMethod.class.getName())
.setLevel(java.util.logging.Level.FINE);
}

private static class ConcreteMethod extends AbstractMethod<String, String> {
public ConcreteMethod(HttpWrapper httpWrapper) {
super(httpWrapper);
Expand Down Expand Up @@ -116,6 +110,12 @@ public void close() {
private AuthMethod mockAuthMethod;
private final CloseableHttpResponse basicResponse = new CloseableBasicHttpResponse();

@BeforeAll
public static void setUpBeforeClass() {
LogManager.getLogManager().getLogger(AbstractMethod.class.getName())
.setLevel(java.util.logging.Level.FINE);
}

@BeforeEach
public void setUp() throws Exception {
mockWrapper = mock(HttpWrapper.class);
Expand Down

0 comments on commit 82b66e2

Please sign in to comment.