Skip to content

Commit

Permalink
better errors
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Perkowski <[email protected]>
  • Loading branch information
adamperkowski committed Nov 24, 2024
1 parent 2f6d745 commit f66f32c
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/api/aur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn get_latest(args: api::ApiArgs) -> api::ReleaseFuture {
let client = args.request_client;

let result = client.get(url).headers(api::setup_headers()).send().await?;
api::match_statuscode(&result)?;
api::match_statuscode(&result, args.package.clone())?;

let json: AURResponse = result.json().await?;

Expand All @@ -30,7 +30,7 @@ pub fn get_latest(args: api::ApiArgs) -> api::ReleaseFuture {
url: String::new(),
})
} else {
Err(error::Error::NoVersion)
Err(error::Error::NoVersion(args.package))
}
})
}
2 changes: 1 addition & 1 deletion src/api/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn get_latest(args: api::ApiArgs) -> api::ReleaseFuture {
let client = args.request_client;

let result = client.get(url).headers(headers).send().await?;
api::match_statuscode(&result)?;
api::match_statuscode(&result, args.package)?;

let json: GitHubResponse = result.json().await?;

Expand Down
2 changes: 1 addition & 1 deletion src/api/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn get_latest(args: api::ApiArgs) -> api::ReleaseFuture {
let client = args.request_client;

let result = client.get(url).headers(headers).send().await?;
api::match_statuscode(&result)?;
api::match_statuscode(&result, args.package)?;

let json: GitLabResponse = result.json().await?;

Expand Down
7 changes: 3 additions & 4 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,16 @@ fn setup_headers() -> reqwest::header::HeaderMap {
}

#[cfg(feature = "http")]
fn match_statuscode(req: &reqwest::Response) -> crate::error::Result<()> {
fn match_statuscode(req: &reqwest::Response, package: String) -> crate::error::Result<()> {
use crate::error;
use reqwest::StatusCode;

let status = req.status();

match status {
StatusCode::OK => Ok(()),
StatusCode::FORBIDDEN => Err(error::Error::RequestForbidden),
StatusCode::NOT_FOUND => Err(error::Error::RequestNotFound),
_ => Err(error::Error::RequestNotOK),
StatusCode::FORBIDDEN => Err(error::Error::RequestForbidden(package)),
_ => Err(error::Error::RequestNotOK(package, status.to_string())),
}
}

Expand Down
24 changes: 10 additions & 14 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! [thiserror] implementation
use colored::Colorize;
use thiserror::Error as ThisError;

const RATE_LIMIT: &str = "we might be getting rate-limited here";
Expand Down Expand Up @@ -29,17 +28,14 @@ pub enum Error {
TOMLError(#[from] toml::de::Error),

// custom errors
#[error("request didn't return 200")]
RequestNotOK,
#[error("{0}: request status != OK: {1}")]
RequestNotOK(String, String),

#[error("request returned 430\n{RATE_LIMIT}")]
RequestForbidden,
#[error("{0}: request returned 430\n{RATE_LIMIT}")]
RequestForbidden(String),

#[error("request returned 404 not found")]
RequestNotFound,

#[error("version not found")]
NoVersion,
#[error("{0}: version not found")]
NoVersion(String),

#[error("specified config file not found")]
NoConfigSpecified,
Expand All @@ -56,11 +52,11 @@ pub enum Error {
#[error("unsupported verfile version\nplease update your verfiles")]
VerfileVer,

#[error("package not in newver")]
PkgNotInNewver,
#[error("{0}: package not in newver")]
PkgNotInNewver(String),

#[error("source not found")]
SourceNotFound,
#[error("source {0} not found")]
SourceNotFound(String),
}

pub type Result<T> = std::result::Result<T, Error>;
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ pub async fn run_source(

Ok((api.func)(args).await?)
} else {
Err(error::Error::SourceNotFound)
Err(error::Error::SourceNotFound(source))
}
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async fn take(core: Core, take_names: Option<Vec<String>>) -> error::Result<()>
oldver.data.data.insert(package_name, new_pkg.1.clone());
}
} else {
return Err(error::Error::PkgNotInNewver);
return Err(error::Error::PkgNotInNewver(package_name));
}
}

Expand Down

0 comments on commit f66f32c

Please sign in to comment.