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

fix(exception): better handle non-JSON response bodies #1404

Merged
merged 4 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/java/com/adyen/service/resource/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.adyen.model.ApiError;
import com.adyen.model.RequestOptions;
import com.adyen.service.exception.ApiException;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.google.gson.Gson;

import java.io.IOException;
Expand Down Expand Up @@ -113,7 +115,11 @@ public String request(String json, RequestOptions requestOptions, ApiConstants.H
} catch (HTTPClientException e) {
apiException = new ApiException(e.getMessage(), e.getCode(), e.getResponseHeaders());
apiException.setResponseBody(e.getResponseBody());
apiException.setError(ApiError.fromJson(e.getResponseBody()));
try {
apiException.setError(ApiError.fromJson(e.getResponseBody()));
} catch (Exception ignore) {
// Response body could not be parsed (e.g. not JSON), raw response body available in ApiException#responseBody
}
}
throw apiException;
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/adyen/service/ResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import java.util.Map;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -82,6 +84,20 @@ public void testRequestQueryString() throws Exception {
verify(clientInterfaceMock).request("/companies/adyen/merchants", null, null, false, null, ApiConstants.HttpMethod.GET, queryString);
}

@Test
public void testNonJsonError() throws Exception {
Map<String, String> pathParams = Collections.singletonMap("companyId", "adyen");
Map<String, String> queryString = Collections.singletonMap("pageSize", "10");
Resource resource = new Resource(serviceMock, "/companies/{companyId}/merchants", null);

HTTPClientException error = new HTTPClientException(500, "error", Collections.emptyMap(), "not JSON");
when(clientInterfaceMock.request(any(), any(), any(), anyBoolean(), any(), any(), any())).thenThrow(error);

ApiException thrown = assertThrows(ApiException.class, () -> resource.request(null, null, ApiConstants.HttpMethod.GET, pathParams, queryString));
assertEquals("not JSON", thrown.getResponseBody());
assertNull(thrown.getError());
}

@Test
public void testRequestExceptionEmpty() throws IOException, HTTPClientException {
try {
Expand Down