Skip to content

Commit 67340a5

Browse files
committed
Merge pull request #463 from hyperium/error-rename
refactor(error): remove redundant parts of error names
2 parents c29af72 + 9ba074d commit 67340a5

File tree

11 files changed

+81
-87
lines changed

11 files changed

+81
-87
lines changed

src/client/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ use header::{ContentLength, Location};
4343
use method::Method;
4444
use net::{NetworkConnector, NetworkStream, HttpConnector, ContextVerifier};
4545
use status::StatusClass::Redirection;
46-
use {Url, HttpResult};
47-
use HttpError::HttpUriError;
46+
use {Url};
47+
use Error;
4848

4949
pub use self::pool::Pool;
5050
pub use self::request::Request;
@@ -203,7 +203,7 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
203203
}
204204

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

385-
fn get_host_and_port(url: &Url) -> HttpResult<(String, u16)> {
385+
fn get_host_and_port(url: &Url) -> ::Result<(String, u16)> {
386386
let host = match url.serialize_host() {
387387
Some(host) => host,
388-
None => return Err(HttpUriError(UrlError::EmptyHost))
388+
None => return Err(Error::Uri(UrlError::EmptyHost))
389389
};
390390
trace!("host={:?}", host);
391391
let port = match url.port_or_default() {
392392
Some(port) => port,
393-
None => return Err(HttpUriError(UrlError::InvalidPort))
393+
None => return Err(Error::Uri(UrlError::InvalidPort))
394394
};
395395
trace!("port={:?}", port);
396396
Ok((host, port))

src/client/request.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use net::{NetworkStream, NetworkConnector, HttpConnector, Fresh, Streaming};
1212
use http::{self, HttpWriter, LINE_ENDING};
1313
use http::HttpWriter::{ThroughWriter, ChunkedWriter, SizedWriter, EmptyWriter};
1414
use version;
15-
use HttpResult;
1615
use client::{Response, get_host_and_port};
1716

1817

@@ -43,14 +42,14 @@ impl<W> Request<W> {
4342

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

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

7776
/// Consume a Fresh Request, writing the headers and method,
7877
/// returning a Streaming Request.
79-
pub fn start(mut self) -> HttpResult<Request<Streaming>> {
78+
pub fn start(mut self) -> ::Result<Request<Streaming>> {
8079
let mut uri = self.url.serialize_path().unwrap();
8180
//TODO: this needs a test
8281
if let Some(ref q) = self.url.query {
@@ -154,7 +153,7 @@ impl Request<Streaming> {
154153
/// Completes writing the request, and returns a response to read from.
155154
///
156155
/// Consumes the Request.
157-
pub fn send(self) -> HttpResult<Response> {
156+
pub fn send(self) -> ::Result<Response> {
158157
let mut raw = try!(self.body.end()).into_inner().unwrap(); // end() already flushes
159158
if !http::should_keep_alive(self.version, &self.headers) {
160159
try!(raw.close(Shutdown::Write));

src/client/response.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use http::{self, HttpReader, RawStatus};
1212
use http::HttpReader::{SizedReader, ChunkedReader, EofReader};
1313
use status;
1414
use version;
15-
use HttpResult;
1615

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

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

3837
let head = try!(http::parse_response(&mut stream));

src/error.rs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,88 @@
1-
//! HttpError and HttpResult module.
2-
use std::error::Error;
1+
//! Error and Result module.
2+
use std::error::Error as StdError;
33
use std::fmt;
44
use std::io::Error as IoError;
55

66
use httparse;
77
use url;
88

9-
use self::HttpError::{HttpMethodError, HttpUriError, HttpVersionError,
10-
HttpHeaderError, HttpStatusError, HttpIoError,
11-
HttpTooLargeError};
9+
use self::Error::{Method, Uri, Version,
10+
Header, Status, Io,
11+
TooLarge};
1212

1313

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

1717
/// A set of errors that can occur parsing HTTP streams.
1818
#[derive(Debug)]
19-
pub enum HttpError {
19+
pub enum Error {
2020
/// An invalid `Method`, such as `GE,T`.
21-
HttpMethodError,
21+
Method,
2222
/// An invalid `RequestUri`, such as `exam ple.domain`.
23-
HttpUriError(url::ParseError),
23+
Uri(url::ParseError),
2424
/// An invalid `HttpVersion`, such as `HTP/1.1`
25-
HttpVersionError,
25+
Version,
2626
/// An invalid `Header`.
27-
HttpHeaderError,
27+
Header,
2828
/// A message head is too large to be reasonable.
29-
HttpTooLargeError,
29+
TooLarge,
3030
/// An invalid `Status`, such as `1337 ELITE`.
31-
HttpStatusError,
31+
Status,
3232
/// An `IoError` that occured while trying to read or write to a network stream.
33-
HttpIoError(IoError),
33+
Io(IoError),
3434
}
3535

36-
impl fmt::Display for HttpError {
36+
impl fmt::Display for Error {
3737
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3838
f.write_str(self.description())
3939
}
4040
}
4141

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

55-
fn cause(&self) -> Option<&Error> {
55+
fn cause(&self) -> Option<&StdError> {
5656
match *self {
57-
HttpIoError(ref error) => Some(error),
58-
HttpUriError(ref error) => Some(error),
57+
Io(ref error) => Some(error),
58+
Uri(ref error) => Some(error),
5959
_ => None,
6060
}
6161
}
6262
}
6363

64-
impl From<IoError> for HttpError {
65-
fn from(err: IoError) -> HttpError {
66-
HttpIoError(err)
64+
impl From<IoError> for Error {
65+
fn from(err: IoError) -> Error {
66+
Io(err)
6767
}
6868
}
6969

70-
impl From<url::ParseError> for HttpError {
71-
fn from(err: url::ParseError) -> HttpError {
72-
HttpUriError(err)
70+
impl From<url::ParseError> for Error {
71+
fn from(err: url::ParseError) -> Error {
72+
Uri(err)
7373
}
7474
}
7575

76-
impl From<httparse::Error> for HttpError {
77-
fn from(err: httparse::Error) -> HttpError {
76+
impl From<httparse::Error> for Error {
77+
fn from(err: httparse::Error) -> Error {
7878
match err {
79-
httparse::Error::HeaderName => HttpHeaderError,
80-
httparse::Error::HeaderValue => HttpHeaderError,
81-
httparse::Error::NewLine => HttpHeaderError,
82-
httparse::Error::Status => HttpStatusError,
83-
httparse::Error::Token => HttpHeaderError,
84-
httparse::Error::TooManyHeaders => HttpTooLargeError,
85-
httparse::Error::Version => HttpVersionError,
79+
httparse::Error::HeaderName => Header,
80+
httparse::Error::HeaderValue => Header,
81+
httparse::Error::NewLine => Header,
82+
httparse::Error::Status => Status,
83+
httparse::Error::Token => Header,
84+
httparse::Error::TooManyHeaders => TooLarge,
85+
httparse::Error::Version => Version,
8686
}
8787
}
8888
}

src/header/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use typeable::Typeable;
1717
use unicase::UniCase;
1818

1919
use self::internals::Item;
20-
use error::HttpResult;
2120

2221
pub use self::shared::*;
2322
pub use self::common::*;
@@ -113,7 +112,7 @@ impl Headers {
113112
}
114113

115114
#[doc(hidden)]
116-
pub fn from_raw<'a>(raw: &[httparse::Header<'a>]) -> HttpResult<Headers> {
115+
pub fn from_raw<'a>(raw: &[httparse::Header<'a>]) -> ::Result<Headers> {
117116
let mut headers = Headers::new();
118117
for header in raw {
119118
trace!("raw header: {:?}={:?}", header.name, &header.value[..]);

src/http.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use method::Method;
1313
use status::StatusCode;
1414
use uri::RequestUri;
1515
use version::HttpVersion::{self, Http10, Http11};
16-
use HttpError::{HttpIoError, HttpTooLargeError};
17-
use {HttpError, HttpResult};
16+
use {Error};
1817

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

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

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

347-
fn parse<R: Read, T: TryParse<Subject=I>, I>(rdr: &mut BufReader<R>) -> HttpResult<Incoming<I>> {
346+
fn parse<R: Read, T: TryParse<Subject=I>, I>(rdr: &mut BufReader<R>) -> ::Result<Incoming<I>> {
348347
loop {
349348
match try!(try_parse::<R, T, I>(rdr)) {
350349
httparse::Status::Complete((inc, len)) => {
@@ -355,12 +354,12 @@ fn parse<R: Read, T: TryParse<Subject=I>, I>(rdr: &mut BufReader<R>) -> HttpResu
355354
}
356355
match try!(rdr.read_into_buf()) {
357356
0 if rdr.get_buf().is_empty() => {
358-
return Err(HttpIoError(io::Error::new(
357+
return Err(Error::Io(io::Error::new(
359358
io::ErrorKind::ConnectionAborted,
360359
"Connection closed"
361360
)))
362361
},
363-
0 => return Err(HttpTooLargeError),
362+
0 => return Err(Error::TooLarge),
364363
_ => ()
365364
}
366365
}
@@ -377,7 +376,7 @@ trait TryParse {
377376
fn try_parse<'a>(headers: &'a mut [httparse::Header<'a>], buf: &'a [u8]) -> TryParseResult<Self::Subject>;
378377
}
379378

380-
type TryParseResult<T> = Result<httparse::Status<(Incoming<T>, usize)>, HttpError>;
379+
type TryParseResult<T> = Result<httparse::Status<(Incoming<T>, usize)>, Error>;
381380

382381
impl<'a> TryParse for httparse::Request<'a, 'a> {
383382
type Subject = (Method, RequestUri);
@@ -552,12 +551,12 @@ mod tests {
552551
#[test]
553552
fn test_parse_tcp_closed() {
554553
use std::io::ErrorKind;
555-
use error::HttpError::HttpIoError;
554+
use error::Error;
556555

557556
let mut empty = MockStream::new();
558557
let mut buf = BufReader::new(&mut empty);
559558
match parse_request(&mut buf) {
560-
Err(HttpIoError(ref e)) if e.kind() == ErrorKind::ConnectionAborted => (),
559+
Err(Error::Io(ref e)) if e.kind() == ErrorKind::ConnectionAborted => (),
561560
other => panic!("unexpected result: {:?}", other)
562561
}
563562
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ extern crate test;
149149
pub use mimewrapper::mime;
150150
pub use url::Url;
151151
pub use client::Client;
152-
pub use error::{HttpResult, HttpError};
152+
pub use error::{Result, Error};
153153
pub use method::Method::{Get, Head, Post, Delete};
154154
pub use status::StatusCode::{Ok, BadRequest, NotFound};
155155
pub use server::Server;

src/method.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33
use std::str::FromStr;
44
use std::convert::AsRef;
55

6-
use error::HttpError;
6+
use error::Error;
77
use self::Method::{Options, Get, Post, Put, Delete, Head, Trace, Connect, Patch,
88
Extension};
99

@@ -87,10 +87,10 @@ impl Method {
8787
}
8888

8989
impl FromStr for Method {
90-
type Err = HttpError;
91-
fn from_str(s: &str) -> Result<Method, HttpError> {
90+
type Err = Error;
91+
fn from_str(s: &str) -> Result<Method, Error> {
9292
if s == "" {
93-
Err(HttpError::HttpMethodError)
93+
Err(Error::Method)
9494
} else {
9595
Ok(match s {
9696
"OPTIONS" => Options,

0 commit comments

Comments
 (0)