Handle JsonSyntaxException when parsing error messages (#13083)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2022-07-05 13:22:46 +02:00 committed by GitHub
parent f5eabf0ba2
commit 4f1fb4ed51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -151,12 +151,15 @@ public class GroupePSAConnectApi {
switch (statusCode) {
case HttpStatus.NOT_FOUND_404:
ErrorObject error = gson.fromJson(response.getContentAsString(), ErrorObject.class);
String message = (error != null) ? error.getMessage() : null;
if (message == null) {
message = "Unknown";
ErrorObject error = null;
try {
error = gson.fromJson(response.getContentAsString(), ErrorObject.class);
} catch (JsonSyntaxException e) {
throw new GroupePSACommunicationException("Error in received JSON: " + getRootCause(e).getMessage(),
e);
}
throw new GroupePSACommunicationException(statusCode, message);
String message = (error == null) ? null : error.getMessage();
throw new GroupePSACommunicationException(statusCode, message == null ? "Unknown" : message);
case HttpStatus.FORBIDDEN_403:
case HttpStatus.UNAUTHORIZED_401: