unexpected behaviour for Custom std::io::Error
source implementation
#124513
Labels
C-bug
Category: This is a bug.
std::io::Error
source implementation
#124513
At the time of writing this bug ticket,
std::io::Error
's source implementation is as follows:If you call:
with
MyError
being defined as:Then that source will be
None
.Seems to me like a bug? How so is there no source?
If you want something deeper, or even as far as the "root cause" then it is up to the user to go deeper, no? Because now let's consider:
And now call:
Now my source is
MyError
.Which means that
WrapError
is invisible to the user. If you argument is now, great right, because the actual root cause was returned. Well yes, you are correct, but that is only a side-effect and is neither expected and neither will it hold up as soon as you add one more layer...To be fair I never create
std::io::Error
myself, I only discovered it as I was using it for a test case out of laziness. It's because I have a custom error module to easily go through the chain of errors, for which I use the source. This has always worked out for me with the assumption that source returns the wrapped error if there is any. And the chain stops when that returns None.AFAIK that is also what
anyhow
relies upon, a crate I do not use myself but which is commonly used in the Rust community, so that's why I do reference it here for extra context.But my chain logic and anyhow's logic alike fails if somewhere in the "chain" there is an
std::io::Error
. Then again not sure when you would ever look for something lower then thestd::io::Error
, but I can imagine that some low level http-like crate might use that for some reason? Not sure. Either way, it's behaviour that surprised me.Whether that's wrong or I am just having wrong assumptions that I am not certain of.
Instead I would expect that source to be implemented as:
Which would then correctly give me the next error in the chain (the inner error of
std::io::Error
.Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: