-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(transport): Improve
Error
type (#217)
- Loading branch information
1 parent
49ce265
commit ec1f37e
Showing
5 changed files
with
15 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,23 @@ | ||
use std::{error, fmt}; | ||
|
||
/// Error's that originate from the client or server; | ||
pub struct Error { | ||
kind: ErrorKind, | ||
source: Option<crate::Error>, | ||
} | ||
|
||
impl Error { | ||
pub(crate) fn from_source(kind: ErrorKind, source: crate::Error) -> Self { | ||
Self { | ||
kind, | ||
source: Some(source), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) enum ErrorKind { | ||
Client, | ||
Server, | ||
} | ||
pub struct Error(crate::Error); | ||
|
||
impl fmt::Debug for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let mut f = f.debug_tuple("Error"); | ||
f.field(&self.kind); | ||
if let Some(source) = &self.source { | ||
f.field(source); | ||
} | ||
f.finish() | ||
impl Error { | ||
pub(crate) fn from_source(source: impl Into<crate::Error>) -> Self { | ||
Self(source.into()) | ||
} | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
if let Some(source) = &self.source { | ||
write!(f, "{}: {}", self.kind, source) | ||
} else { | ||
write!(f, "{}", self.kind) | ||
} | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
impl error::Error for Error { | ||
fn source(&self) -> Option<&(dyn error::Error + 'static)> { | ||
self.source | ||
.as_ref() | ||
.map(|e| &**e as &(dyn error::Error + 'static)) | ||
} | ||
} | ||
|
||
impl fmt::Display for ErrorKind { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{:?}", self) | ||
self.0.source() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters