Skip to content

Refactor how we convert from T: Error to dyn CargoError #1516

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

Merged
merged 2 commits into from
Oct 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 8 additions & 43 deletions src/util/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,6 @@ impl CargoError for Box<dyn CargoError> {
(**self).response()
}
}
impl<T: CargoError> CargoError for Box<T> {
fn description(&self) -> &str {
(**self).description()
}
fn cause(&self) -> Option<&dyn CargoError> {
(**self).cause()
}
fn human(&self) -> bool {
(**self).human()
}
fn response(&self) -> Option<Response> {
(**self).response()
}
}

pub type CargoResult<T> = Result<T, Box<dyn CargoError>>;

Expand Down Expand Up @@ -163,42 +149,21 @@ impl<E: CargoError> fmt::Display for ChainedError<E> {
// =============================================================================
// Error impls

impl<E: Any + Error + Send + 'static> From<E> for Box<dyn CargoError> {
fn from(err: E) -> Box<dyn CargoError> {
if let Some(err) = Any::downcast_ref::<DieselError>(&err) {
if let DieselError::NotFound = *err {
return Box::new(NotFound);
}
}

#[derive(Debug)]
struct Shim<E>(E);
impl<E: Error + Send + 'static> CargoError for Shim<E> {
fn description(&self) -> &str {
Error::description(&self.0)
}
}
impl<E: fmt::Display> fmt::Display for Shim<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
Box::new(Shim(err))
}
}

impl CargoError for ::serde_json::Error {
impl<E: Error + Send + 'static> CargoError for E {
fn description(&self) -> &str {
Error::description(self)
}
}

impl CargoError for ::std::io::Error {
fn description(&self) -> &str {
Error::description(self)
impl<E: Any + Error + Send + 'static> From<E> for Box<dyn CargoError> {
fn from(err: E) -> Box<dyn CargoError> {
if let Some(DieselError::NotFound) = Any::downcast_ref::<DieselError>(&err) {
Box::new(NotFound)
} else {
Box::new(err)
}
}
}

// =============================================================================
// Concrete errors

Expand Down