-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: io: change Copy error to distinguish reader from writer #44571
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
Comments
If you were able to determine that the reader, or the writer, was the cause of the error, what would that let you do? |
Can you give an example of the case which you need to distinguish? In many cases I would expect the error would be an |
At this point I'm not at all sure we can change the errors being returned here. We could potentially provide Reader and Writer wrappers that remember errors so that you can check them after the call, but they'd have to forward ReadFrom etc checks, which would require other changes to Copy. |
This proposal has been added to the active column of the proposals project |
Condiser below scenarios:
|
Is it feasible to change in below way? type ErrWrite struct{ err error }
func (e ErrWrite) Unwrap() error {return e.err}
type ErrRead struct{ err error }
func (e ErrRead) Unwrap() error {return e.err}
// ... code snippets from copyBuffer
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw < 0 || nr < nw {
nw = 0
if ew == nil {
ew = ErrWrite{errInvalidWrite} // wrap
}
}
written += int64(nw)
if ew != nil {
err = ErrWrite{ew} // wrap
break
}
if nr != nw {
err = ErrWrite{ErrShortWrite} // wrap
break
}
}
if er != nil {
if er != EOF {
err = ErrRead{er} // wrap
}
break
}
}
// ... |
This seems like something that does not come up a lot. It is also specific to io.Copy. We just need names for the wrappers. I wrote one of these a few weeks ago and called it a WriteErrorSaver, which I'm not wild about. Any better, shorter names? |
OK, in the absence of better names, it sounds like the proposal is
and similarly for Read. This has the property of breaking the io.Copy optimizations for ReadFrom and WriteTo. Thoughts? |
Given the impact on the optimisations, the size of the code required, and the frequency of need; would it not be better to have this as an example showing how specific |
In this way we can still have the optimizations. |
People can write the wrappers I listed above themselves. There's no clear reason they must be in the standard library. |
Maybe just append some guide in the doca? |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
I encountered a need where different operation is performed based on the error from io.Copy is a read error or write error.
Currently I can only guess the following:
err != nil && n > 0
likely be a write error, but can be a successful write followed by a failed readerr != nil && n == 0
can be either a failed read or failed writeIt would be good such check as below is valid.
I am happy to contribute is if this is nice to have.
The text was updated successfully, but these errors were encountered: