Skip to content

refactor(error): remove redundant parts of error names #463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ use header::{ContentLength, Location};
use method::Method;
use net::{NetworkConnector, NetworkStream, HttpConnector, ContextVerifier};
use status::StatusClass::Redirection;
use {Url, HttpResult};
use HttpError::HttpUriError;
use {Url};
use Error;

pub use self::pool::Pool;
pub use self::request::Request;
Expand Down Expand Up @@ -203,7 +203,7 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
}

/// Execute this request and receive a Response back.
pub fn send(self) -> HttpResult<Response> {
pub fn send(self) -> ::Result<Response> {
let RequestBuilder { client, method, url, headers, body } = self;
let mut url = try!(url.into_url());
trace!("send {:?} {:?}", method, url);
Expand Down Expand Up @@ -382,15 +382,15 @@ impl Default for RedirectPolicy {
}
}

fn get_host_and_port(url: &Url) -> HttpResult<(String, u16)> {
fn get_host_and_port(url: &Url) -> ::Result<(String, u16)> {
let host = match url.serialize_host() {
Some(host) => host,
None => return Err(HttpUriError(UrlError::EmptyHost))
None => return Err(Error::Uri(UrlError::EmptyHost))
};
trace!("host={:?}", host);
let port = match url.port_or_default() {
Some(port) => port,
None => return Err(HttpUriError(UrlError::InvalidPort))
None => return Err(Error::Uri(UrlError::InvalidPort))
};
trace!("port={:?}", port);
Ok((host, port))
Expand Down
9 changes: 4 additions & 5 deletions src/client/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use net::{NetworkStream, NetworkConnector, HttpConnector, Fresh, Streaming};
use http::{self, HttpWriter, LINE_ENDING};
use http::HttpWriter::{ThroughWriter, ChunkedWriter, SizedWriter, EmptyWriter};
use version;
use HttpResult;
use client::{Response, get_host_and_port};


Expand Down Expand Up @@ -43,14 +42,14 @@ impl<W> Request<W> {

impl Request<Fresh> {
/// Create a new client request.
pub fn new(method: method::Method, url: Url) -> HttpResult<Request<Fresh>> {
pub fn new(method: method::Method, url: Url) -> ::Result<Request<Fresh>> {
let mut conn = HttpConnector(None);
Request::with_connector(method, url, &mut conn)
}

/// Create a new client request with a specific underlying NetworkStream.
pub fn with_connector<C, S>(method: method::Method, url: Url, connector: &mut C)
-> HttpResult<Request<Fresh>> where
-> ::Result<Request<Fresh>> where
C: NetworkConnector<Stream=S>,
S: Into<Box<NetworkStream + Send>> {
let (host, port) = try!(get_host_and_port(&url));
Expand All @@ -76,7 +75,7 @@ impl Request<Fresh> {

/// Consume a Fresh Request, writing the headers and method,
/// returning a Streaming Request.
pub fn start(mut self) -> HttpResult<Request<Streaming>> {
pub fn start(mut self) -> ::Result<Request<Streaming>> {
let mut uri = self.url.serialize_path().unwrap();
//TODO: this needs a test
if let Some(ref q) = self.url.query {
Expand Down Expand Up @@ -154,7 +153,7 @@ impl Request<Streaming> {
/// Completes writing the request, and returns a response to read from.
///
/// Consumes the Request.
pub fn send(self) -> HttpResult<Response> {
pub fn send(self) -> ::Result<Response> {
let mut raw = try!(self.body.end()).into_inner().unwrap(); // end() already flushes
if !http::should_keep_alive(self.version, &self.headers) {
try!(raw.close(Shutdown::Write));
Expand Down
3 changes: 1 addition & 2 deletions src/client/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use http::{self, HttpReader, RawStatus};
use http::HttpReader::{SizedReader, ChunkedReader, EofReader};
use status;
use version;
use HttpResult;

/// A response for a client request to a remote server.
#[derive(Debug)]
Expand All @@ -32,7 +31,7 @@ pub struct Response<S = HttpStream> {
impl Response {

/// Creates a new response from a server.
pub fn new(stream: Box<NetworkStream + Send>) -> HttpResult<Response> {
pub fn new(stream: Box<NetworkStream + Send>) -> ::Result<Response> {
let mut stream = BufReader::new(stream);

let head = try!(http::parse_response(&mut stream));
Expand Down
84 changes: 42 additions & 42 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
//! HttpError and HttpResult module.
use std::error::Error;
//! Error and Result module.
use std::error::Error as StdError;
use std::fmt;
use std::io::Error as IoError;

use httparse;
use url;

use self::HttpError::{HttpMethodError, HttpUriError, HttpVersionError,
HttpHeaderError, HttpStatusError, HttpIoError,
HttpTooLargeError};
use self::Error::{Method, Uri, Version,
Header, Status, Io,
TooLarge};


/// Result type often returned from methods that can have `HttpError`s.
pub type HttpResult<T> = Result<T, HttpError>;
/// Result type often returned from methods that can have hyper `Error`s.
pub type Result<T> = ::std::result::Result<T, Error>;

/// A set of errors that can occur parsing HTTP streams.
#[derive(Debug)]
pub enum HttpError {
pub enum Error {
/// An invalid `Method`, such as `GE,T`.
HttpMethodError,
Method,
/// An invalid `RequestUri`, such as `exam ple.domain`.
HttpUriError(url::ParseError),
Uri(url::ParseError),
/// An invalid `HttpVersion`, such as `HTP/1.1`
HttpVersionError,
Version,
/// An invalid `Header`.
HttpHeaderError,
Header,
/// A message head is too large to be reasonable.
HttpTooLargeError,
TooLarge,
/// An invalid `Status`, such as `1337 ELITE`.
HttpStatusError,
Status,
/// An `IoError` that occured while trying to read or write to a network stream.
HttpIoError(IoError),
Io(IoError),
}

impl fmt::Display for HttpError {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.description())
}
}

impl Error for HttpError {
impl StdError for Error {
fn description(&self) -> &str {
match *self {
HttpMethodError => "Invalid Method specified",
HttpUriError(_) => "Invalid Request URI specified",
HttpVersionError => "Invalid HTTP version specified",
HttpHeaderError => "Invalid Header provided",
HttpTooLargeError => "Message head is too large",
HttpStatusError => "Invalid Status provided",
HttpIoError(_) => "An IoError occurred while connecting to the specified network",
Method => "Invalid Method specified",
Uri(_) => "Invalid Request URI specified",
Version => "Invalid HTTP version specified",
Header => "Invalid Header provided",
TooLarge => "Message head is too large",
Status => "Invalid Status provided",
Io(_) => "An IoError occurred while connecting to the specified network",
}
}

fn cause(&self) -> Option<&Error> {
fn cause(&self) -> Option<&StdError> {
match *self {
HttpIoError(ref error) => Some(error),
HttpUriError(ref error) => Some(error),
Io(ref error) => Some(error),
Uri(ref error) => Some(error),
_ => None,
}
}
}

impl From<IoError> for HttpError {
fn from(err: IoError) -> HttpError {
HttpIoError(err)
impl From<IoError> for Error {
fn from(err: IoError) -> Error {
Io(err)
}
}

impl From<url::ParseError> for HttpError {
fn from(err: url::ParseError) -> HttpError {
HttpUriError(err)
impl From<url::ParseError> for Error {
fn from(err: url::ParseError) -> Error {
Uri(err)
}
}

impl From<httparse::Error> for HttpError {
fn from(err: httparse::Error) -> HttpError {
impl From<httparse::Error> for Error {
fn from(err: httparse::Error) -> Error {
match err {
httparse::Error::HeaderName => HttpHeaderError,
httparse::Error::HeaderValue => HttpHeaderError,
httparse::Error::NewLine => HttpHeaderError,
httparse::Error::Status => HttpStatusError,
httparse::Error::Token => HttpHeaderError,
httparse::Error::TooManyHeaders => HttpTooLargeError,
httparse::Error::Version => HttpVersionError,
httparse::Error::HeaderName => Header,
httparse::Error::HeaderValue => Header,
httparse::Error::NewLine => Header,
httparse::Error::Status => Status,
httparse::Error::Token => Header,
httparse::Error::TooManyHeaders => TooLarge,
httparse::Error::Version => Version,
}
}
}
3 changes: 1 addition & 2 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use typeable::Typeable;
use unicase::UniCase;

use self::internals::Item;
use error::HttpResult;

pub use self::shared::*;
pub use self::common::*;
Expand Down Expand Up @@ -113,7 +112,7 @@ impl Headers {
}

#[doc(hidden)]
pub fn from_raw<'a>(raw: &[httparse::Header<'a>]) -> HttpResult<Headers> {
pub fn from_raw<'a>(raw: &[httparse::Header<'a>]) -> ::Result<Headers> {
let mut headers = Headers::new();
for header in raw {
trace!("raw header: {:?}={:?}", header.name, &header.value[..]);
Expand Down
19 changes: 9 additions & 10 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ use method::Method;
use status::StatusCode;
use uri::RequestUri;
use version::HttpVersion::{self, Http10, Http11};
use HttpError::{HttpIoError, HttpTooLargeError};
use {HttpError, HttpResult};
use {Error};

use self::HttpReader::{SizedReader, ChunkedReader, EofReader, EmptyReader};
use self::HttpWriter::{ThroughWriter, ChunkedWriter, SizedWriter, EmptyWriter};
Expand Down Expand Up @@ -334,17 +333,17 @@ const MAX_HEADERS: usize = 100;

/// Parses a request into an Incoming message head.
#[inline]
pub fn parse_request<R: Read>(buf: &mut BufReader<R>) -> HttpResult<Incoming<(Method, RequestUri)>> {
pub fn parse_request<R: Read>(buf: &mut BufReader<R>) -> ::Result<Incoming<(Method, RequestUri)>> {
parse::<R, httparse::Request, (Method, RequestUri)>(buf)
}

/// Parses a response into an Incoming message head.
#[inline]
pub fn parse_response<R: Read>(buf: &mut BufReader<R>) -> HttpResult<Incoming<RawStatus>> {
pub fn parse_response<R: Read>(buf: &mut BufReader<R>) -> ::Result<Incoming<RawStatus>> {
parse::<R, httparse::Response, RawStatus>(buf)
}

fn parse<R: Read, T: TryParse<Subject=I>, I>(rdr: &mut BufReader<R>) -> HttpResult<Incoming<I>> {
fn parse<R: Read, T: TryParse<Subject=I>, I>(rdr: &mut BufReader<R>) -> ::Result<Incoming<I>> {
loop {
match try!(try_parse::<R, T, I>(rdr)) {
httparse::Status::Complete((inc, len)) => {
Expand All @@ -355,12 +354,12 @@ fn parse<R: Read, T: TryParse<Subject=I>, I>(rdr: &mut BufReader<R>) -> HttpResu
}
match try!(rdr.read_into_buf()) {
0 if rdr.get_buf().is_empty() => {
return Err(HttpIoError(io::Error::new(
return Err(Error::Io(io::Error::new(
io::ErrorKind::ConnectionAborted,
"Connection closed"
)))
},
0 => return Err(HttpTooLargeError),
0 => return Err(Error::TooLarge),
_ => ()
}
}
Expand All @@ -377,7 +376,7 @@ trait TryParse {
fn try_parse<'a>(headers: &'a mut [httparse::Header<'a>], buf: &'a [u8]) -> TryParseResult<Self::Subject>;
}

type TryParseResult<T> = Result<httparse::Status<(Incoming<T>, usize)>, HttpError>;
type TryParseResult<T> = Result<httparse::Status<(Incoming<T>, usize)>, Error>;

impl<'a> TryParse for httparse::Request<'a, 'a> {
type Subject = (Method, RequestUri);
Expand Down Expand Up @@ -552,12 +551,12 @@ mod tests {
#[test]
fn test_parse_tcp_closed() {
use std::io::ErrorKind;
use error::HttpError::HttpIoError;
use error::Error;

let mut empty = MockStream::new();
let mut buf = BufReader::new(&mut empty);
match parse_request(&mut buf) {
Err(HttpIoError(ref e)) if e.kind() == ErrorKind::ConnectionAborted => (),
Err(Error::Io(ref e)) if e.kind() == ErrorKind::ConnectionAborted => (),
other => panic!("unexpected result: {:?}", other)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ extern crate test;
pub use mimewrapper::mime;
pub use url::Url;
pub use client::Client;
pub use error::{HttpResult, HttpError};
pub use error::{Result, Error};
pub use method::Method::{Get, Head, Post, Delete};
pub use status::StatusCode::{Ok, BadRequest, NotFound};
pub use server::Server;
Expand Down
8 changes: 4 additions & 4 deletions src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt;
use std::str::FromStr;
use std::convert::AsRef;

use error::HttpError;
use error::Error;
use self::Method::{Options, Get, Post, Put, Delete, Head, Trace, Connect, Patch,
Extension};

Expand Down Expand Up @@ -87,10 +87,10 @@ impl Method {
}

impl FromStr for Method {
type Err = HttpError;
fn from_str(s: &str) -> Result<Method, HttpError> {
type Err = Error;
fn from_str(s: &str) -> Result<Method, Error> {
if s == "" {
Err(HttpError::HttpMethodError)
Err(Error::Method)
} else {
Ok(match s {
"OPTIONS" => Options,
Expand Down
Loading