-
Notifications
You must be signed in to change notification settings - Fork 892
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
Completely overhaul error handling #358
Conversation
This combines quick_error! with some of Cargo's ideas. It defines the ChainError trait and the ChainedError struct. With it any Result<T, E> where E: std::error::Error + Send + 'static has a chain_error method that takes yet another Error + Send + 'static. It returns a Result<T, ChainedError> containing both the boxed cause and the boxed new error. BasicError is for simple errors that don't deserve a new variant, and is used like BasicError::new("msg"). It then exports the easy_error! trait which is exactly like quick_error! but automatically defines BasicError and ChainError variants with From conversions from the corresponding types.
Sorry for the sloppy writing. Been trying to get this done before the week starts. |
I'd like to name this library |
raw::symlink_dir(src, dest).map_err(|e| { | ||
Error::LinkingDirectory { | ||
Ok(try!(raw::symlink_dir(src, dest).chain_err(|| { | ||
ErrorKind::LinkingDirectory { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this pattern of wrapping in Ok(try!
is not necessary here, an artifact of earlier iterations.
I'm pretty stoked to read this diff |
[root] | ||
name = "rustup-error" | ||
version = "0.1.7" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stray file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
Nice! The support library here seems pretty awesome. Overall this seems like huge win by not throwing away error contexts wherever possible and making it easier to define new errors, I'd be fine merging basically whenever you're comfortable as well. Some thoughts of mine:
|
Very nice! Do you think a similar refactoring would work for the Notification system? I see notifications and errors as being symmetric WRT variance, ie. errors are covariant while notifications are contravariant. |
The basic strategy this uses to compose and convert between error types might work for creating the notification heirarchy without nightly features, but I'm not sure. One tricky thing with notifications is that they don't want to allocate. The errors make a huge concession that it's ok to do a lot of allocation work on the (rare) error path. Notifications though are on the non-error path, so it's harder to justify moving things onto the heap just to maybe print them to the console. |
Seems to be freebsd/netbsd nightly issues. |
This is a huge refactor to try to make error handling easier, in particular to encourage correct chaining of errors, and use of the correct error type. It makes it easy to just use strings as errors for simple cases. Introduces the
rustup_error
crate, the documentation for which follows.I'd suggest not reading the individual commits because they are just small refactoring steps.
Based on quick_error! and Cargo's chain_error method.
This crate defines an opinionated strategy for error handling in Rust,
built on the following principles:
makes it easy to "chain" errors with the
chain_err
method.at the error site with just a string.
consistent way -
From
conversion behavior is never specifiedexplicitly.
Similar to other libraries like error-type and quick-error, this
library defines a macro,
declare_errors!
that declares the typesand implementation boilerplate necessary for fulfilling a
particular error-hadling strategy. Most importantly it defines
a custom error type (called
Error
by convention) and theFrom
conversions that let the
try!
macro and?
operator work.This library differs in a few ways:
Error
type as an enum, it is astruct containing an
ErrorKind
(which defines thedescription
anddisplay
methods for the error), an opaque,optional, boxed
std::error::Error + Send + 'static
object(which defines the
cause
, and establishes the links in theerror chain), and a
Backtrace
.ChainErr
, that defines achain_err
method. This methodon all
std::error::Error + Send + 'static
types extendsthe error chain by boxing the current error into an opaque
object and putting it inside a new concrete error.
From
conversions between other error typesdefined by the
declare_errors!
that preserve type information.From
conversions between any other errortype that hide the type of the other error in the
cause
box.propagates it down the stack through
From
andChainErr
conversions.
To accomplish its goals it makes some tradeoffs:
Error
andErrorKind
types can make itslightly more cumbersome to introduce new, unchained,
errors, requiring an
Into
orFrom
conversion; as well asslightly more cumbersome to match on errors with another layer
of types to match.
std::error::Error + Send + 'static
objects,it can't implement
PartialEq
for easy comparisons.Declaring error types
Generally, you define one family of error types per crate, though
it's also perfectly fine to define error types on a finer-grained
basis, such as per module.
Assuming you are using crate-level error types, typically you will
define an
errors
module and inside it calldeclare_errors!
:This populates the the module with a number of definitions,
the most of important of which are the
Error
typeand the
ErrorKind
type. They look something like thefollowing:
This is the basic error structure. You can see that
ErrorKind
has been populated in a variety of ways. All
ErrorKind
s get aMsg
variant for basic errors. When strings are converted toErrorKind
s they becomeErrorKind::Msg
. The "links" defined inthe macro are expanded to
Dist
andUtils
variants, and the"foreign links" to the
Temp
variant.Both types come with a variety of
From
conversians as well:Error
can be created fromErrorKind
, from&str
andString
,and from the "link" and "foreign_link" error types.
ErrorKind
can be created from the corresponding
ErrorKind
s of the linktypes, as wall as from
&str
andString
.into() and
From::from` are used heavily to massage types intothe right shape. Which one to use in any specific case depends on
the influence of type inference, but there are some patterns that
arise frequently.
Returning new errors
Introducing new error chains, with a string message:
Introducing new error chains, with an
ErrorKind
:Note that the return type is is the typedef
Result
, which isdefined by the macro as
pub type Result<T> = ::std::result::Result<T, Error>
. Note that in both cases.into()
is called to convert a type into theError
type: bothstrings and
ErrorKind
haveFrom
conversions to turn them intoError
.When the error is emitted inside a
try!
macro or behind the?
operator, then the explicit conversion isn't needed, sincethe behavior of
try!
will automatically convertErr(ErrorKind)
to
Err(Error)
. So the below is equivalent to the previous:Chaining errors
To extend the error chain:
chain_err
can be called on anyResult
type where the containederror type implements
std::error::Error + Send + 'static
. Ifthe
Result
is anErr
thenchain_err
evaluates the closure,which returns some type that can be converted to
ErrorKind
,boxes the original error to store as the cause, then returns a new
error containing the original error.
Foreign links
Errors that do not conform to the same conventions as this library
can still be included in the error chain. They are considered "foreign
errors", and are declared using the
foreign_links
block of thedeclare_errors!
macro.Error
s are automatically created fromforeign errors by the
try!
macro.Foreign links and regular links have one crucial difference:
From
conversions for regular links do not introduce a new errorinto the error chain, while conversions for foreign links always
introduce a new error into the error chain. So for the example
above all errors deriving from the
temp::Error
type will bepresented to the user as a new
ErrorKind::Temp
variant, and thecause will be the original
temp::Error
error. In contrast, whenrustup_utils::Error
is converted toError
the twoErrorKinds
are converted between each other to create a new
Error
but theold error is discarded; there is no "cause" created from the
original error.
Backtraces
The earliest non-foreign error to be generated creates a single
backtrace, which is passed through all
From
conversions andchain_err
invocations of compatible types. To read the backtracejust call the
backtrace()
method.Iteration
The
iter
method returns an iterator over the chain of error boxes.