Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
Merge branch 'failure'
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Nov 17, 2019
2 parents 89579fb + a45d449 commit 0a7b3b6
Show file tree
Hide file tree
Showing 20 changed files with 88 additions and 128 deletions.
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@ reqwest = "0.9"
serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0"
serde_urlencoded = "0.6"

urlencoding = "1.0.0"

failure = "0.1"
regex = "1.1"

regex = "1.3"
log = "0.4"
thiserror = "1.0"

[build-dependencies]
skeptic = "0.13"
Expand Down
5 changes: 2 additions & 3 deletions src/action/maven.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Types related to maven

use failure::Error;
use serde::Deserialize;

use crate::client;
use crate::client::{self, Result};
use crate::client_internals::path::Path;
use crate::Jenkins;

Expand Down Expand Up @@ -42,7 +41,7 @@ impl ShortMavenArtifactRecord {
pub fn get_full_artifact_record(
&self,
jenkins_client: &Jenkins,
) -> Result<MavenArtifactRecord, Error> {
) -> Result<MavenArtifactRecord> {
let path = jenkins_client.url_to_path(&self.url);
if let Path::MavenArtifactRecord { .. } = path {
Ok(jenkins_client.get(&path)?.json()?)
Expand Down
9 changes: 4 additions & 5 deletions src/build/common.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::marker::PhantomData;

use failure::Error;
use serde::{self, Deserialize, Serialize};
use serde_json;

use crate::helpers::Class;

use crate::action::CommonAction;
use crate::client;
use crate::client::{self, Result};
use crate::client_internals::path::Path;
use crate::job::{CommonJob, Job};
use crate::Jenkins;
Expand Down Expand Up @@ -36,7 +35,7 @@ where
for<'de> T: Deserialize<'de>,
{
/// Get the full details of a `Build` matching the `ShortBuild`
pub fn get_full_build(&self, jenkins_client: &Jenkins) -> Result<T, Error> {
pub fn get_full_build(&self, jenkins_client: &Jenkins) -> Result<T> {
let path = jenkins_client.url_to_path(&self.url);
if let Path::Build { .. } = path {
Ok(jenkins_client.get(&path)?.json()?)
Expand Down Expand Up @@ -165,7 +164,7 @@ pub trait Build {
fn url(&self) -> &str;

/// Get the `Job` from a `Build`
fn get_job(&self, jenkins_client: &Jenkins) -> Result<Self::ParentJob, Error>
fn get_job(&self, jenkins_client: &Jenkins) -> Result<Self::ParentJob>
where
for<'de> Self::ParentJob: Deserialize<'de>,
{
Expand All @@ -192,7 +191,7 @@ pub trait Build {
}

/// Get the console output from a `Build`
fn get_console(&self, jenkins_client: &Jenkins) -> Result<String, Error> {
fn get_console(&self, jenkins_client: &Jenkins) -> Result<String> {
let path = jenkins_client.url_to_path(&self.url());
if let Path::Build {
job_name,
Expand Down
5 changes: 2 additions & 3 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Jenkins Builds

use failure::Error;

use crate::client::Result;
use crate::client_internals::path::{Name, Path};
use crate::job::JobName;
use crate::Jenkins;
Expand All @@ -24,7 +23,7 @@ pub use self::multijob::MultiJobBuild;

impl Jenkins {
/// Get a build from a `job_name` and `build_number`
pub fn get_build<'a, J, B>(&self, job_name: J, build_number: B) -> Result<CommonBuild, Error>
pub fn get_build<'a, J, B>(&self, job_name: J, build_number: B) -> Result<CommonBuild>
where
J: Into<JobName<'a>>,
B: Into<BuildNumber>,
Expand Down
8 changes: 3 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//! Helpers to build advanced queries

use failure::Error as FailureError;
use serde::{self, Deserialize};

use crate::client_internals::path::{Name, Path as PrivatePath};
use crate::client_internals::InternalAdvancedQueryParams;

// pub use client_internals::path::Name;
pub use crate::client_internals::AdvancedQuery;
pub use crate::client_internals::{error, Error};
pub use crate::client_internals::{error, Error, Result};
pub use crate::client_internals::{TreeBuilder, TreeQueryParam};

use crate::build;
Expand Down Expand Up @@ -118,7 +117,6 @@ impl super::Jenkins {
/// # Example
///
/// ```rust
/// # extern crate failure;
/// # #[macro_use]
/// # extern crate serde;
/// #
Expand All @@ -140,7 +138,7 @@ impl super::Jenkins {
/// last_build: LastBuild,
/// }
///
/// # fn main() -> Result<(), failure::Error> {
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let jenkins = JenkinsBuilder::new("http://localhost:8080")
/// # .with_user("user", Some("password"))
/// # .build()?;
Expand All @@ -163,7 +161,7 @@ impl super::Jenkins {
/// # }
/// ```
///
pub fn get_object_as<Q, T>(&self, object: Path, parameters: Q) -> Result<T, FailureError>
pub fn get_object_as<Q, T>(&self, object: Path, parameters: Q) -> Result<T>
where
Q: Into<Option<AdvancedQuery>>,
for<'de> T: Deserialize<'de>,
Expand Down
4 changes: 2 additions & 2 deletions src/client_internals/builder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::str::FromStr;

use failure::Error;
use reqwest::{self, Client, Url};

use super::{Jenkins, User};
use crate::client::Result;

/// Builder for Jenkins client
///
Expand Down Expand Up @@ -45,7 +45,7 @@ impl JenkinsBuilder {
}

/// Build the Jenkins client
pub fn build(self) -> Result<Jenkins, Error> {
pub fn build(self) -> Result<Jenkins> {
let url = Url::from_str(&self.url)?;
if url.cannot_be_a_base() {
return Err(reqwest::UrlError::RelativeUrlWithoutBase.into());
Expand Down
7 changes: 3 additions & 4 deletions src/client_internals/csrf.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use failure;

use reqwest::{header::HeaderName, header::HeaderValue, RequestBuilder};
use serde::Deserialize;

use super::{path::Path, Jenkins};
use crate::client::Result;

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
Expand All @@ -16,7 +15,7 @@ impl Jenkins {
pub(crate) fn add_csrf_to_request(
&self,
request_builder: RequestBuilder,
) -> Result<RequestBuilder, failure::Error> {
) -> Result<RequestBuilder> {
if self.csrf_enabled {
let crumb = self.get_csrf()?;
Ok(request_builder.header(
Expand All @@ -28,7 +27,7 @@ impl Jenkins {
}
}

pub(crate) fn get_csrf(&self) -> Result<Crumb, failure::Error> {
pub(crate) fn get_csrf(&self) -> Result<Crumb> {
let crumb: Crumb = self.get(&Path::CrumbIssuer)?.json()?;
Ok(crumb)
}
Expand Down
38 changes: 18 additions & 20 deletions src/client_internals/errors.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
use std::fmt;

use failure::Fail;
use thiserror::Error;

/// Wrapper `Result` type
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

/// Errors that can be thrown
#[derive(Debug, Fail)]
#[derive(Debug, Error)]
pub enum Error {
/// Error thrown when a link between objects has an unexpected format
#[fail(display = "invalid url for {}: {}", expected, url)]
#[error("invalid url for {expected}: {url}")]
/// Error thrown when a link between objects has an unexpected format
InvalidUrl {
/// URL found
url: String,
/// Expected URL type
expected: ExpectedType,
},

/// Error thrown when CSRF protection use an unexpected field name
#[fail(
display = "invalid crumbfield '{}', expected 'Jenkins-Crumb'",
field_name
)]
#[error("invalid crumbfield '{field_name}', expected 'Jenkins-Crumb'")]
/// Error thrown when CSRF protection use an unexpected field name
InvalidCrumbFieldName {
/// Field name provided by Jenkins api for crumb
field_name: String,
},

/// Error thrown when building a parameterized job with an invalid parameter
#[fail(display = "illegal argument: '{}'", message)]
#[error("illegal argument: '{message}'")]
/// Error thrown when building a parameterized job with an invalid parameter
IllegalArgument {
/// Exception message provided by Jenkins
message: String,
},
/// Error thrown when building a job with invalid parameters
#[fail(display = "illegal state: '{}'", message)]

#[error("illegal state: '{message}'")]
/// Error thrown when building a job with invalid parameters
IllegalState {
/// Exception message provided by Jenkins
message: String,
},

/// Error when trying to remotely build a job with parameters
#[fail(display = "can't build a job remotely with parameters")]
#[error("can't build a job remotely with parameters")]
/// Error when trying to remotely build a job with parameters
UnsupportedBuildConfiguration,

/// Error when trying to do an action on an object not supporting it
#[fail(
display = "can't do '{}' on a {} of type {}",
action, object_type, variant_name
)]
#[error("can't do '{action}' on a {object_type} of type {variant_name}")]
/// Error when trying to do an action on an object not supporting it
InvalidObjectType {
/// Object type
object_type: ExpectedType,
Expand Down
19 changes: 7 additions & 12 deletions src/client_internals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::fmt::Debug;
use std::string::ToString;

use failure;
use log::{debug, warn};
use regex::Regex;
use reqwest::{
Expand All @@ -12,7 +11,7 @@ use reqwest::{
use serde::Serialize;

mod errors;
pub use self::errors::Error;
pub use self::errors::{Error, Result};
mod builder;
pub mod path;
pub use self::builder::JenkinsBuilder;
Expand Down Expand Up @@ -84,7 +83,7 @@ impl Jenkins {
format!("{}{}", self.url, endpoint)
}

fn send(&self, mut request_builder: RequestBuilder) -> Result<Response, failure::Error> {
fn send(&self, mut request_builder: RequestBuilder) -> Result<Response> {
if let Some(ref user) = self.user {
request_builder =
request_builder.basic_auth(user.username.clone(), user.password.clone());
Expand All @@ -94,31 +93,27 @@ impl Jenkins {
Ok(self.client.execute(query)?)
}

fn error_for_status(response: Response) -> Result<Response, failure::Error> {
fn error_for_status(response: Response) -> Result<Response> {
let status = response.status();
if status.is_client_error() || status.is_server_error() {
warn!("got an error: {}", status);
}
Ok(response.error_for_status()?)
}

pub(crate) fn get(&self, path: &Path) -> Result<Response, failure::Error> {
pub(crate) fn get(&self, path: &Path) -> Result<Response> {
self.get_with_params(path, &[("depth", &self.depth.to_string())])
}

pub(crate) fn get_with_params<T: Serialize>(
&self,
path: &Path,
qps: T,
) -> Result<Response, failure::Error> {
pub(crate) fn get_with_params<T: Serialize>(&self, path: &Path, qps: T) -> Result<Response> {
let query = self
.client
.get(&self.url_api_json(&path.to_string()))
.query(&qps);
Ok(Self::error_for_status(self.send(query)?)?)
}

pub(crate) fn post(&self, path: &Path) -> Result<Response, failure::Error> {
pub(crate) fn post(&self, path: &Path) -> Result<Response> {
let mut request_builder = self.client.post(&self.url(&path.to_string()));

request_builder = self.add_csrf_to_request(request_builder)?;
Expand All @@ -131,7 +126,7 @@ impl Jenkins {
path: &Path,
body: T,
qps: &[(&str, &str)],
) -> Result<Response, failure::Error> {
) -> Result<Response> {
let mut request_builder = self.client.post(&self.url(&path.to_string()));

request_builder = self.add_csrf_to_request(request_builder)?;
Expand Down
3 changes: 2 additions & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ macro_rules! specialize {
($common:ty => $trait:path) => {
impl $common {
#[doc = "Read the object as one of it's specialization implementing $trait"]
pub fn as_variant<T: Class + $trait>(&self) -> Result<T, serde_json::Error>
#[allow(unused_qualifications)]
pub fn as_variant<T: Class + $trait>(&self) -> std::result::Result<T, serde_json::Error>
where
for<'de> T: Deserialize<'de>,
{
Expand Down
5 changes: 2 additions & 3 deletions src/home.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Jenkins Home, describing state of the master

use failure::Error;
use serde::Deserialize;

use crate::client_internals::Path;
use crate::client_internals::{Path, Result};
use crate::job::ShortJob;
use crate::view::ShortView;
use crate::Jenkins;
Expand Down Expand Up @@ -48,7 +47,7 @@ pub struct Home {

impl Jenkins {
/// Get Jenkins `Home`
pub fn get_home(&self) -> Result<Home, Error> {
pub fn get_home(&self) -> Result<Home> {
Ok(self.get(&Path::Home)?.json()?)
}
}

0 comments on commit 0a7b3b6

Please sign in to comment.