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) { switch (statusCode) {
case HttpStatus.NOT_FOUND_404: case HttpStatus.NOT_FOUND_404:
ErrorObject error = gson.fromJson(response.getContentAsString(), ErrorObject.class); ErrorObject error = null;
String message = (error != null) ? error.getMessage() : null; try {
if (message == null) { error = gson.fromJson(response.getContentAsString(), ErrorObject.class);
message = "Unknown"; } 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.FORBIDDEN_403:
case HttpStatus.UNAUTHORIZED_401: case HttpStatus.UNAUTHORIZED_401: