-
Notifications
You must be signed in to change notification settings - Fork 1
encode/decode DeadlineExpiredException types #30
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
Conversation
Generate changelog in
|
public interface ResponseEncodingAdapter<RESPONSE> { | ||
void setHeader(RESPONSE response, String headerName, String headerValue); | ||
} |
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.
We could control the status code here as well
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.
cool, done
public interface ResponseEncodingAdapter<RESPONSE> { | ||
void setHeader(RESPONSE response, String headerName, String headerValue); | ||
|
||
void setStatus(RESPONSE response, int status); |
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.
perhaps we should invert this and use the visitor style a la QosException
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.
to expand on that: reason being that in e.g. palantir/conjure-java#2454, we're leveraging the private ConjureExceptions#writeResponse
which accepts an http status as a parameter, but also contains useful logic that we probably want to re-use involved in writing the response body.
instead of including a setStatus()
method on the encoder, we could expose a basic method here:
public int httpStatusFor(DeadlineExpiredException exception) {
if (exception instanceof DeadlineExpiredException.External) {
return 400;
} else if (exception instanceof DeadlineExpiredException.Internal) {
return 500;
}
return 500;
}
thoughts?
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 think either way could work. I don't think we necessarily need to send a response at all (similarly to QosException), although I suppose an errorInstanceId
might be nice to quickly find the failures propagation path back to clients.
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.
updated to use the visitor thing. i don't love it... but i guess it works
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 think this is simpler: 8fbd209
@@ -31,6 +31,8 @@ private DeadlineExpiredException(String message) { | |||
super(message); | |||
} | |||
|
|||
public abstract int httpStatusCode(); |
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.
Slight preference to put these into a helper method on Deadlines.java
along the lines of int getHttpStatusCode(DeadlineExpiredException)
, that way the exceptions themselves aren't directly coupled to 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.
works for me, will fix
public static int getHttpStatusCode(DeadlineExpiredException.External _exception) { | ||
// external deadline expiration is considered a client error | ||
return 400; | ||
} | ||
|
||
public static int getHttpStatusCode(DeadlineExpiredException.Internal _exception) { | ||
// internal deadline expiration is considered a server error | ||
return 500; | ||
} |
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.
Thoughts on merging this to public static int getHttpStatusCode(DeadlineExpiredException exception) {
so we don't need to inspect the type to get the correct status code? Makes future additions a bit easier imo
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.
sure, fixed: 7669548
return reason.flatMap(s -> switch (s) { | ||
case "external" -> Optional.of(DeadlineExpiredException.external()); | ||
case "internal" -> Optional.of(DeadlineExpiredException.internal()); | ||
default -> Optional.empty(); | ||
}); |
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.
Should we check the status code for internal/external as well? Ideally we're as precise as possible, only producing DeadlineExpiredException.external
when the response status is 400, and internal when the status is 500
} | ||
|
||
public interface ResponseDecodingAdapter<RESPONSE> { | ||
Optional<String> getFirstHeader(RESPONSE response, String headerName); |
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.
Thoughts on using @Nullable
here instead of optional? I sort of wish we'd done that on the other adapter to avoid unnecessary optional allocation.
We could call it @Nullable String maybeFirstHeaer(RESPONSE response, String headerName)
, allowing us to add an equivalent method on the existing adapter in a follow-up
@Deprecated | ||
default Optional<String> getFirstHeader(RESPONSE response, String headerName) { | ||
return Optional.empty(); | ||
} |
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.
Since this type is new in this PR, we can remove this method
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.
ah whoops
Released 0.7.0 |
No description provided.