Skip to content

Commit

Permalink
Show actual error response from UID2 operator instead of generic erro…
Browse files Browse the repository at this point in the history
…r response from Java network library on SDK side (#48)

* Show actual error response from UID2 operator instead of generic error response from Java network library
  • Loading branch information
sunnywu authored Dec 12, 2023
1 parent 44f109f commit e47cdd8
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions src/main/java/com/uid2/client/PublisherUid2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,8 @@ public IdentityTokens generateToken(TokenGenerateInput tokenGenerateInput) {
.post(RequestBody.create(envelope.getEnvelope(), FORM))
.build();


try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new Uid2Exception("Unexpected code " + response);
}

String responseString = response.body().string();
final String responseString = getResponse(response);
return publisherUid2Helper.createIdentityfromTokenGenerateResponse(responseString, envelope);
} catch (IOException e) {
throw new Uid2Exception("error communicating with api endpoint", e);
Expand All @@ -62,16 +57,32 @@ public TokenGenerateResponse generateTokenResponse(TokenGenerateInput tokenGener


try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new Uid2Exception("Unexpected code " + response);
}

String responseString = response.body() != null ? response.body().string() : "";
final String responseString = getResponse(response);
return publisherUid2Helper.createTokenGenerateResponse(responseString, envelope);
} catch (IOException e) {
throw new Uid2Exception("Error communicating with api endpoint", e);
}
}

private static String getResponse(Response response) {

String responseString = "";

try {
if(response == null) {
throw new Uid2Exception("Response is null");
}
else {
responseString = response.body() != null ? response.body().string() : response.toString();
if (!response.isSuccessful()) {
throw new Uid2Exception("Unexpected code " + responseString);
}
}
return responseString;
} catch (IOException e) {
throw new Uid2Exception("Error communicating with api endpoint", e);
}
}

/**
* @param currentIdentity the current IdentityTokens instance, typically retrieved from a user's session
Expand All @@ -86,10 +97,8 @@ public TokenRefreshResponse refreshToken(IdentityTokens currentIdentity) {


try (Response response = client.newCall(request).execute()) {
final String responseString = response.body() != null ? response.body().string() : "";
if (!response.isSuccessful()) {
throw new Uid2Exception("Unexpected code " + response + " " + responseString);
}
final String responseString = getResponse(response);


return PublisherUid2Helper.createTokenRefreshResponse(responseString, currentIdentity);
} catch (IOException e) {
Expand Down

0 comments on commit e47cdd8

Please sign in to comment.