refactor: simplify JSON parsing in handleError()

Omit reading lines to String first and pass the reader directly to the
JSON mapper.
This commit is contained in:
Stefan Kalscheuer 2024-04-27 12:04:44 +02:00
parent d04067db7e
commit dd5adf897a
Signed by: stefan
GPG Key ID: 3887EC2A53B55430

View File

@ -431,18 +431,19 @@ public final class RequestHelper implements Serializable {
* @throws VaultConnectorException Expected exception with details to throw * @throws VaultConnectorException Expected exception with details to throw
*/ */
private void handleError(final HttpResponse<InputStream> response) throws VaultConnectorException { private void handleError(final HttpResponse<InputStream> response) throws VaultConnectorException {
if (response.body() != null) { try (var body = response.body()) {
try (var reader = new BufferedReader(new InputStreamReader(response.body(), UTF_8))) { if (body != null) {
var responseString = reader.lines().collect(Collectors.joining("\n")); try (var reader = new BufferedReader(new InputStreamReader(body, UTF_8))) {
ErrorResponse er = jsonMapper.readValue(responseString, ErrorResponse.class); ErrorResponse er = jsonMapper.readValue(reader, ErrorResponse.class);
/* Check for "permission denied" response */ /* Check for "permission denied" response */
if (!er.getErrors().isEmpty() && er.getErrors().get(0).equals("permission denied")) { if (!er.getErrors().isEmpty() && er.getErrors().get(0).equals("permission denied")) {
throw new PermissionDeniedException(); throw new PermissionDeniedException();
} }
throw new InvalidResponseException(Error.RESPONSE_CODE, response.statusCode(), er.toString()); throw new InvalidResponseException(Error.RESPONSE_CODE, response.statusCode(), er.toString());
}
}
} catch (IOException ignored) { } catch (IOException ignored) {
// Exception ignored. // Exception ignored.
} }
} }
}
} }