Skip to content

Commit

Permalink
More info on ingress logs (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper committed Aug 6, 2024
1 parent f153deb commit c7aca83
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 26 deletions.
34 changes: 13 additions & 21 deletions sdk-common/src/main/java/dev/restate/sdk/client/DefaultClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public <Req, Res> CompletableFuture<Res> 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) {
Expand All @@ -67,8 +67,7 @@ public <Req, Res> CompletableFuture<Res> 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);
}
});
}
Expand All @@ -82,7 +81,7 @@ public <Req> CompletableFuture<SendResponse> 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);
Expand All @@ -94,8 +93,7 @@ public <Req> CompletableFuture<SendResponse> 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");
Expand All @@ -106,9 +104,7 @@ public <Req> CompletableFuture<SendResponse> 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"));
Expand All @@ -120,7 +116,7 @@ public AwakeableHandle awakeableHandle(String id) {
return new AwakeableHandle() {
private Void handleVoidResponse(HttpResponse<byte[]> 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) {
Expand Down Expand Up @@ -306,7 +302,7 @@ BiFunction<HttpResponse<byte[]>, Throwable, Output<Res>> handleGetOutputResponse
Serde<Res> 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) {
Expand All @@ -320,8 +316,7 @@ BiFunction<HttpResponse<byte[]>, Throwable, Output<Res>> 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);
}
};
}
Expand All @@ -330,7 +325,7 @@ BiFunction<HttpResponse<byte[]>, Throwable, Output<Res>> handleGetOutputResponse
Serde<Res> 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) {
Expand All @@ -340,8 +335,7 @@ BiFunction<HttpResponse<byte[]>, Throwable, Output<Res>> 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);
}
};
}
Expand Down Expand Up @@ -413,15 +407,13 @@ private void handleNonSuccessResponse(HttpResponse<byte[]> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte[]> response, Throwable cause) {
this(
message,
response.request().method(),
response.request().uri().toString(),
response.statusCode(),
response.body(),
cause);
}

IngressException(String message, HttpResponse<byte[]> 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() {
Expand All @@ -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()
Expand Down

0 comments on commit c7aca83

Please sign in to comment.