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

Error handling with Go Readers/Writers #15

Open
jnichols-git opened this issue Jul 24, 2022 · 1 comment
Open

Error handling with Go Readers/Writers #15

jnichols-git opened this issue Jul 24, 2022 · 1 comment
Labels
help wanted Extra attention is needed stability Improve stability
Milestone

Comments

@jnichols-git
Copy link
Owner

Readers and Writers in Golang have interesting error behavior. Generally, any read/write operations will return an int count of how many pieces of data (usually bytes) were read into a slice. If that count is less than expected, an error will also be returned. These errors don't mean a permanent failure to read, so they can't just fail out of a function.

I was unable to find resources on handling these errors, and would love some help if someone is more knowledgeable about those particular operations.

@jnichols-git jnichols-git added help wanted Extra attention is needed stability Improve stability labels Jul 24, 2022
@jnichols-git jnichols-git added this to the V1 Release milestone Jul 24, 2022
@SuneelFreimuth
Copy link
Collaborator

I believe this postcondition is rarely used, as this doesn't make sense to implement for most Readers. Take this excerpt for example:

bodyReader := req.Body
body, err := ioutil.ReadAll(bodyReader)
if err != nil {
    return err
}
if err := json.Unmarshal(body, out); err != nil {
    return err
}
return nil

req.Body implements io.ReadCloser, so it must implement both Read() and Close(). Completely draining a raw request body (body, err := ioutil.ReadAll(bodyReader)) probably won't return an error except for EOF, since it treats the body as a simple stream of bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed stability Improve stability
Projects
None yet
Development

No branches or pull requests

2 participants