-
Notifications
You must be signed in to change notification settings - Fork 34
made the Error type use thiserror #80
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
Open
kingananas20
wants to merge
2
commits into
theduke:main
Choose a base branch
from
kingananas20:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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,119 +1,25 @@ | ||
//! Error types. | ||
/// Errors returned by the api client. | ||
#[derive(Debug)] | ||
#[derive(Debug, thiserror::Error)] | ||
#[non_exhaustive] | ||
pub enum Error { | ||
/// Low-level http error. | ||
Http(reqwest::Error), | ||
/// Invalid URL. | ||
Url(url::ParseError), | ||
/// Crate could not be found. | ||
NotFound(NotFoundError), | ||
/// Low level http error | ||
#[error("Low level http error: {0}")] | ||
Http(#[from] reqwest::Error), | ||
/// Invalid url | ||
#[error("Invalid url: {0}")] | ||
Url(#[from] url::ParseError), | ||
/// Crate couldn't be found | ||
#[error("Resource at {0} couldn't be found.")] | ||
NotFound(String), | ||
/// No permission to access the resource. | ||
PermissionDenied(PermissionDeniedError), | ||
#[error("No permission to access the resource: {0}")] | ||
PermissionDenied(String), | ||
/// JSON decoding of API response failed. | ||
JsonDecode(JsonDecodeError), | ||
#[error("JSON decoding of API response failed: {0}")] | ||
JsonDecode(String), | ||
/// Error returned by the crates.io API directly. | ||
Api(crate::types::ApiErrors), | ||
} | ||
|
||
impl std::fmt::Display for Error { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Error::Http(e) => e.fmt(f), | ||
Error::Url(e) => e.fmt(f), | ||
Error::NotFound(e) => e.fmt(f), | ||
Error::PermissionDenied(e) => e.fmt(f), | ||
Error::Api(err) => { | ||
let inner = if err.errors.is_empty() { | ||
"Unknown API error".to_string() | ||
} else { | ||
err.errors | ||
.iter() | ||
.map(|err| err.to_string()) | ||
.collect::<Vec<_>>() | ||
.join(", ") | ||
}; | ||
|
||
write!(f, "API Error ({})", inner) | ||
} | ||
Error::JsonDecode(err) => write!(f, "Could not decode API JSON response: {err}"), | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for Error { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
match self { | ||
Error::Http(e) => Some(e), | ||
Error::Url(e) => Some(e), | ||
Error::NotFound(_) => None, | ||
Error::PermissionDenied(_) => None, | ||
Error::Api(_) => None, | ||
Error::JsonDecode(err) => Some(err), | ||
} | ||
} | ||
|
||
// TODO: uncomment once backtrace feature is stabilized (https://github.com/rust-lang/rust/issues/53487). | ||
/* | ||
fn backtrace(&self) -> Option<&std::backtrace::Backtrace> { | ||
match self { | ||
Self::Http(e) => e.backtrace(), | ||
Self::Url(e) => e.backtrace(), | ||
Self::InvalidHeader(e) => e.backtrace(), | ||
Self::NotFound(_) => None, | ||
} | ||
} | ||
*/ | ||
} | ||
|
||
impl From<reqwest::Error> for Error { | ||
fn from(e: reqwest::Error) -> Self { | ||
Error::Http(e) | ||
} | ||
} | ||
|
||
impl From<url::ParseError> for Error { | ||
fn from(e: url::ParseError) -> Self { | ||
Error::Url(e) | ||
} | ||
} | ||
|
||
/// Error returned when the JSON returned by the API could not be decoded. | ||
#[derive(Debug)] | ||
pub struct JsonDecodeError { | ||
pub(crate) message: String, | ||
} | ||
|
||
impl std::fmt::Display for JsonDecodeError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "Could not decode JSON: {}", self.message) | ||
} | ||
} | ||
|
||
impl std::error::Error for JsonDecodeError {} | ||
|
||
/// Error returned when a resource could not be found. | ||
#[derive(Debug)] | ||
pub struct NotFoundError { | ||
pub(crate) url: String, | ||
} | ||
|
||
impl std::fmt::Display for NotFoundError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "Resource at url '{}' could not be found", self.url) | ||
} | ||
} | ||
|
||
/// Error returned when a resource is not accessible. | ||
#[derive(Debug)] | ||
pub struct PermissionDeniedError { | ||
pub(crate) reason: String, | ||
} | ||
|
||
impl std::fmt::Display for PermissionDeniedError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "Permission denied: {}", self.reason) | ||
} | ||
#[error("Error returned by the crates.io API directly: {0:?}")] | ||
Api(#[from] crate::types::ApiErrors), | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
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.
removed everything below because
thiserror
automatically implements them