-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the dapr runtime returned error details to the Java DaprException #998
Changes from 13 commits
139a230
c97dd51
783ebf2
cf64bc4
a402db2
c217129
f8ffb2d
967100e
69b0d68
eb96cf2
d377fad
d7b492d
c23c685
77975d8
896e997
84ebb63
41a36ac
14c1880
1cf7325
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,14 @@ | |
|
||
import io.dapr.client.DaprClient; | ||
import io.dapr.client.DaprClientBuilder; | ||
import io.dapr.exceptions.DaprErrorDetails; | ||
import io.dapr.exceptions.DaprException; | ||
import io.dapr.utils.TypeRef; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* 1. Build and install jars: | ||
|
@@ -33,17 +40,17 @@ public class Client { | |
*/ | ||
public static void main(String[] args) throws Exception { | ||
try (DaprClient client = new DaprClientBuilder().build()) { | ||
|
||
try { | ||
client.getState("Unknown state store", "myKey", String.class).block(); | ||
client.publishEvent("unknown_pubsub", "mytopic", "mydata").block(); | ||
} catch (DaprException exception) { | ||
System.out.println("Error code: " + exception.getErrorCode()); | ||
System.out.println("Error message: " + exception.getMessage()); | ||
|
||
exception.printStackTrace(); | ||
System.out.println("Dapr exception's error code: " + exception.getErrorCode()); | ||
System.out.println("Dapr exception's message: " + exception.getMessage()); | ||
System.out.println("Dapr exception's reason: " + exception.getStatusDetails().get( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clarifying my change: this is the opinionated layer in the SDK and determines how users can retrieve error details. We don't enumerate all the attributes on purpose. We can decide to change this later but I think this is a good first design to give a semi-structured way to fetch error details. |
||
DaprErrorDetails.ErrorDetailType.ERROR_INFO, | ||
"reason", | ||
TypeRef.STRING)); | ||
} | ||
|
||
System.out.println("Done"); | ||
} | ||
System.out.println("Done"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
import io.dapr.client.domain.Metadata; | ||
import io.dapr.config.Properties; | ||
import io.dapr.exceptions.DaprError; | ||
import io.dapr.exceptions.DaprErrorDetails; | ||
import io.dapr.exceptions.DaprException; | ||
import io.dapr.utils.Version; | ||
import okhttp3.Call; | ||
|
@@ -73,6 +74,11 @@ public class DaprHttp implements AutoCloseable { | |
private static final Set<String> ALLOWED_CONTEXT_IN_HEADERS = | ||
Collections.unmodifiableSet(new HashSet<>(Arrays.asList("grpc-trace-bin", "traceparent", "tracestate"))); | ||
|
||
/** | ||
* Object mapper to parse DaprError with or without details. | ||
*/ | ||
private static final ObjectMapper DAPR_ERROR_DETAILS_OBJECT_MAPPER = new ObjectMapper(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clarifying my change: no need for a custom ObjectMapper. The stock one is enough now. |
||
|
||
/** | ||
* HTTP Methods supported. | ||
*/ | ||
|
@@ -136,11 +142,6 @@ public int getStatusCode() { | |
*/ | ||
private static final byte[] EMPTY_BYTES = new byte[0]; | ||
|
||
/** | ||
* JSON Object Mapper. | ||
*/ | ||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
||
/** | ||
* Endpoint used to communicate to Dapr's HTTP endpoint. | ||
*/ | ||
|
@@ -347,12 +348,13 @@ private static DaprError parseDaprError(byte[] json) { | |
} | ||
|
||
try { | ||
return OBJECT_MAPPER.readValue(json, DaprError.class); | ||
return DAPR_ERROR_DETAILS_OBJECT_MAPPER.readValue(json, DaprError.class); | ||
} catch (IOException e) { | ||
throw new DaprException("UNKNOWN", new String(json, StandardCharsets.UTF_8)); | ||
} | ||
} | ||
|
||
|
||
private static byte[] getBodyBytesOrEmptyArray(okhttp3.Response response) throws IOException { | ||
ResponseBody body = response.body(); | ||
if (body != null) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clarifying my change: keeping this example simple.