Skip to content

Commit a00646f

Browse files
committed
java: Fix server error message parsing
1 parent 6a29ffb commit a00646f

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

java/src/main/java/de/gdata/vaas/Vaas.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package de.gdata.vaas;
22

33
import com.google.gson.Gson;
4+
import com.google.gson.JsonSyntaxException;
5+
46
import de.gdata.vaas.authentication.IAuthenticator;
57
import de.gdata.vaas.exceptions.VaasAuthenticationException;
68
import de.gdata.vaas.exceptions.VaasClientException;
@@ -67,29 +69,27 @@ public Vaas(IAuthenticator authenticator) {
6769
}
6870

6971
private static void throwParsedVaasError(HttpResponse<String> response) throws VaasAuthenticationException, VaasClientException, VaasServerException {
70-
String responseBody = response.body();
7172
try {
72-
var problemDetails = new Gson().fromJson(responseBody, Map.class);
73-
if (problemDetails != null) {
74-
var type = (String) problemDetails.getOrDefault("type", "");
75-
var detail = (String) problemDetails.getOrDefault("detail", "Unknown error");
76-
if (type.equals("VaasClientException")) {
77-
throw new VaasClientException(detail);
78-
} else if (type.equals("VaasAuthenticationException")) {
79-
throw new VaasAuthenticationException(detail);
80-
}
81-
throw new VaasServerException(detail);
82-
} else {
83-
throw new VaasServerException("Invalid JSON error response from server");
73+
var problemDetails = ProblemDetails.fromJson(response.body());
74+
if(problemDetails == null) {
75+
throw new JsonSyntaxException("no JSON in server response");
76+
}
77+
var type = problemDetails.type;
78+
var detail = problemDetails.detail;
79+
if (type.equals("VaasClientException")) {
80+
throw new VaasClientException(detail);
81+
} else if (type.equals("VaasAuthenticationException")) {
82+
throw new VaasAuthenticationException(detail);
8483
}
85-
} catch (Exception e) {
84+
throw new VaasServerException(detail);
85+
} catch(JsonSyntaxException e) {
8686
if (response.statusCode() == 401) {
8787
throw new VaasAuthenticationException(
8888
"Server did not accept token from identity provider. Check if you are using the correct identity provider and credentials.");
8989
} else if (response.statusCode() >= 400 && response.statusCode() < 500) {
90-
throw new VaasClientException("HTTP Error: " + response.statusCode());
90+
throw new VaasClientException("HTTP Error: " + response.statusCode() + " for request " + response.uri());
9191
} else {
92-
throw new VaasServerException("HTTP Error: " + response.statusCode());
92+
throw new VaasServerException("HTTP Error: " + response.statusCode() + " for request " + response.uri());
9393
}
9494
}
9595
}

java/src/main/java/de/gdata/vaas/messages/ProblemDetails.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package de.gdata.vaas.messages;
22

3-
import org.jetbrains.annotations.Nullable;
3+
import org.jetbrains.annotations.NotNull;
44

55
import com.google.gson.Gson;
66
import com.google.gson.annotations.SerializedName;
@@ -14,11 +14,11 @@
1414
@NoArgsConstructor
1515
public class ProblemDetails
1616
{
17-
@Nullable
17+
@NotNull
1818
@SerializedName("type")
1919
public String type;
2020

21-
@Nullable
21+
@NotNull
2222
@SerializedName("detail")
2323
public String detail;
2424

0 commit comments

Comments
 (0)