Skip to content

Print response headers on error responses too#344

Open
rabbbit wants to merge 1 commit into
yarpc:devfrom
rabbbit:pawel/err
Open

Print response headers on error responses too#344
rabbbit wants to merge 1 commit into
yarpc:devfrom
rabbbit:pawel/err

Conversation

@rabbbit

@rabbbit rabbbit commented Jun 25, 2021

Copy link
Copy Markdown
Collaborator

This would be super helpfull during debugging.

It is slightly unclear whether this should be printed on stdout or
stderr .. so I'm opting for the easiest option and printing it as is.

This would be super helpfull during debugging.

It is slightly unclear whether this should be printed on stdout or
stderr .. so I'm opting for the easiest option and printing it as is.
Comment thread main.go
}
bs, err = json.MarshalIndent(outSerialized, "", " ")
if err != nil {
out.Fatalf("Failed to convert map to JSON: %v\nMap: %+v\n", err, outSerialized)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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?

@rabbbit rabbbit Jun 28, 2021

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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 - out.Fatalf("Failed while making call: %s\n", buffer.String()) will cause a quit, but since we're in a defer I'm gonna get my headers out.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see now.

So.

  1. We'll always print the whole response - it just won't be serialized. This in itself might be good enough for most cases? (most structs should be serializable?)
  2. Headers are strings, so the serialization would need to fail for strings - I tried to break it (https://play.golang.org/p/7DsOl61mQRU), failed, but I guess it's possible (?)
  3. Most importantly - moving headers to a separate output can be a breaking change too, right? They're currently printed as the same dict.

Unless we do something like:

out.Printf("{", bs)
if len(headers) {
    out.Printf(headers)
}
if len (body) {
 if len(headers) {
    out.Printf(",", bs)
 }
 out.Printf(headers)
}
out.Printf("}", bs)

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 :)

Comment thread transport/grpc.go
Comment on lines +135 to +140
errs = multierr.Append(errs, err)

r, err := yarpcResponseToResponse(transportResponse)
errs = multierr.Append(errs, err)

return r, errs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we make this change? This implies yarpcResponseToResponse now has to be able to handle error cases of transportResponse now (nil, or some object with unknown state).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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 yarpcResponseToResponse always to be called, I think.

@Dogild Dogild left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please update the summary of the diff with example of:

  • TChannel/Thrift errors with headers output
  • gRPC/proto errors with headers
  • HTTP/json errors with headers

Comment thread main.go
if len(response.Headers) > 0 {
outSerialized["headers"] = response.Headers
}
outSerialized["body"] = responseMap

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment // Print the initial output body. is not up do date anymore, please move it to the defer function

Comment thread main.go
if response != nil && len(response.Headers) > 0 {
outSerialized["headers"] = response.Headers
}
defer func() {

Copy link
Copy Markdown

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 instance defer printOutputInJSON(outSerialized)

@rabbbit

rabbbit commented Jul 10, 2021 via email

Copy link
Copy Markdown
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants