-
Notifications
You must be signed in to change notification settings - Fork 39
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
Use thiserror for error handling #156
Merged
+276
−264
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,17 +1,68 @@ | ||
//! Error chains, types and traits. | ||
//! Defines root error type | ||
|
||
error_chain! { | ||
foreign_links { | ||
Base64Decode(base64::DecodeError); | ||
HeaderInvalid(http::header::InvalidHeaderValue); | ||
HeaderParse(http::header::ToStrError); | ||
Hyper(http::Error); | ||
Io(std::io::Error); | ||
Json(serde_json::Error); | ||
Regex(regex::Error); | ||
Reqwest(reqwest::Error); | ||
UriParse(http::uri::InvalidUri); | ||
Utf8Parse(std::string::FromUtf8Error); | ||
StrumParse(strum::ParseError); | ||
#[non_exhaustive] | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error { | ||
#[error("base64 decode error")] | ||
Base64Decode(#[from] base64::DecodeError), | ||
#[error("header parse error")] | ||
HeaderParse(#[from] http::header::ToStrError), | ||
#[error("json error")] | ||
Json(#[from] serde_json::Error), | ||
#[error("http transport error")] | ||
Reqwest(#[from] reqwest::Error), | ||
#[error("URI parse error")] | ||
Uri(#[from] url::ParseError), | ||
#[error("input is not UTF-8")] | ||
Ut8Parse(#[from] std::string::FromUtf8Error), | ||
#[error("strum error")] | ||
StrumParse(#[from] strum::ParseError), | ||
#[error("authentication information missing for index {0}")] | ||
AuthInfoMissing(String), | ||
#[error("unknown media type {0:?}")] | ||
UnknownMimeType(mime::Mime), | ||
#[error("unknown media type {0:?}")] | ||
UnsupportedMediaType(crate::mediatypes::MediaTypes), | ||
#[error("mime parse error")] | ||
MimeParse(#[from] mime::FromStrError), | ||
#[error("missing authentication header {0}")] | ||
MissingAuthHeader(&'static str), | ||
#[error("unexpected HTTP status {0}")] | ||
UnexpectedHttpStatus(http::StatusCode), | ||
#[error("invalid auth token '{0}'")] | ||
InvalidAuthToken(String), | ||
#[error("API V2 not supported")] | ||
V2NotSupported, | ||
#[error("obtained token is invalid")] | ||
LoginReturnedBadToken, | ||
#[error("www-authenticate header parse error")] | ||
Www(#[from] crate::v2::WwwHeaderParseError), | ||
#[error("request failed with status {status} and body of size {len}: {}", String::from_utf8_lossy(&body))] | ||
Client { | ||
status: http::StatusCode, | ||
len: usize, | ||
body: Vec<u8>, | ||
}, | ||
#[error("content digest error")] | ||
ContentDigestParse(#[from] crate::v2::ContentDigestError), | ||
#[error("no header Content-Type given and no workaround to apply")] | ||
MediaTypeSniff, | ||
#[error("manifest error")] | ||
Manifest(#[from] crate::v2::manifest::ManifestError), | ||
#[error("reference is invalid")] | ||
ReferenceParse(#[from] crate::reference::ReferenceParseError), | ||
#[error("requested operation requires that credentials are available")] | ||
NoCredentials | ||
} | ||
|
||
pub type Result<T> = std::result::Result<T, Error>; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
#[test] | ||
fn test_error_bounds() { | ||
fn check_bounds<T: Send + Sync + 'static>() {} | ||
check_bounds::<Error>(); | ||
} | ||
} |
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,6 +1,6 @@ | ||
//! Media-types for API objects. | ||
|
||
use crate::errors::{Error, Result}; | ||
use crate::errors::{Result}; | ||
use mime; | ||
use strum::EnumProperty; | ||
|
||
|
@@ -62,13 +62,13 @@ impl MediaTypes { | |
} | ||
("vnd.docker.image.rootfs.diff.tar.gzip", _) => Ok(MediaTypes::ImageLayerTgz), | ||
("vnd.docker.container.image.v1", "json") => Ok(MediaTypes::ContainerConfigV1), | ||
_ => bail!("unknown mediatype {:?}", mtype), | ||
_ => return Err(crate::Error::UnknownMimeType(mtype.clone())), | ||
} | ||
} | ||
_ => bail!("unknown mediatype {:?}", mtype), | ||
_ => return Err(crate::Error::UnknownMimeType(mtype.clone())), | ||
} | ||
} | ||
pub fn to_mime(&self) -> Result<mime::Mime> { | ||
pub fn to_mime(&self) -> mime::Mime { | ||
match self { | ||
&MediaTypes::ApplicationJson => Ok(mime::APPLICATION_JSON), | ||
ref m => { | ||
|
@@ -79,6 +79,6 @@ impl MediaTypes { | |
} | ||
} | ||
} | ||
.map_err(|e| Error::from(e.to_string())) | ||
.expect("to_mime should be always successful") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't follow why this cannot fail. I'd prefer to leave the |
||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please see the comment below