Skip to content

Commit e47cdd8

Browse files
authored
Show actual error response from UID2 operator instead of generic error 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
1 parent 44f109f commit e47cdd8

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/main/java/com/uid2/client/PublisherUid2Client.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,8 @@ public IdentityTokens generateToken(TokenGenerateInput tokenGenerateInput) {
3333
.post(RequestBody.create(envelope.getEnvelope(), FORM))
3434
.build();
3535

36-
3736
try (Response response = client.newCall(request).execute()) {
38-
if (!response.isSuccessful()) {
39-
throw new Uid2Exception("Unexpected code " + response);
40-
}
41-
42-
String responseString = response.body().string();
37+
final String responseString = getResponse(response);
4338
return publisherUid2Helper.createIdentityfromTokenGenerateResponse(responseString, envelope);
4439
} catch (IOException e) {
4540
throw new Uid2Exception("error communicating with api endpoint", e);
@@ -62,16 +57,32 @@ public TokenGenerateResponse generateTokenResponse(TokenGenerateInput tokenGener
6257

6358

6459
try (Response response = client.newCall(request).execute()) {
65-
if (!response.isSuccessful()) {
66-
throw new Uid2Exception("Unexpected code " + response);
67-
}
68-
69-
String responseString = response.body() != null ? response.body().string() : "";
60+
final String responseString = getResponse(response);
7061
return publisherUid2Helper.createTokenGenerateResponse(responseString, envelope);
7162
} catch (IOException e) {
7263
throw new Uid2Exception("Error communicating with api endpoint", e);
7364
}
7465
}
66+
67+
private static String getResponse(Response response) {
68+
69+
String responseString = "";
70+
71+
try {
72+
if(response == null) {
73+
throw new Uid2Exception("Response is null");
74+
}
75+
else {
76+
responseString = response.body() != null ? response.body().string() : response.toString();
77+
if (!response.isSuccessful()) {
78+
throw new Uid2Exception("Unexpected code " + responseString);
79+
}
80+
}
81+
return responseString;
82+
} catch (IOException e) {
83+
throw new Uid2Exception("Error communicating with api endpoint", e);
84+
}
85+
}
7586

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

8798

8899
try (Response response = client.newCall(request).execute()) {
89-
final String responseString = response.body() != null ? response.body().string() : "";
90-
if (!response.isSuccessful()) {
91-
throw new Uid2Exception("Unexpected code " + response + " " + responseString);
92-
}
100+
final String responseString = getResponse(response);
101+
93102

94103
return PublisherUid2Helper.createTokenRefreshResponse(responseString, currentIdentity);
95104
} catch (IOException e) {

0 commit comments

Comments
 (0)