Skip to content

Commit

Permalink
feat(java sdk): purge ReddioServiceException
Browse files Browse the repository at this point in the history
Signed-off-by: STRRL <im@strrl.dev>
  • Loading branch information
STRRL committed Mar 1, 2023
1 parent 5b931ac commit 1196207
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 101 deletions.
2 changes: 1 addition & 1 deletion reddio-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<revision>0.0.54-SNAPSHOT</revision>
<revision>0.0.54</revision>
<gpg.keyname/>
<gpg.passphrase/>
<gpg.skip>true</gpg.skip>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -371,15 +373,15 @@ public void onResponse(Call call, Response response) {
}
}

public static ReddioErrorCode tryExtractErrorCode(String responseJsonString) {
public static ResponseWrapper<Object> tryExtractParseResponse(String responseJsonString) {
try {
final ResponseWrapper<?> model = objectMapper.readValue(responseJsonString, new TypeReference<ResponseWrapper<?>>() {
return objectMapper.readValue(responseJsonString, new TypeReference<ResponseWrapper<Object>>() {
});
return ReddioErrorCode.fromCode(model.getErrorCode());
} catch (Throwable e) {
// TODO: debug log
return null;
}

}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -149,46 +148,6 @@ public void testEnsureSuccessWithUnrecognizedErrorCode() {
}
}

@Test
public void testToCompletableFutureCallbackOnResponse() {
CompletableFuture<ResponseWrapper<Object>> future = new CompletableFuture<>();
final DefaultReddioRestClient.ToCompletableFutureCallback<?> callback = new DefaultReddioRestClient.ToCompletableFutureCallback(future, new TypeReference<ResponseWrapper<Object>>() {
});
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<ResponseWrapper<Object>> future = new CompletableFuture<>();
final DefaultReddioRestClient.ToCompletableFutureCallback<?> callback = new DefaultReddioRestClient.ToCompletableFutureCallback(future, new TypeReference<ResponseWrapper<Object>>() {
});
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() {
Expand All @@ -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());
}
}
}

0 comments on commit 1196207

Please sign in to comment.