[oauth] Fix exception wrapping (#4150)

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen
2024-03-23 16:25:44 +01:00
committed by GitHub
parent c9ee992047
commit b3047c675a
@@ -327,11 +327,17 @@ public class OAuthConnector {
if (statusCode == HttpStatus.OK_200) {
AccessTokenResponse jsonResponse = gson.fromJson(content, AccessTokenResponse.class);
if (jsonResponse == null) {
throw new OAuthException("Empty response content when deserializing AccessTokenResponse");
}
jsonResponse.setCreatedOn(Instant.now()); // this is not supplied by the response
logger.debug("grant type {} to URL {} success", grantType, request.getURI());
return jsonResponse;
} else if (statusCode == HttpStatus.BAD_REQUEST_400) {
OAuthResponseException errorResponse = gson.fromJson(content, OAuthResponseException.class);
if (errorResponse == null) {
throw new OAuthException("Empty response content when deserializing OAuthResponseException");
}
logger.error("grant type {} to URL {} failed with error code {}, description {}", grantType,
request.getURI(), errorResponse.getError(), errorResponse.getErrorDescription());
@@ -345,12 +351,8 @@ public class OAuthConnector {
throw new IOException("Exception in oauth communication, grant type " + grantType, e);
} catch (JsonSyntaxException e) {
throw new OAuthException(String.format(
"Unable to deserialize json into AccessTokenResponse/ OAuthResponseException. httpCode: %d json: %s: %s",
"Unable to deserialize json into AccessTokenResponse/OAuthResponseException. httpCode: %d json: %s: %s",
statusCode, content, e.getMessage()), e);
} catch (Exception e) {
// Dont know what exception it is, wrap it up and throw it out
throw new OAuthException(
"Exception in oauth communication, grant type " + grantType + ": " + e.getMessage(), e);
}
}