-
Notifications
You must be signed in to change notification settings - Fork 35
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
Print response headers on error responses too #344
base: dev
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -472,6 +472,23 @@ func makeContextWithTrace(ctx context.Context, t transport.Transport, request *t | |
|
||
func makeInitialRequest(out output, transport transport.Transport, serializer encoding.Serializer, req *transport.Request) { | ||
response, err := makeRequestWithTracePriority(transport, req, 1) | ||
|
||
var bs []byte | ||
outSerialized := map[string]interface{}{} | ||
if response != nil && len(response.Headers) > 0 { | ||
outSerialized["headers"] = response.Headers | ||
} | ||
defer func() { | ||
if len(outSerialized) == 0 { | ||
return | ||
} | ||
bs, err = json.MarshalIndent(outSerialized, "", " ") | ||
if err != nil { | ||
out.Fatalf("Failed to convert map to JSON: %v\nMap: %+v\n", err, outSerialized) | ||
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. Would that cause yab to quit? Some combination of non-serializable header could break otherwise working yab. Sould it be a non-quitting warn instead? 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. This is a defer - so we still print it out last. I don't see how this could affect yab qutiting. Moving it up here to achieve the opposite - 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. We have headers+body now. If body marshals fine but header fails, that will change the original functionality and nothing will be printed (unless I'm misreading something somewhere). It might be better to have separate marshaling for each, and if header fails skip it, printing body only to preserve backward compatibility. 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. Oh, I see now. So.
Unless we do something like:
It would be tricky to preseve the same output - we'd need a json output template of style. Given the low likelihood of headers not being serializable, the hacks above feel like an overkill - it feels like non-serializable headers are rare enough that it shouldn't be a problem? Also see the code above that blindly fatalfs if it fails to parse proto error details - it does not even try to handle this case, and print the output. Happy to make a change if you find it necessary though. If so, please advise how you'd see it - it feels like we'll break some backwards compatibility either way :) |
||
} | ||
out.Printf("%s\n\n", bs) | ||
}() | ||
|
||
if err != nil { | ||
buffer := bytes.NewBufferString(err.Error()) | ||
|
||
|
@@ -502,20 +519,10 @@ func makeInitialRequest(out output, transport transport.Transport, serializer en | |
} | ||
|
||
// Print the initial output body. | ||
outSerialized := map[string]interface{}{ | ||
"body": responseMap, | ||
} | ||
if len(response.Headers) > 0 { | ||
outSerialized["headers"] = response.Headers | ||
} | ||
outSerialized["body"] = responseMap | ||
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. The comment |
||
for k, v := range response.TransportFields { | ||
outSerialized[k] = v | ||
} | ||
bs, err := json.MarshalIndent(outSerialized, "", " ") | ||
if err != nil { | ||
out.Fatalf("Failed to convert map to JSON: %v\nMap: %+v\n", err, responseMap) | ||
} | ||
out.Printf("%s\n\n", bs) | ||
} | ||
|
||
// isYabTemplate is currently very conservative, it requires a file that exists | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,11 +128,16 @@ func (t *grpcTransport) Call(ctx context.Context, request *Request) (*Response, | |
|
||
ctx, cancel := requestContextWithTimeout(ctx, request) | ||
defer cancel() | ||
|
||
var errs error | ||
|
||
transportResponse, err := t.Outbound.Call(ctx, t.requestToYARPCRequest(request)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return yarpcResponseToResponse(transportResponse) | ||
errs = multierr.Append(errs, err) | ||
|
||
r, err := yarpcResponseToResponse(transportResponse) | ||
errs = multierr.Append(errs, err) | ||
|
||
return r, errs | ||
Comment on lines
+135
to
+140
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. why do we make this change? This implies 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. Well, this because, previously, on error we should just bubble up error and ignore the response altogether. Since I now want to have response headers to be properly build/populated, we always want to build a proper response object, and return it together with the error. So, we want |
||
} | ||
|
||
func (t *grpcTransport) CallStream(ctx context.Context, request *StreamRequest) (*transport.ClientStream, error) { | ||
|
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.
nit: even if previously we had this piece of code in the function
makeInitialRequest
, can we expose in another method for better readability? For instancedefer printOutputInJSON(outSerialized)