Skip to content

Commit

Permalink
replace Error::cause with Error::source (#2370) (#201)
Browse files Browse the repository at this point in the history
Currently, linkerd2-proxy uses and implements `Error::cause`
for error handling.

This branch removes the use of the deprecated
`Error::cause` method and implements and uses the
`Error::source` method, as [recommended by the
documentation](https://doc.rust-lang.org/std/error/trait.Error.html#method.cause). To implement `Error::source`, there are
static lifetimes added to the `Error` traits. However, this
doesn't remove the [use of `cause2`](https://github.com/DebugSteven/linkerd2-proxy/blob/231cb67b1a2178627bbb304055d408cee1007fd9/src/proxy/http/glue.rs#L226) because
`hyper::Error` doesn't provide a `source` yet:
https://docs.rs/hyper/0.12.24/src/hyper/error.rs.html#289-333.
A separate issue could be opened to use `source` for
`hyper::Error` when it becomes available.

Fixes: #2370

Signed-off-by: J Haigh <DebugSteven@gmail.com>
  • Loading branch information
DebugSteven authored and hawkw committed Feb 27, 2019
1 parent 1ea7559 commit 0fe8063
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 22 deletions.
8 changes: 4 additions & 4 deletions lib/metrics/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl fmt::Display for ServeError {
f,
"{}: {}",
self.description(),
self.cause().expect("ServeError must have cause")
self.source().expect("ServeError must have source")
)
}
}
Expand All @@ -126,10 +126,10 @@ impl Error for ServeError {
}
}

fn cause(&self) -> Option<&Error> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match *self {
ServeError::Http(ref cause) => Some(cause),
ServeError::Io(ref cause) => Some(cause),
ServeError::Http(ref source) => Some(source),
ServeError::Io(ref source) => Some(source),
}
}
}
6 changes: 3 additions & 3 deletions lib/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ where

impl<T, U> error::Error for Error<T, U>
where
T: error::Error,
U: error::Error,
T: error::Error + 'static,
U: error::Error + 'static,
{
fn cause(&self) -> Option<&error::Error> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
Error::Inner(ref why) => Some(why),
Error::Route(ref why) => Some(why),
Expand Down
6 changes: 3 additions & 3 deletions lib/stack/src/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ impl<A: fmt::Display, B: fmt::Display> fmt::Display for Either<A, B> {
}

impl<A: error::Error, B: error::Error> error::Error for Either<A, B> {
fn cause(&self) -> Option<&error::Error> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Either::A(a) => a.cause(),
Either::B(b) => b.cause(),
Either::A(a) => a.source(),
Either::B(b) => b.source(),
}
}
}
4 changes: 2 additions & 2 deletions lib/timeout/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ where

impl<E> error::Error for Error<E>
where
E: error::Error,
E: error::Error + 'static,
{
fn cause(&self) -> Option<&error::Error> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
Error::Error(ref err) => Some(err),
Error::Timer(ref err) => Some(err),
Expand Down
4 changes: 2 additions & 2 deletions src/proxy/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ impl<M: fmt::Display, S> fmt::Display for Error<M, S> {
}

impl<M: error::Error, S> error::Error for Error<M, S> {
fn cause(&self) -> Option<&error::Error> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::Stack(e) => e.cause(),
Error::Stack(e) => e.source(),
Error::Spawn(_) => None,
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/proxy/canonicalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ impl<M: fmt::Display, S: fmt::Display> fmt::Display for Error<M, S> {
}

impl<M: error::Error, S: error::Error> error::Error for Error<M, S> {
fn cause(&self) -> Option<&error::Error> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::Stack(e) => e.cause(),
Error::Service(e) => e.cause(),
Error::Stack(e) => e.source(),
Error::Service(e) => e.source(),
}
}
}
4 changes: 2 additions & 2 deletions src/proxy/http/glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl fmt::Display for Error {
}

impl StdError for Error {
fn cause(&self) -> Option<&StdError> {
self.0.cause()
fn source(&self) -> Option<&(StdError + 'static)> {
self.0.source()
}
}
6 changes: 3 additions & 3 deletions src/proxy/http/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ pub mod router {
}

impl<E: error::Error, M: error::Error> error::Error for Error<E, M> {
fn cause(&self) -> Option<&error::Error> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::Service(e) => e.cause(),
Error::Stack(e) => e.cause(),
Error::Service(e) => e.source(),
Error::Stack(e) => e.source(),
}
}
}
Expand Down

0 comments on commit 0fe8063

Please sign in to comment.