-
Notifications
You must be signed in to change notification settings - Fork 15
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
feat: improve error response messages #614
Conversation
a5e8c90
to
79ba202
Compare
79ba202
to
f0cf7fd
Compare
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.
A couple of nits, but overall looks good to me
public Response intercept(Chain chain) throws IOException { | ||
// Don't modify the request, but get the response | ||
Response response = chain.proceed(chain.request()); | ||
if (!"HEAD".equals(chain.request().method()) // skip HEAD requests because they have no body |
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.
This is redundant with response.body() != null
below
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 would keep it if there's an event where the response of a non-HEAD request fails and the body doesn't exist.
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 mean it's other way around - we don't need HEAD check if we already have check for body not been 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.
I think @eiri means that we could skip the HEAD
check and just use the body check, but if we want to do that we need to be sure on the subtlety any of empty string vs 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.
Ok, I'm open to removing HEAD
check. If the body is not null and just empty, the GSON deserialize code should returns a null
response. Here's the doc for fromJson
:
Returns:
an object of type T from the string. Returns null if json is null or if json is empty.
If we don't want it to reach that code then we can add a response.body().toString().isEmpty()
check.
Edited: Tried out the change in a branch and seems to work fine. Also added a test (to verify) for GET request with no body and that passes.
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 in 8411bd3
if (errorBody != null) { | ||
// Don't augment if there is already a trace present | ||
if (!errorBody.has("trace")) { | ||
String error = Optional.ofNullable(errorBody.get("error")).map(JsonElement::getAsString).orElse(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.
A nit, but logically those two lines fit better after if (!errorBody.has("errors")) {
check
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 in 8411bd3
Add ErrorTransformInterceptor. Modify CloudantBaseService to apply interceptor. New tests in ErrorTransformInterceptorTest and CloudantErrorInterceptorTest. Co-authored-by: Rich Ellis <[email protected]>
8411bd3
to
1369e17
Compare
PR summary
Add interceptor for error response augmentation
Fixes: s1002
Note: An existing issue is required before opening a PR.
PR Checklist
Please make sure that your PR fulfills the following requirements:
Angular Commit Message Guidelines.
PR Type
What is the current behavior?
Cloudant/CouchDB error models are handled by the core with only the
error
field being processed. Obtaining e.g. thereason
field requires additional user code.What is the new behavior?
The error response is augmented with
errors
andtrace
properties.This improves the way the core handles the error response and enhances the user facing exception string to include the
reason
.The
trace
is the CouchDB request ID which can be used to correlate with logs.Does this PR introduce a breaking change?
The existing error model fields are retained, the new fields are backwards compatible.
Exception strings will be different, but the types remain the same.
Other information
Add
ErrorTransformInterceptor
.Modify
CloudantBaseService
to apply interceptor.New tests in
ErrorTransformInterceptorTest
andCloudantErrorInterceptorTest
.