-
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
Custom non-2xx response handling - DaprErrorResponseParser - Issue 783 #842
Conversation
Signed-off-by: Maciej <m.szymeczko@dac.digital>
Signed-off-by: Maciej <m.szymeczko@dac.digital>
* Bump from spring boot 2.3.5.RELEASE to 2.7.8 Signed-off-by: Sergio <champel@gmail.com> (cherry picked from commit 9152c91) * Ensure old versions of spring boot are still compatible Signed-off-by: Sergio <champel@gmail.com> --------- Signed-off-by: champel <champel@gmail.com> Signed-off-by: Sergio <champel@gmail.com> Signed-off-by: Maciej <m.szymeczko@dac.digital>
Signed-off-by: Maciej Szymeczko <maciek.047@gmail.com>
DaprError error; | ||
|
||
@Override | ||
public DaprException parse(@NotNull okhttp3.Response response) throws IOException { |
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.
Two points here. First, can we make this interface not depend on Okhttp3? This way we can migrate to any http client in the near future.
Second point is the the http implementation will be deprecated and removed soon and gRPC will be the only implementation. Users of the SDK should not care about the protocol, except for service invocation.
So, I see two ways to proceed with this PR: make this interface protocol agnostic, making it work on with gRPC and http; Or create a new method for HTTP service invocation only and make this be used only in HTTP service invocation.
Happy to get other idea on this that would address my first two concerns.
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.
Thanks for your input, I've added changes to address the concerns raised, the interface works now both with HTTP and gRPC and is passed via the DaprClientBuilder
. Let me know if this is a good direction.
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.
Sorry, I was out of vacation. I will review this in the next couple of days.
Signed-off-by: Maciej <m.szymeczko@dac.digital>
…o issue_783 # Conflicts: # sdk/src/main/java/io/dapr/client/DaprClientGrpc.java
Signed-off-by: Maciej <m.szymeczko@dac.digital>
Signed-off-by: Maciej <m.szymeczko@dac.digital>
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.
I like where this is going. Thanks a lot.
StatusRuntimeException statusException = (StatusRuntimeException) t; | ||
int statusCode = statusException.getStatus().getCode().value(); | ||
byte[] errorDetails = statusException.getStatus().getDescription() != null | ||
? statusException.getStatus().getDescription().getBytes(StandardCharsets.UTF_8) |
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.
Get encoding from our properties. See the Properties.java file.
import java.io.IOException; | ||
|
||
public interface DaprErrorResponseParser { | ||
DaprException parse(int statusCode, byte[] response) throws IOException; |
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.
What is statusCode? These are different for gRPC or HTTP. Maybe add a new param for the protocol, so the implementation can handle both. Another option would be to have one method for gRPC and one for HTTP.
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.
My intention with the DaprErrorResponseParser interface was to provide separate implementations for handling different protocols (as demonstrated by DefaultDaprHttpErrorResponseParser and DefaultDaprGrpcErrorResponseParser). I believe this approach maintains a clean separation of concerns between gRPC and HTTP error handling, which is why I opted not to include a protocol parameter in the interface. Ofc if you think there is a strong reason for having a single implementation handling both protocols, then I can change it
|
||
@Override | ||
public DaprException parse(int statusCode, byte[] response) { | ||
String errorMessage = (response == null || new String(response).isEmpty()) ? "HTTP status code: " + statusCode : new String(response, StandardCharsets.UTF_8); |
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.
Please, reference the encoding from the Properties.java file.
public class DefaultDaprGrpcErrorResponseParser implements DaprErrorResponseParser { | ||
@Override | ||
public DaprException parse(int statusCode, byte[] errorDetails) { | ||
String errorMessage = new String(errorDetails, StandardCharsets.UTF_8); |
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.
same.
try { | ||
error = OBJECT_MAPPER.readValue(response, DaprError.class); | ||
} catch (IOException e) { | ||
return new DaprException("UNKNOWN", new String(response, StandardCharsets.UTF_8)); |
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.
same.
@@ -139,7 +144,7 @@ public void invokeGetMethod() throws IOException { | |||
mockInterceptor.addRule() | |||
.get("http://127.0.0.1:3500/v1.0/get") | |||
.respond(serializer.serialize(EXPECTED_RESULT)); | |||
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient); | |||
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, okHttpClient, DEFAULT_PARSER); |
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.
Use an overloaded version of the constructor instead. So, there will not be changes needed everywhere.
Signed-off-by: Maciej <m.szymeczko@dac.digital>
Signed-off-by: Maciej <m.szymeczko@dac.digital>
Signed-off-by: Artur Souza <asouza.pro@gmail.com>
Codecov Report
@@ Coverage Diff @@
## master #842 +/- ##
============================================
- Coverage 77.82% 77.48% -0.34%
- Complexity 1267 1282 +15
============================================
Files 116 119 +3
Lines 3892 3936 +44
Branches 458 465 +7
============================================
+ Hits 3029 3050 +21
- Misses 630 650 +20
- Partials 233 236 +3
|
@maciek047 Hi, is there any updated? |
Hi @maciek047 , Could you please help to add more unit test to fix the coverage issue? It will not take a long time with GPT. |
gentle ping @maciek047 - mind updating this PR? |
Description
This PR aims to address the problems described in issue #783. It introduces the DaprErrorResponseParser interface, which allows for setting a custom parser for the DaprHttpClient. This enables the deserialization of responses with structures different from DaprError. The changes are backward compatible, as the current logic is wrapped in the DefaultDaprErrorResponseParser.
Issue reference
#783
Checklist
Please make sure you've completed the relevant tasks for this PR, out of the following list: