diff --git a/reddio-java/pom.xml b/reddio-java/pom.xml index 8b78e78..de503a2 100644 --- a/reddio-java/pom.xml +++ b/reddio-java/pom.xml @@ -56,7 +56,7 @@ UTF-8 1.8 1.8 - 0.0.54-SNAPSHOT + 0.0.54 true diff --git a/reddio-java/reddio-api/src/main/java/com/reddio/api/v1/rest/DefaultReddioRestClient.java b/reddio-java/reddio-api/src/main/java/com/reddio/api/v1/rest/DefaultReddioRestClient.java index 0bc7e74..f736416 100644 --- a/reddio-java/reddio-api/src/main/java/com/reddio/api/v1/rest/DefaultReddioRestClient.java +++ b/reddio-java/reddio-api/src/main/java/com/reddio/api/v1/rest/DefaultReddioRestClient.java @@ -7,9 +7,7 @@ import com.reddio.exception.ReddioBusinessException; import com.reddio.exception.ReddioErrorCode; import com.reddio.exception.ReddioException; -import com.reddio.exception.ReddioServiceException; import okhttp3.*; -import okhttp3.internal.http2.ErrorCode; import org.jetbrains.annotations.NotNull; import java.io.IOException; @@ -360,9 +358,13 @@ public void onFailure(Call call, IOException e) { public void onResponse(Call call, Response response) { try { String responseBodyAsString = response.body() != null ? response.body().string() : ""; - ReddioErrorCode errCode = tryExtractErrorCode(responseBodyAsString); if (!response.isSuccessful()) { - this.future.completeExceptionally(new ReddioServiceException("reddio service not respond as sucessful", response.code(), errCode, responseBodyAsString)); + final ResponseWrapper model = tryExtractParseResponse(responseBodyAsString); + if (model != null) { + this.future.completeExceptionally(new ReddioBusinessException(model.getStatus(), model.getError(), ReddioErrorCode.fromCode(model.getErrorCode()), model)); + } else { + this.future.completeExceptionally(new ReddioException(String.format("reddio service responded with status code %d, response body: %s", response.code(), responseBodyAsString))); + } return; } this.future.complete(objectMapper.readValue(responseBodyAsString, typeReference)); @@ -371,15 +373,15 @@ public void onResponse(Call call, Response response) { } } - public static ReddioErrorCode tryExtractErrorCode(String responseJsonString) { + public static ResponseWrapper tryExtractParseResponse(String responseJsonString) { try { - final ResponseWrapper model = objectMapper.readValue(responseJsonString, new TypeReference>() { + return objectMapper.readValue(responseJsonString, new TypeReference>() { }); - return ReddioErrorCode.fromCode(model.getErrorCode()); } catch (Throwable e) { // TODO: debug log return null; } + } } diff --git a/reddio-java/reddio-api/src/main/java/com/reddio/exception/ReddioServiceException.java b/reddio-java/reddio-api/src/main/java/com/reddio/exception/ReddioServiceException.java deleted file mode 100644 index aa3ed59..0000000 --- a/reddio-java/reddio-api/src/main/java/com/reddio/exception/ReddioServiceException.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.reddio.exception; - -import lombok.Getter; - -/** - * ReddioServiceException represents the exception when the reddio service not respond successfully. - * - * @author strrl - */ -public class ReddioServiceException extends ReddioException { - public ReddioServiceException(String description, int httpStatusCode, ReddioErrorCode errorCode, String responseBody) { - this.description = description; - this.httpStatusCode = httpStatusCode; - this.errorCode = errorCode; - this.responseBody = responseBody; - } - - private static final long serialVersionUID = 1205765559865993637L; - - /** - * Description of the exception. - */ - @Getter - private final String description; - - /** - * Http status code. - */ - @Getter - private final int httpStatusCode; - - /** - * Error code as enum, might be null if the error code is not recognized. - */ - @Getter - private final ReddioErrorCode errorCode; - - /** - * The raw response body as string. - */ - @Getter - private final String responseBody; - - - @Override - public String getMessage() { - return String.format("reddio service not respond service, description: %s, http status code: %d, error code: %s, response body: %s", description, httpStatusCode, errorCode, responseBody); - } -} diff --git a/reddio-java/reddio-api/src/test/java/com/reddio/api/v1/rest/DefaultReddioRestClientTest.java b/reddio-java/reddio-api/src/test/java/com/reddio/api/v1/rest/DefaultReddioRestClientTest.java index 76fd360..9da7160 100644 --- a/reddio-java/reddio-api/src/test/java/com/reddio/api/v1/rest/DefaultReddioRestClientTest.java +++ b/reddio-java/reddio-api/src/test/java/com/reddio/api/v1/rest/DefaultReddioRestClientTest.java @@ -5,7 +5,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.reddio.exception.ReddioBusinessException; import com.reddio.exception.ReddioErrorCode; -import com.reddio.exception.ReddioServiceException; import okhttp3.Protocol; import okhttp3.Request; import okhttp3.Response; @@ -149,46 +148,6 @@ public void testEnsureSuccessWithUnrecognizedErrorCode() { } } - @Test - public void testToCompletableFutureCallbackOnResponse() { - CompletableFuture> future = new CompletableFuture<>(); - final DefaultReddioRestClient.ToCompletableFutureCallback callback = new DefaultReddioRestClient.ToCompletableFutureCallback(future, new TypeReference>() { - }); - final Request stubRequest = new Request.Builder().url("http://fake-url").build(); - final Response r = new Response.Builder().code(400).request(stubRequest).protocol(Protocol.HTTP_1_1).message("").build(); - callback.onResponse(null, r); - try { - future.join(); - Assert.fail(); - } catch (CompletionException e) { - final Throwable cause = e.getCause(); - Assert.assertTrue(cause instanceof ReddioServiceException); - Assert.assertEquals(400, ((ReddioServiceException) cause).getHttpStatusCode()); - } catch (Throwable t) { - Assert.fail(); - } - } - - @Test - public void testToCompletableFutureCallbackOnResponseUnrecognizedCode() { - CompletableFuture> future = new CompletableFuture<>(); - final DefaultReddioRestClient.ToCompletableFutureCallback callback = new DefaultReddioRestClient.ToCompletableFutureCallback(future, new TypeReference>() { - }); - final Request stubRequest = new Request.Builder().url("http://fake-url").build(); - final Response r = new Response.Builder().code(503).request(stubRequest).protocol(Protocol.HTTP_1_1).message("").build(); - callback.onResponse(null, r); - try { - future.join(); - Assert.fail(); - } catch (CompletionException e) { - final Throwable cause = e.getCause(); - Assert.assertTrue(cause instanceof ReddioServiceException); - Assert.assertEquals(503, ((ReddioServiceException) cause).getHttpStatusCode()); - } catch (Throwable t) { - Assert.fail(); - } - } - @Test @Ignore("Waiting backend update") public void testMintWithInvalidApiKey() { @@ -201,11 +160,11 @@ public void testMintWithInvalidApiKey() { "1" ) ).join(); + Assert.fail(); } catch (CompletionException e) { final Throwable cause = e.getCause(); - Assert.assertTrue(cause instanceof ReddioServiceException); - Assert.assertEquals(403, ((ReddioServiceException) cause).getHttpStatusCode()); - Assert.assertEquals(ReddioErrorCode.InvalidAPIKey, ((ReddioServiceException) cause).getErrorCode()); + Assert.assertTrue(cause instanceof ReddioBusinessException); + Assert.assertEquals(ReddioErrorCode.InvalidAPIKey, ((ReddioBusinessException) cause).getErrorCode()); } } }