Refactor for rich errors, split user abort and genuine error

This commit is contained in:
Félix Saparelli 2022-05-31 20:51:32 +12:00
parent 3c38a2f0eb
commit c0eaffb05d
No known key found for this signature in database
GPG key ID: B948C4BAE44FC474
11 changed files with 340 additions and 138 deletions

View file

@ -6,14 +6,14 @@ use serde::Serialize;
use url::Url;
use super::Data;
use crate::{download, remote_exists, PkgFmt, Template};
use crate::{download, remote_exists, BinstallError, PkgFmt, Template};
pub struct GhCrateMeta {
data: Data,
}
impl GhCrateMeta {
fn url(&self) -> Result<Url, anyhow::Error> {
fn url(&self) -> Result<Url, BinstallError> {
let ctx = Context::from_data(&self.data);
debug!("Using context: {:?}", ctx);
Ok(ctx.render_url(&self.data.meta.pkg_url)?)
@ -26,13 +26,13 @@ impl super::Fetcher for GhCrateMeta {
Box::new(Self { data: data.clone() })
}
async fn check(&self) -> Result<bool, anyhow::Error> {
async fn check(&self) -> Result<bool, BinstallError> {
let url = self.url()?;
info!("Checking for package at: '{url}'");
remote_exists(url.as_str(), Method::HEAD).await
}
async fn fetch(&self, dst: &Path) -> Result<(), anyhow::Error> {
async fn fetch(&self, dst: &Path) -> Result<(), BinstallError> {
let url = self.url()?;
info!("Downloading package from: '{url}'");
download(url.as_str(), dst).await
@ -101,7 +101,7 @@ impl<'c> Context<'c> {
}
}
pub(self) fn render_url(&self, template: &str) -> Result<Url, anyhow::Error> {
pub(self) fn render_url(&self, template: &str) -> Result<Url, BinstallError> {
Ok(Url::parse(&self.render(template)?)?)
}
}

View file

@ -2,9 +2,10 @@ use std::path::Path;
use log::info;
use reqwest::Method;
use url::Url;
use super::Data;
use crate::{download, remote_exists, PkgFmt};
use crate::{download, remote_exists, BinstallError, PkgFmt};
const BASE_URL: &str = "https://github.com/alsuren/cargo-quickinstall/releases/download";
const STATS_URL: &str = "https://warehouse-clerk-tmp.vercel.app/api/crate";
@ -25,14 +26,14 @@ impl super::Fetcher for QuickInstall {
})
}
async fn check(&self) -> Result<bool, anyhow::Error> {
async fn check(&self) -> Result<bool, BinstallError> {
let url = self.package_url();
self.report().await?;
info!("Checking for package at: '{url}'");
remote_exists(&url, Method::HEAD).await
}
async fn fetch(&self, dst: &Path) -> Result<(), anyhow::Error> {
async fn fetch(&self, dst: &Path) -> Result<(), BinstallError> {
let url = self.package_url();
info!("Downloading package from: '{url}'");
download(&url, &dst).await
@ -68,14 +69,20 @@ impl QuickInstall {
)
}
pub async fn report(&self) -> Result<(), anyhow::Error> {
pub async fn report(&self) -> Result<(), BinstallError> {
info!("Sending installation report to quickinstall (anonymous)");
let url = Url::parse(&self.stats_url())?;
reqwest::Client::builder()
.user_agent(USER_AGENT)
.build()?
.request(Method::HEAD, &self.stats_url())
.request(Method::HEAD, url.clone())
.send()
.await?;
.await
.map_err(|err| BinstallError::Http {
method: Method::HEAD,
url,
err,
})?;
Ok(())
}
}