|
1 | 1 | package de.gdata.vaas; |
2 | 2 |
|
3 | 3 | import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonSyntaxException; |
| 5 | + |
4 | 6 | import de.gdata.vaas.authentication.IAuthenticator; |
5 | 7 | import de.gdata.vaas.exceptions.VaasAuthenticationException; |
6 | 8 | import de.gdata.vaas.exceptions.VaasClientException; |
@@ -67,29 +69,27 @@ public Vaas(IAuthenticator authenticator) { |
67 | 69 | } |
68 | 70 |
|
69 | 71 | private static void throwParsedVaasError(HttpResponse<String> response) throws VaasAuthenticationException, VaasClientException, VaasServerException { |
70 | | - String responseBody = response.body(); |
71 | 72 | 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); |
84 | 83 | } |
85 | | - } catch (Exception e) { |
| 84 | + throw new VaasServerException(detail); |
| 85 | + } catch(JsonSyntaxException e) { |
86 | 86 | if (response.statusCode() == 401) { |
87 | 87 | throw new VaasAuthenticationException( |
88 | 88 | "Server did not accept token from identity provider. Check if you are using the correct identity provider and credentials."); |
89 | 89 | } 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()); |
91 | 91 | } else { |
92 | | - throw new VaasServerException("HTTP Error: " + response.statusCode()); |
| 92 | + throw new VaasServerException("HTTP Error: " + response.statusCode() + " for request " + response.uri()); |
93 | 93 | } |
94 | 94 | } |
95 | 95 | } |
|
0 commit comments