Skip to content

Commit

Permalink
feat: add get_project & improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kairyou committed Nov 5, 2024
1 parent 7dbfe0d commit b0be85f
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/jenkins/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,26 @@ impl JenkinsClient {
Ok(response)
}
Err(e) => {
let base_msg = format!("{:?}", e).replace("reqwest::Error ", "");
let error_msg = if let Some(source) = std::error::Error::source(&e) {
let source_msg = source
.source()
.map(|s| s.to_string())
.unwrap_or_else(|| source.to_string());
base_msg.replace(&format!("{:?}", source), &source_msg)
} else {
base_msg
};

// eprintln!("Error {:?}", e);
if e.is_connect() {
eprintln!("Connection error: {:?}", e);
eprintln!("Connection error: {}", error_msg);
} else if e.is_timeout() {
eprintln!("Request timed out: {:?}", e);
eprintln!("Request timed out: {}", error_msg);
} else if e.is_request() {
eprintln!("Request error: {:?}", e);
eprintln!("Request error: {}", error_msg);
} else {
eprintln!("Other error: {:?}", e);
eprintln!("Other error: {}", error_msg);
}
Err(anyhow::anyhow!(e))
}
Expand Down Expand Up @@ -580,6 +592,16 @@ impl JenkinsClient {
}
}
}
/// Get project info
pub async fn get_project(&self, job_url: &str) -> Result<JenkinsJob, Box<dyn std::error::Error>> {
let api_url = format_url(&format!("{}/api/json", job_url));
let headers = self.build_headers(None)?;
// println!("get_project: {}", url);
let result = self.client.get(&api_url).headers(headers).send().await;
let response = self.handle_response(result).await?;
let project: JenkinsJob = response.json().await?;
Ok(project)
}
}

/// Prevent newline when Enter key is pressed
Expand Down

0 comments on commit b0be85f

Please sign in to comment.