diff options
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | errors.rs | 0 | ||||
-rw-r--r-- | src/builder.rs | 3 | ||||
-rw-r--r-- | src/errors.rs | 37 | ||||
-rw-r--r-- | src/lib.rs | 6 | ||||
-rw-r--r-- | src/transport.rs | 37 |
6 files changed, 57 insertions, 29 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 877b717..afe0b96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,13 @@ # unreleased 0.2.0 +* improve (remove) mut in interfaces where it was no longer needed * gitignore Cargo.lock * update deps * refactor to use [hyperlocal](https://github.com/softprops/hyperlocal) for better unix domain socket transport support * Added ContainerListBuilder interface * Config Cmd and Env are now Option types * expose Config env as a map instead of a vec of strings -* export containers and image +* add support for export containers and image # 0.1.2 diff --git a/errors.rs b/errors.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/errors.rs diff --git a/src/builder.rs b/src/builder.rs index ccf89f9..4d2c247 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -4,12 +4,11 @@ extern crate rustc_serialize; extern crate jed; extern crate url; -use self::super::Docker; +use self::super::{Docker, Result}; use self::super::transport::Body; use self::super::rep::{ContainerCreateInfo, Event}; use self::super::rep::Container as ContainerRep; use std::collections::{BTreeMap, HashMap}; -use std::io::Result; use rustc_serialize::json::{self, Json, ToJson}; use url::form_urlencoded; diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..68568c8 --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,37 @@ +use std::io::Error as IoError; +use hyper::Error as HttpError; +use hyper::status::StatusCode; +use rustc_serialize::json::{DecoderError,EncoderError}; + +#[derive(Debug)] +pub enum Error { + Decoding(DecoderError), + Encoding(EncoderError), + Http(HttpError), + IO(IoError), + Fault { code: StatusCode, message: String } +} + +impl From<DecoderError> for Error { + fn from(error: DecoderError) -> Error { + Error::Decoding(error) + } +} + +impl From<EncoderError> for Error { + fn from(error: EncoderError) -> Error { + Error::Encoding(error) + } +} + +impl From<HttpError> for Error { + fn from(error: HttpError) -> Error { + Error::Http(error) + } +} + +impl From<IoError> for Error { + fn from(error: IoError) -> Error { + Error::IO(error) + } +} @@ -23,7 +23,9 @@ extern crate url; pub mod builder; pub mod rep; pub mod transport; +pub mod errors; +pub use errors::Error; use builder::{ContainerBuilder, ContainerListBuilder, EventsBuilder}; use hyper::{Client, Url}; use hyper::net::{HttpsConnector, Openssl}; @@ -36,13 +38,15 @@ use rep::{Change, ContainerDetails, Exit, History, ImageDetails, Info, SearchRes Status, Top, Version}; use rustc_serialize::json::{self, Json}; use std::env::{self, VarError}; -use std::io::{Read, Result}; +use std::io::Read; use std::iter::IntoIterator; use std::path::Path; use std::sync::Arc; use transport::{Body, Transport}; use url::{form_urlencoded, Host, RelativeSchemeData, SchemeData}; +pub type Result<T> = std::result::Result<T, Error>; + /// Entrypoint interface for communicating with docker daemon pub struct Docker { transport: Transport, diff --git a/src/transport.rs b/src/transport.rs index 329ff9b..e82f5c8 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -5,29 +5,16 @@ extern crate mime; use hyper::Client; use hyper::client; +use self::super::{Error, Result}; use self::hyper::buffer::BufReader; use self::hyper::header::ContentType; use self::hyper::status::StatusCode; use hyper::method::Method; -use self::mime::{Attr, Mime, Value}; -use self::mime::TopLevel::Application; -use self::mime::SubLevel::Json; use std::fmt; use std::ops::DerefMut; -use std::io::{Error, ErrorKind, Read, Result, Write}; +use std::io::{Read, Write}; use hyperlocal::DomainUrl; -fn lift_status_err(status: u16) -> Result<Box<Read>> { - match status { - 400 => Err(Error::new(ErrorKind::InvalidInput, "bad parameter")), - 404 => Err(Error::new(ErrorKind::InvalidInput, "not found")), - 406 => Err(Error::new(ErrorKind::InvalidInput, "not acceptable")), - 409 => Err(Error::new(ErrorKind::InvalidInput, "conflict found")), - 500 => Err(Error::new(ErrorKind::InvalidInput, "interal server error")), - _ => unreachable!(), - } -} - /// Transports are types which define the means of communication /// with the docker daemon pub enum Transport { @@ -63,7 +50,8 @@ impl Transport { Err(e) => panic!("failed request {:?}", e), }; let mut body = String::new(); - res.read_to_string(&mut body).map(|_| body) + try!(res.read_to_string(&mut body)); + Ok(body) } pub fn stream(&self, @@ -83,23 +71,22 @@ impl Transport { let embodied = match body { Some(Body { read: r, size: l }) => { let reader: &mut Read = *r.deref_mut(); - let content_type: Mime = Mime(Application, - Json, - vec![(Attr::Charset, Value::Utf8)]); - req.header(ContentType(content_type)).body(client::Body::SizedBody(reader, l)) + req.header(ContentType::json()).body(client::Body::SizedBody(reader, l)) } _ => req, }; - let res = match embodied.send() { - Ok(r) => r, - Err(e) => panic!("failed request {:?}", e), - }; + let res = try!(embodied.send()); match res.status { StatusCode::Ok | StatusCode::Created | StatusCode::SwitchingProtocols => { Ok(Box::new(res)) } StatusCode::NoContent => Ok(Box::new(BufReader::new("".as_bytes()))), - status => lift_status_err(status.to_u16()), + StatusCode::BadRequest => Err(Error::Fault { code: res.status, message: "bad parameter".to_owned() }), + StatusCode::NotFound => Err(Error::Fault { code: res.status, message: "not found".to_owned() }), + StatusCode::NotAcceptable => Err(Error::Fault { code: res.status, message: "not acceptable".to_owned() }), + StatusCode::Conflict => Err(Error::Fault { code: res.status, message: "conflict found".to_owned() }), + StatusCode::InternalServerError => Err(Error::Fault{ code: res.status, message: "internal server error".to_owned() }), + _ => unreachable!() } } } |