diff --git a/src/main/java/com/bitwarden/passwordless/PasswordlessHttpClient.java b/src/main/java/com/bitwarden/passwordless/PasswordlessHttpClient.java index b471f49..37f0ea6 100644 --- a/src/main/java/com/bitwarden/passwordless/PasswordlessHttpClient.java +++ b/src/main/java/com/bitwarden/passwordless/PasswordlessHttpClient.java @@ -21,6 +21,7 @@ import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.Objects; @@ -72,12 +73,14 @@ public R sendRequest(ClassicHttpRequest request, TypeReference typeRefere } if (entity != null) { - String responseBody = EntityUtils.toString(entity); + byte[] responseBytes = EntityUtils.toByteArray(entity); - log.debug("Response body {}", responseBody); + if (log.isDebugEnabled()) { + log.debug("Response body {}", new String(responseBytes, StandardCharsets.UTF_8)); + } - if (typeReference != null) { - return objectMapper.readValue(responseBody, typeReference); + if (typeReference != null && responseBytes != null && responseBytes.length > 0) { + return objectMapper.readValue(responseBytes, typeReference); } } @@ -131,7 +134,8 @@ private PasswordlessProblemDetails buildProblemDetails(ClassicHttpResponse respo PasswordlessProblemDetails details = null; if (entity != null) { Header contentType = response.getHeader("content-type"); - if (contentType != null && contentType.getValue().equalsIgnoreCase("application/problem+json")) { + if (contentType != null + && contentType.getValue().equalsIgnoreCase(ContentType.APPLICATION_PROBLEM_JSON.getMimeType())) { try (InputStream inStream = entity.getContent()) { details = objectMapper.readValue(inStream, new TypeReference() { }); @@ -144,14 +148,17 @@ private PasswordlessProblemDetails buildProblemDetails(ClassicHttpResponse respo String errorDetail = null; if (entity != null) { - errorDetail = EntityUtils.toString(entity); + byte[] responseBytes = EntityUtils.toByteArray(entity); + if (responseBytes != null && responseBytes.length > 0) { + errorDetail = new String(responseBytes, StandardCharsets.UTF_8); + } } details = PasswordlessProblemDetails.builder() + .type("https://docs.passwordless.dev/guide/errors.html") .status(response.getCode()) - .title("unexpected_error") + .title("Unexpected error") .detail(errorDetail) - .type("https://docs.passwordless.dev/errors") .build(); } diff --git a/src/main/java/com/bitwarden/passwordless/error/PasswordlessProblemDetails.java b/src/main/java/com/bitwarden/passwordless/error/PasswordlessProblemDetails.java index fc22d2a..bf664ad 100644 --- a/src/main/java/com/bitwarden/passwordless/error/PasswordlessProblemDetails.java +++ b/src/main/java/com/bitwarden/passwordless/error/PasswordlessProblemDetails.java @@ -12,4 +12,5 @@ public class PasswordlessProblemDetails { Integer status; String detail; String instance; + String errorCode; }