Skip to content

Commit

Permalink
Provide better error messages in certain situations
Browse files Browse the repository at this point in the history
The situations this commit covers is:
    Invalid architecture
    Invalid OS

As well as providing a simpler message for when the requested JDK
version isn't found. This message still isn't as simple as I'd like, as
it gets wrapped in 2 other errors, but it's a lot better than before.
  • Loading branch information
DenWav committed Sep 26, 2021
1 parent a0c1d29 commit 5500221
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 31 deletions.
64 changes: 37 additions & 27 deletions src/api/adoptium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,38 @@ pub struct AdoptiumApi {

impl AdoptiumApi {
fn get_jdk_url(&self, major: u8) -> JdkFetchResult<String> {
let arch = {
let env_arch = std::env::consts::ARCH;
match env_arch {
"x86" => Ok(env_arch),
"x86_64" => Ok("x64"),
_ => Err(JdkFetchError::Generic {
message: format!("Unknown ARCH {}", env_arch),
}),
}
}?;
let os = {
let env_os = std::env::consts::OS;
match env_os {
"linux" => Ok(env_os),
"macos" => Ok("mac"),
_ => Err(JdkFetchError::Generic {
message: format!("Unknown OS {}", env_os),
}),
}
}?;
let arch = get_arch_name()?;
let os = get_os_name()?;
Ok(format!(
"{}/binary/latest/{}/ga/{}/{}/jdk/hotspot/normal/{}?project=jdk",
&self.base_url, major, os, arch, &self.vendor
))
}

fn get_latest_jdk_version_url(&self, major: u8) -> String {
fn get_latest_jdk_version_url(&self, major: u8) -> JdkFetchResult<String> {
let os_name = get_os_name()?;
let arch_name = get_arch_name()?;

// grabs a 10 item page containing the most recent version of [major, major+1)
// EAs CAN get mixed in here, unfortunately
return format!(
return Ok(format!(
"{}/info/release_versions\
?page=0\
?architecture={}\
&OS={}\
&page=0\
&page_size=10\
&release_type=ga\
&sort_method=DEFAULT\
&sort_order=DESC\
&vendor={}\
&version=%5B{}%2C{}%29",
&self.base_url,
arch_name,
os_name,
&self.vendor,
major,
major + 1,
);
));
}
}

Expand All @@ -65,9 +54,8 @@ impl JdkFetchApi for AdoptiumApi {
}

fn get_latest_jdk_version(&self, major: u8) -> JdkFetchResult<Option<String>> {
let response = attohttpc::get(&self.get_latest_jdk_version_url(major))
.send()
.map_err(JdkFetchError::HttpIo)?;
let url = self.get_latest_jdk_version_url(major)?;
let response = attohttpc::get(&url).send().map_err(JdkFetchError::HttpIo)?;
if !response.is_success() {
if response.status().as_u16() == 404 {
return Ok(None);
Expand Down Expand Up @@ -114,3 +102,25 @@ struct JdkVersion {
enum Prerelease {
EA,
}

fn get_os_name() -> JdkFetchResult<&'static str> {
let env_os = std::env::consts::OS;
match env_os {
"linux" => Ok(env_os),
"macos" => Ok("mac"),
_ => Err(JdkFetchError::Incompatible {
message: format!("Unsupported OS: {}", env_os),
}),
}
}

fn get_arch_name() -> JdkFetchResult<&'static str> {
let env_arch = std::env::consts::ARCH;
match env_arch {
"x86" => Ok(env_arch),
"x86_64" => Ok("x64"),
_ => Err(JdkFetchError::Incompatible {
message: format!("Unsupported architecture: {}", env_arch),
}),
}
}
2 changes: 2 additions & 0 deletions src/api/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub enum JdkFetchError {
HttpIo(#[from] attohttpc::Error),
#[error("error from upstream: {message}")]
Upstream { message: String },
#[error("{message}")]
Incompatible { message: String },
#[error("unknown error: {message}")]
Generic { message: String },
}
Expand Down
12 changes: 8 additions & 4 deletions src/jdk_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub enum JdkManagerError {
Fetch {
message: String,
#[source]
source: JdkFetchError,
source: Option<JdkFetchError>,
},
#[error("unknown error: {message}")]
Generic { message: String },
Expand All @@ -54,7 +54,7 @@ impl JdkManagerError {
}
}

fn fetch<S: Into<String>>(message: S, error: JdkFetchError) -> JdkManagerError {
fn fetch<S: Into<String>>(message: S, error: Option<JdkFetchError>) -> JdkManagerError {
JdkManagerError::Fetch {
message: message.into(),
source: error,
Expand Down Expand Up @@ -221,11 +221,15 @@ impl<A: JdkFetchApi> JdkManager<A> {
let response = self
.api
.get_latest_jdk_binary(major)
.map_err(|e| JdkManagerError::fetch("Failed to get latest JDK binary", e))?;
.map_err(|e| JdkManagerError::fetch("Failed to get latest JDK binary", Some(e)))?;
// Custom simpler error message for most common failure
if response.status() == attohttpc::StatusCode::NOT_FOUND {
return Err(JdkManagerError::fetch("JDK version not found", None));
}
if !response.is_success() {
return Err(JdkManagerError::fetch(
"",
handle_response_fail(response, "Failed to get JDK binary"),
Some(handle_response_fail(response, "Failed to get JDK binary")),
));
}

Expand Down
29 changes: 29 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,36 @@ fn check_env_bound(jdk_manager: &JdkManager<AdoptiumApi>) -> Result<()> {
Ok(())
}

fn is_unsupported_system() -> bool {
let env_os = std::env::consts::OS;
match env_os {
"linux" | "macos" => {}
_ => {
eprintln!("{}", format!("jpre does not support OS: {}", env_os).red());
return true;
}
}

let env_arch = std::env::consts::ARCH;
match env_arch {
"x86" | "x86_64" => {}
_ => {
eprintln!(
"{}",
format!("jpre does not support architecture: {}", env_arch).red()
);
return true;
}
}

false
}

fn main() {
if is_unsupported_system() {
return;
}

let args: Jpre = Jpre::from_args();
if let Err(error) = main_for_result(args) {
eprintln!("{}", format!("Error: {:?}", error).red());
Expand Down

0 comments on commit 5500221

Please sign in to comment.