Skip to content

Commit

Permalink
passwordless http client stability fixes, problem details with errorC…
Browse files Browse the repository at this point in the history
…ode field
  • Loading branch information
WaciX committed Sep 10, 2023
1 parent ea05053 commit 630e020
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -72,12 +73,14 @@ public <R> R sendRequest(ClassicHttpRequest request, TypeReference<R> 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);
}
}

Expand Down Expand Up @@ -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<PasswordlessProblemDetails>() {
});
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class PasswordlessProblemDetails {
Integer status;
String detail;
String instance;
String errorCode;
}

0 comments on commit 630e020

Please sign in to comment.