Skip to content
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

Add method for custom formatting of stream errors. #564

Conversation

johanbrandhorst
Copy link
Collaborator

Alternative to discussion in #538 (comment). This would allow a user to get errors as marshalled status.Proto() types with the use of both WithProtoStreamErrorFormatter and WithProtoErrorHandler.

This is pretty experimental so I'm happy to make any changes.

@abursavich
Copy link

I would much prefer the handler's signature to be more in the style of (Default)HTTPError, so that a client can write the stream error however they wish. In my case, I would like to construct my own error status proto directly and avoid my marshalers having to handle maps.

@johanbrandhorst
Copy link
Collaborator Author

Could you come up with a practical alternative? The stream is already established at this point, so a handler function may require rewriting of much of the logic.

@abursavich
Copy link

abursavich commented Mar 1, 2018

@johanbrandhorst
Copy link
Collaborator Author

@abursavich Thanks for that! I think one problem with your approach is that we expose a new function type, used by the user, which has an empty interface in the signature. That's pretty poor interface design. It also rewrites quite a bit of the logic, and it seems to add an error case if the response is nil, which may break backwards compatibility? I think your suggestion is a step in the right direction, as I would also like more control over the writing of mesages, but maybe the configuration should be more fine grained? One option for formatting the error and one for formatting a successful reply? I'm not sure.

@abursavich
Copy link

FWIW, the error on nil response was already there, I just lifted it out of streamChunk (which I deleted):

if result == nil {
return streamChunk(nil, fmt.Errorf("empty response"))
}

I don't like the empty interface much either, but I was matching the existing interface for Marshaler:

// Marshaler defines a conversion between byte sequence and gRPC payloads / fields.
type Marshaler interface {
// Marshal marshals "v" into byte sequence.
Marshal(v interface{}) ([]byte, error)
// Unmarshal unmarshals "data" into "v".
// "v" must be a pointer value.
Unmarshal(data []byte, v interface{}) error
// NewDecoder returns a Decoder which reads byte sequence from "r".
NewDecoder(r io.Reader) Decoder
// NewEncoder returns an Encoder which writes bytes sequence into "w".
NewEncoder(w io.Writer) Encoder
// ContentType returns the Content-Type which this marshaler is responsible for.
ContentType() string
}

@johanbrandhorst
Copy link
Collaborator Author

My heart is not really in this change after merging #561, so I will close this to let someone else provide a better solution to the specific problem of customizing stream error returns.

@johanbrandhorst johanbrandhorst deleted the add-proto-stream-error-formatter branch March 6, 2018 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants