diff --git a/sdk-common/src/main/java/dev/restate/sdk/client/DefaultClient.java b/sdk-common/src/main/java/dev/restate/sdk/client/DefaultClient.java index bd4a7343..810f07cc 100644 --- a/sdk-common/src/main/java/dev/restate/sdk/client/DefaultClient.java +++ b/sdk-common/src/main/java/dev/restate/sdk/client/DefaultClient.java @@ -57,7 +57,7 @@ public CompletableFuture callAsync( .handle( (response, throwable) -> { if (throwable != null) { - throw new IngressException("Error when executing the request", throwable); + throw new IngressException("Error when executing the request", response, throwable); } if (response.statusCode() >= 300) { @@ -67,8 +67,7 @@ public CompletableFuture callAsync( try { return resSerde.deserialize(response.body()); } catch (Exception e) { - throw new IngressException( - "Cannot deserialize the response", response.statusCode(), response.body(), e); + throw new IngressException("Cannot deserialize the response", response, e); } }); } @@ -82,7 +81,7 @@ public CompletableFuture sendAsync( .handle( (response, throwable) -> { if (throwable != null) { - throw new IngressException("Error when executing the request", throwable); + throw new IngressException("Error when executing the request", response, throwable); } if (response.statusCode() >= 300) { handleNonSuccessResponse(response); @@ -94,8 +93,7 @@ public CompletableFuture sendAsync( findStringFieldsInJsonObject( new ByteArrayInputStream(response.body()), "invocationId", "status"); } catch (Exception e) { - throw new IngressException( - "Cannot deserialize the response", response.statusCode(), response.body(), e); + throw new IngressException("Cannot deserialize the response", response, e); } String statusField = fields.get("status"); @@ -106,9 +104,7 @@ public CompletableFuture sendAsync( status = SendResponse.SendStatus.PREVIOUSLY_ACCEPTED; } else { throw new IngressException( - "Cannot deserialize the response status, got " + statusField, - response.statusCode(), - response.body()); + "Cannot deserialize the response status, got " + statusField, response); } return new SendResponse(status, fields.get("invocationId")); @@ -120,7 +116,7 @@ public AwakeableHandle awakeableHandle(String id) { return new AwakeableHandle() { private Void handleVoidResponse(HttpResponse response, Throwable throwable) { if (throwable != null) { - throw new IngressException("Error when executing the request", throwable); + throw new IngressException("Error when executing the request", response, throwable); } if (response.statusCode() >= 300) { @@ -306,7 +302,7 @@ BiFunction, Throwable, Output> handleGetOutputResponse Serde resSerde) { return (response, throwable) -> { if (throwable != null) { - throw new IngressException("Error when executing the request", throwable); + throw new IngressException("Error when executing the request", response, throwable); } if (response.statusCode() == 470) { @@ -320,8 +316,7 @@ BiFunction, Throwable, Output> handleGetOutputResponse try { return Output.ready(resSerde.deserialize(response.body())); } catch (Exception e) { - throw new IngressException( - "Cannot deserialize the response", response.statusCode(), response.body(), e); + throw new IngressException("Cannot deserialize the response", response, e); } }; } @@ -330,7 +325,7 @@ BiFunction, Throwable, Output> handleGetOutputResponse Serde resSerde) { return (response, throwable) -> { if (throwable != null) { - throw new IngressException("Error when executing the request", throwable); + throw new IngressException("Error when executing the request", response, throwable); } if (response.statusCode() >= 300) { @@ -340,8 +335,7 @@ BiFunction, Throwable, Output> handleGetOutputResponse try { return resSerde.deserialize(response.body()); } catch (Exception e) { - throw new IngressException( - "Cannot deserialize the response", response.statusCode(), response.body(), e); + throw new IngressException("Cannot deserialize the response", response, e); } }; } @@ -413,15 +407,13 @@ private void handleNonSuccessResponse(HttpResponse response) { errorMessage = findStringFieldInJsonObject(new ByteArrayInputStream(response.body()), "message"); } catch (Exception e) { - throw new IngressException( - "Can't decode error response from ingress", response.statusCode(), response.body(), e); + throw new IngressException("Can't decode error response from ingress", response, e); } - throw new IngressException(errorMessage, response.statusCode(), response.body()); + throw new IngressException(errorMessage, response); } // Fallback error - throw new IngressException( - "Received non success status code", response.statusCode(), response.body()); + throw new IngressException("Received non success status code", response); } private static String findStringFieldInJsonObject(InputStream body, String fieldName) diff --git a/sdk-common/src/main/java/dev/restate/sdk/client/IngressException.java b/sdk-common/src/main/java/dev/restate/sdk/client/IngressException.java index 36c1223b..830aa5ee 100644 --- a/sdk-common/src/main/java/dev/restate/sdk/client/IngressException.java +++ b/sdk-common/src/main/java/dev/restate/sdk/client/IngressException.java @@ -8,26 +8,66 @@ // https://github.com/restatedev/sdk-java/blob/main/LICENSE package dev.restate.sdk.client; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; import org.jspecify.annotations.Nullable; public class IngressException extends RuntimeException { + private final String requestMethod; + private final String requestURI; private final int statusCode; private final byte[] responseBody; - public IngressException(String message, Throwable cause) { - this(message, -1, null, cause); + public IngressException( + String message, String requestMethod, String requestURI, Throwable cause) { + this(message, requestMethod, requestURI, -1, null, cause); } - public IngressException(String message, int statusCode, byte[] responseBody) { - this(message, statusCode, responseBody, null); + IngressException(String message, HttpRequest request, Throwable cause) { + this(message, request.method(), request.uri().toString(), -1, null, cause); } - public IngressException(String message, int statusCode, byte[] responseBody, Throwable cause) { + IngressException(String message, HttpRequest request) { + this(message, request, null); + } + + public IngressException( + String message, + String requestMethod, + String requestURI, + int statusCode, + byte[] responseBody) { + this(message, requestMethod, requestURI, statusCode, responseBody, null); + } + + IngressException(String message, HttpResponse response, Throwable cause) { + this( + message, + response.request().method(), + response.request().uri().toString(), + response.statusCode(), + response.body(), + cause); + } + + IngressException(String message, HttpResponse response) { + this(message, response, null); + } + + public IngressException( + String message, + String requestMethod, + String requestURI, + int statusCode, + byte[] responseBody, + Throwable cause) { super(message, cause); this.statusCode = statusCode; this.responseBody = responseBody; + this.requestMethod = requestMethod; + this.requestURI = requestURI; } public int getStatusCode() { @@ -38,9 +78,21 @@ public int getStatusCode() { return responseBody; } + public String getRequestMethod() { + return requestMethod; + } + + public String getRequestURI() { + return requestURI; + } + @Override public String getMessage() { return "[" + + requestMethod + + " " + + requestURI + + "][Status: " + statusCode + "] " + super.getMessage()