-
Notifications
You must be signed in to change notification settings - Fork 144
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
Introduce TransportError type #374
Conversation
It is not very convenient to use () for Result error type. Unit type does not implement Error trait and thus cannot be used with the "?" operator. It is not very convenient for users which may need to return arbitrary errors from their transport callbacks. These may be IO errors from the standard library or some custom errors. Introduce a new type "TransportError" which will represent errors for transport callbacks. It is an enumeration which can hold three kinds of errors: - Arbitrary std::error::Error type This kind allows to wrap any other error into a TransportError. We implement a From conversion so that TransportError works with the "?" operator. This makes it very convenient to forward existing errors out of SecureSessionTransport callbacks. - Custom human-readable string This kind is useful when you don't have an Error handy but still want to return something more descriptive. Anything that can be converted into a String will do. - Unspecified kind This kind is useful to explicity say that we do not have any detailed information on the error. It will be used in some conversions of raw Themis errors. Unfortunately, it's not possible to implement Error trait for TransportError because of a blanket impl on From/Into traits: it conflicts with out custom From<T: Error> implementation. Well, it's not a big deal for now, but may bite us later. However, we do implement all other Error requirements like Display so the error should be convenient enough to use. Note that the error itself is a struct which wraps an enumeration. This allows us to not expose enumeration variants to the user. Also note the additional "Send + Sync" trait bounds on the Error impl. It is important to have these traits implemented if we want to be able to transfer errors between threads.
Update the SecureSessionTransport trait to use the new type. Update usage in tests. Note how "?" is used to forward channel errors and TransportError::new() usage for custom error reporting.
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 believe that having a separate error type for transport errors is a great idea!
Yeah, Rust has amazing ergonomics at times. While you have to spell out all these However, this keeps all the benefits of strong static typing (you always know the precise type of the error, unless you explicitly erase it with something like |
It is not very convenient to use
()
for Result error type. Unit type does not implement Error trait and thus cannot be used with the?
operator. It is not very convenient for users which may need to return arbitrary errors from their transport callbacks. These may be IO errors from the standard library or some custom errors.Introduce a new type TransportError which will represent errors for transport callbacks. It is an enumeration which can hold three kinds of errors:
Arbitrary std::error::Error type
This kind allows to wrap any other error into a TransportError. We implement a
From
conversion so that TransportError works with the?
operator. This makes it very convenient to forward existing errors out of SecureSessionTransport callbacks.Custom human-readable string
This kind is useful when you don't have an
Error
handy but still want to return something more descriptive. Anything that can be converted into aString
will do.Unspecified kind
This kind is useful to explicity say that we do not have any detailed information on the error. It will be used in some conversions of raw Themis errors.
Unfortunately, it's not possible to implement
Error
trait for TransportError because of a blanket impl on From/Into traits: it conflicts with out customFrom<T: Error>
implementation. Well, it's not a big deal for now, but may bite us later.However, we do implement all other Error requirements like
Display
so the error should be convenient enough to use.Note that the error itself is a struct which wraps an enumeration. This allows us to not expose enumeration variants to the user.
Also note the additional
Send + Sync
trait bounds on the Error impl. It is important to have these traits implemented if we want to be able to transfer errors between threads.Update the SecureSessionTransport trait to use the new type.
Update usage in tests. Note how
?
is used to forward channel errors andTransportError::new()
usage for custom error reporting.