1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
//! Error and Result module
use std::{io, fmt, result};
use std::str::Utf8Error;
use std::string::FromUtf8Error;
use std::io::Error as IoError;

use cookie;
use httparse;
use actix::MailboxError;
use futures::Canceled;
use failure;
use failure::{Fail, Backtrace};
use http2::Error as Http2Error;
use http::{header, StatusCode, Error as HttpError};
use http::uri::InvalidUriBytes;
use http_range::HttpRangeParseError;
use serde_json::error::Error as JsonError;
pub use url::ParseError as UrlParseError;

// re-exports
pub use cookie::{ParseError as CookieParseError};

use body::Body;
use handler::Responder;
use httprequest::HttpRequest;
use httpresponse::HttpResponse;
use httpcodes::{self, HTTPExpectationFailed};

/// A specialized [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html)
/// for actix web operations
///
/// This typedef is generally used to avoid writing out `actix_web::error::Error` directly and
/// is otherwise a direct mapping to `Result`.
pub type Result<T, E=Error> = result::Result<T, E>;

/// General purpose actix web error
pub struct Error {
    cause: Box<ResponseError>,
    backtrace: Option<Backtrace>,
}

impl Error {

    /// Returns a reference to the underlying cause of this Error.
    // this should return &Fail but needs this https://github.com/rust-lang/rust/issues/5665
    pub fn cause(&self) -> &ResponseError {
        self.cause.as_ref()
    }
}

/// Error that can be converted to `HttpResponse`
pub trait ResponseError: Fail {

    /// Create response for error
    ///
    /// Internal server error is generated by default.
    fn error_response(&self) -> HttpResponse {
        HttpResponse::new(StatusCode::INTERNAL_SERVER_ERROR, Body::Empty)
    }
}

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

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(bt) = self.cause.backtrace() {
            write!(f, "{:?}\n\n{:?}", &self.cause, bt)
        } else {
            write!(f, "{:?}\n\n{:?}", &self.cause, self.backtrace.as_ref().unwrap())
        }
    }
}

/// `HttpResponse` for `Error`
impl From<Error> for HttpResponse {
    fn from(err: Error) -> Self {
        HttpResponse::from_error(err)
    }
}

/// `Error` for any error that implements `ResponseError`
impl<T: ResponseError> From<T> for Error {
    fn from(err: T) -> Error {
        let backtrace = if err.backtrace().is_none() {
            Some(Backtrace::new())
        } else {
            None
        };
        Error { cause: Box::new(err), backtrace }
    }
}

/// Compatibility for `failure::Error`
impl<T> ResponseError for failure::Compat<T>
    where T: fmt::Display + fmt::Debug + Sync + Send + 'static { }

impl From<failure::Error> for Error {
    fn from(err: failure::Error) -> Error {
        err.compat().into()
    }
}

/// `InternalServerError` for `JsonError`
impl ResponseError for JsonError {}

/// `InternalServerError` for `UrlParseError`
impl ResponseError for UrlParseError {}

/// Return `InternalServerError` for `HttpError`,
/// Response generation can return `HttpError`, so it is internal error
impl ResponseError for HttpError {}

/// Return `InternalServerError` for `io::Error`
impl ResponseError for io::Error {

    fn error_response(&self) -> HttpResponse {
        match self.kind() {
            io::ErrorKind::NotFound =>
                HttpResponse::new(StatusCode::NOT_FOUND, Body::Empty),
            io::ErrorKind::PermissionDenied =>
                HttpResponse::new(StatusCode::FORBIDDEN, Body::Empty),
            _ =>
                HttpResponse::new(StatusCode::INTERNAL_SERVER_ERROR, Body::Empty)
        }
    }
}

/// `InternalServerError` for `InvalidHeaderValue`
impl ResponseError for header::InvalidHeaderValue {}

/// `InternalServerError` for `futures::Canceled`
impl ResponseError for Canceled {}

/// `InternalServerError` for `actix::MailboxError`
impl ResponseError for MailboxError {}

/// A set of errors that can occur during parsing HTTP streams
#[derive(Fail, Debug)]
pub enum ParseError {
    /// An invalid `Method`, such as `GE.T`.
    #[fail(display="Invalid Method specified")]
    Method,
    /// An invalid `Uri`, such as `exam ple.domain`.
    #[fail(display="Uri error: {}", _0)]
    Uri(InvalidUriBytes),
    /// An invalid `HttpVersion`, such as `HTP/1.1`
    #[fail(display="Invalid HTTP version specified")]
    Version,
    /// An invalid `Header`.
    #[fail(display="Invalid Header provided")]
    Header,
    /// A message head is too large to be reasonable.
    #[fail(display="Message head is too large")]
    TooLarge,
    /// A message reached EOF, but is not complete.
    #[fail(display="Message is incomplete")]
    Incomplete,
    /// An invalid `Status`, such as `1337 ELITE`.
    #[fail(display="Invalid Status provided")]
    Status,
    /// A timeout occurred waiting for an IO event.
    #[allow(dead_code)]
    #[fail(display="Timeout")]
    Timeout,
    /// An `io::Error` that occurred while trying to read or write to a network stream.
    #[fail(display="IO error: {}", _0)]
    Io(#[cause] IoError),
    /// Parsing a field as string failed
    #[fail(display="UTF8 error: {}", _0)]
    Utf8(#[cause] Utf8Error),
}

/// Return `BadRequest` for `ParseError`
impl ResponseError for ParseError {
    fn error_response(&self) -> HttpResponse {
        HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
    }
}

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

impl From<Utf8Error> for ParseError {
    fn from(err: Utf8Error) -> ParseError {
        ParseError::Utf8(err)
    }
}

impl From<FromUtf8Error> for ParseError {
    fn from(err: FromUtf8Error) -> ParseError {
        ParseError::Utf8(err.utf8_error())
    }
}

impl From<httparse::Error> for ParseError {
    fn from(err: httparse::Error) -> ParseError {
        match err {
            httparse::Error::HeaderName | httparse::Error::HeaderValue |
                httparse::Error::NewLine | httparse::Error::Token => ParseError::Header,
            httparse::Error::Status => ParseError::Status,
            httparse::Error::TooManyHeaders => ParseError::TooLarge,
            httparse::Error::Version => ParseError::Version,
        }
    }
}

#[derive(Fail, Debug)]
/// A set of errors that can occur during payload parsing
pub enum PayloadError {
    /// A payload reached EOF, but is not complete.
    #[fail(display="A payload reached EOF, but is not complete.")]
    Incomplete,
    /// Content encoding stream corruption
    #[fail(display="Can not decode content-encoding.")]
    EncodingCorrupted,
    /// A payload reached size limit.
    #[fail(display="A payload reached size limit.")]
    Overflow,
    /// A payload length is unknown.
    #[fail(display="A payload length is unknown.")]
    UnknownLength,
    /// Io error
    #[fail(display="{}", _0)]
    Io(#[cause] IoError),
    /// Http2 error
    #[fail(display="{}", _0)]
    Http2(#[cause] Http2Error),
}

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

/// `InternalServerError` for `PayloadError`
impl ResponseError for PayloadError {}

/// Return `BadRequest` for `cookie::ParseError`
impl ResponseError for cookie::ParseError {
    fn error_response(&self) -> HttpResponse {
        HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
    }
}

/// Http range header parsing error
#[derive(Fail, PartialEq, Debug)]
pub enum HttpRangeError {
    /// Returned if range is invalid.
    #[fail(display="Range header is invalid")]
    InvalidRange,
    /// Returned if first-byte-pos of all of the byte-range-spec
    /// values is greater than the content size.
    /// See `https://github.com/golang/go/commit/aa9b3d7`
    #[fail(display="First-byte-pos of all of the byte-range-spec values is greater than the content size")]
    NoOverlap,
}

/// Return `BadRequest` for `HttpRangeError`
impl ResponseError for HttpRangeError {
    fn error_response(&self) -> HttpResponse {
        HttpResponse::new(
            StatusCode::BAD_REQUEST, Body::from("Invalid Range header provided"))
    }
}

impl From<HttpRangeParseError> for HttpRangeError {
    fn from(err: HttpRangeParseError) -> HttpRangeError {
        match err {
            HttpRangeParseError::InvalidRange => HttpRangeError::InvalidRange,
            HttpRangeParseError::NoOverlap => HttpRangeError::NoOverlap,
        }
    }
}

/// A set of errors that can occur during parsing multipart streams
#[derive(Fail, Debug)]
pub enum MultipartError {
    /// Content-Type header is not found
    #[fail(display="No Content-type header found")]
    NoContentType,
    /// Can not parse Content-Type header
    #[fail(display="Can not parse Content-Type header")]
    ParseContentType,
    /// Multipart boundary is not found
    #[fail(display="Multipart boundary is not found")]
    Boundary,
    /// Multipart stream is incomplete
    #[fail(display="Multipart stream is incomplete")]
    Incomplete,
    /// Error during field parsing
    #[fail(display="{}", _0)]
    Parse(#[cause] ParseError),
    /// Payload error
    #[fail(display="{}", _0)]
    Payload(#[cause] PayloadError),
}

impl From<ParseError> for MultipartError {
    fn from(err: ParseError) -> MultipartError {
        MultipartError::Parse(err)
    }
}

impl From<PayloadError> for MultipartError {
    fn from(err: PayloadError) -> MultipartError {
        MultipartError::Payload(err)
    }
}

/// Return `BadRequest` for `MultipartError`
impl ResponseError for MultipartError {

    fn error_response(&self) -> HttpResponse {
        HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
    }
}

/// Error during handling `Expect` header
#[derive(Fail, PartialEq, Debug)]
pub enum ExpectError {
    /// Expect header value can not be converted to utf8
    #[fail(display="Expect header value can not be converted to utf8")]
    Encoding,
    /// Unknown expect value
    #[fail(display="Unknown expect value")]
    UnknownExpect,
}

impl ResponseError for ExpectError {
    fn error_response(&self) -> HttpResponse {
        HTTPExpectationFailed.with_body("Unknown Expect")
    }
}

/// A set of error that can occure during parsing content type
#[derive(Fail, PartialEq, Debug)]
pub enum ContentTypeError {
    /// Can not parse content type
    #[fail(display="Can not parse content type")]
    ParseError,
    /// Unknown content encoding
    #[fail(display="Unknown content encoding")]
    UnknownEncoding,
}

/// Return `BadRequest` for `ContentTypeError`
impl ResponseError for ContentTypeError {
    fn error_response(&self) -> HttpResponse {
        HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
    }
}

/// A set of errors that can occur during parsing urlencoded payloads
#[derive(Fail, Debug)]
pub enum UrlencodedError {
    /// Can not decode chunked transfer encoding
    #[fail(display="Can not decode chunked transfer encoding")]
    Chunked,
    /// Payload size is bigger than 256k
    #[fail(display="Payload size is bigger than 256k")]
    Overflow,
    /// Payload size is now known
    #[fail(display="Payload size is now known")]
    UnknownLength,
    /// Content type error
    #[fail(display="Content type error")]
    ContentType,
    /// Parse error
    #[fail(display="Parse error")]
    Parse,
    /// Payload error
    #[fail(display="Error that occur during reading payload: {}", _0)]
    Payload(#[cause] PayloadError),
}

/// Return `BadRequest` for `UrlencodedError`
impl ResponseError for UrlencodedError {

    fn error_response(&self) -> HttpResponse {
        match *self {
            UrlencodedError::Overflow => httpcodes::HTTPPayloadTooLarge.into(),
            UrlencodedError::UnknownLength => httpcodes::HTTPLengthRequired.into(),
            _ => httpcodes::HTTPBadRequest.into(),
        }
    }
}

impl From<PayloadError> for UrlencodedError {
    fn from(err: PayloadError) -> UrlencodedError {
        UrlencodedError::Payload(err)
    }
}

/// A set of errors that can occur during parsing json payloads
#[derive(Fail, Debug)]
pub enum JsonPayloadError {
    /// Payload size is bigger than 256k
    #[fail(display="Payload size is bigger than 256k")]
    Overflow,
    /// Content type error
    #[fail(display="Content type error")]
    ContentType,
    /// Deserialize error
    #[fail(display="Json deserialize error: {}", _0)]
    Deserialize(#[cause] JsonError),
    /// Payload error
    #[fail(display="Error that occur during reading payload: {}", _0)]
    Payload(#[cause] PayloadError),
}

/// Return `BadRequest` for `UrlencodedError`
impl ResponseError for JsonPayloadError {

    fn error_response(&self) -> HttpResponse {
        match *self {
            JsonPayloadError::Overflow => httpcodes::HTTPPayloadTooLarge.into(),
            _ => httpcodes::HTTPBadRequest.into(),
        }
    }
}

impl From<PayloadError> for JsonPayloadError {
    fn from(err: PayloadError) -> JsonPayloadError {
        JsonPayloadError::Payload(err)
    }
}

impl From<JsonError> for JsonPayloadError {
    fn from(err: JsonError) -> JsonPayloadError {
        JsonPayloadError::Deserialize(err)
    }
}

/// Errors which can occur when attempting to interpret a segment string as a
/// valid path segment.
#[derive(Fail, Debug, PartialEq)]
pub enum UriSegmentError {
    /// The segment started with the wrapped invalid character.
    #[fail(display="The segment started with the wrapped invalid character")]
    BadStart(char),
    /// The segment contained the wrapped invalid character.
    #[fail(display="The segment contained the wrapped invalid character")]
    BadChar(char),
    /// The segment ended with the wrapped invalid character.
    #[fail(display="The segment ended with the wrapped invalid character")]
    BadEnd(char),
}

/// Return `BadRequest` for `UriSegmentError`
impl ResponseError for UriSegmentError {

    fn error_response(&self) -> HttpResponse {
        HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
    }
}

/// Errors which can occur when attempting to generate resource uri.
#[derive(Fail, Debug, PartialEq)]
pub enum UrlGenerationError {
    #[fail(display="Resource not found")]
    ResourceNotFound,
    #[fail(display="Not all path pattern covered")]
    NotEnoughElements,
    #[fail(display="Router is not available")]
    RouterNotAvailable,
    #[fail(display="{}", _0)]
    ParseError(#[cause] UrlParseError),
}

/// `InternalServerError` for `UrlGeneratorError`
impl ResponseError for UrlGenerationError {}

impl From<UrlParseError> for UrlGenerationError {
    fn from(err: UrlParseError) -> Self {
        UrlGenerationError::ParseError(err)
    }
}

/// Helper type that can wrap any error and generate custom response.
///
/// In following example any `io::Error` will be converted into "BAD REQUEST" response
/// as opposite to *INNTERNAL SERVER ERROR* which is defined by default.
///
/// ```rust
/// # extern crate actix_web;
/// # use actix_web::*;
/// use actix_web::fs::NamedFile;
///
/// fn index(req: HttpRequest) -> Result<fs::NamedFile> {
///    let f = NamedFile::open("test.txt").map_err(error::ErrorBadRequest)?;
///    Ok(f)
/// }
/// # fn main() {}
/// ```
pub struct InternalError<T> {
    cause: T,
    status: StatusCode,
    backtrace: Backtrace,
}

unsafe impl<T> Sync for InternalError<T> {}
unsafe impl<T> Send for InternalError<T> {}

impl<T> InternalError<T> {
    pub fn new(cause: T, status: StatusCode) -> Self {
        InternalError {
            cause,
            status,
            backtrace: Backtrace::new(),
        }
    }
}

impl<T> Fail for InternalError<T>
    where T: Send + Sync + fmt::Debug + 'static
{
    fn backtrace(&self) -> Option<&Backtrace> {
        Some(&self.backtrace)
    }
}

impl<T> fmt::Debug for InternalError<T>
    where T: Send + Sync + fmt::Debug + 'static
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(&self.cause, f)
    }
}

impl<T> fmt::Display for InternalError<T>
    where T: Send + Sync + fmt::Debug + 'static
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(&self.cause, f)
    }
}

impl<T> ResponseError for InternalError<T>
    where T: Send + Sync + fmt::Debug + 'static
{
    fn error_response(&self) -> HttpResponse {
        HttpResponse::new(self.status, Body::Empty)
    }
}

impl<T> Responder for InternalError<T>
    where T: Send + Sync + fmt::Debug + 'static
{
    type Item = HttpResponse;
    type Error = Error;

    fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, Error> {
        Err(self.into())
    }
}

/// Helper function that creates wrapper of any error and generate *BAD REQUEST* response.
#[allow(non_snake_case)]
pub fn ErrorBadRequest<T>(err: T) -> InternalError<T> {
    InternalError::new(err, StatusCode::BAD_REQUEST)
}

///  Helper function that creates wrapper of any error and generate *UNAUTHORIZED* response.
#[allow(non_snake_case)]
pub fn ErrorUnauthorized<T>(err: T) -> InternalError<T> {
    InternalError::new(err, StatusCode::UNAUTHORIZED)
}

///  Helper function that creates wrapper of any error and generate *FORBIDDEN* response.
#[allow(non_snake_case)]
pub fn ErrorForbidden<T>(err: T) -> InternalError<T> {
    InternalError::new(err, StatusCode::FORBIDDEN)
}

///  Helper function that creates wrapper of any error and generate *NOT FOUND* response.
#[allow(non_snake_case)]
pub fn ErrorNotFound<T>(err: T) -> InternalError<T> {
    InternalError::new(err, StatusCode::NOT_FOUND)
}

///  Helper function that creates wrapper of any error and generate *METHOD NOT ALLOWED* response.
#[allow(non_snake_case)]
pub fn ErrorMethodNotAllowed<T>(err: T) -> InternalError<T> {
    InternalError::new(err, StatusCode::METHOD_NOT_ALLOWED)
}

///  Helper function that creates wrapper of any error and generate *REQUEST TIMEOUT* response.
#[allow(non_snake_case)]
pub fn ErrorRequestTimeout<T>(err: T) -> InternalError<T> {
    InternalError::new(err, StatusCode::REQUEST_TIMEOUT)
}

///  Helper function that creates wrapper of any error and generate *CONFLICT* response.
#[allow(non_snake_case)]
pub fn ErrorConflict<T>(err: T) -> InternalError<T> {
    InternalError::new(err, StatusCode::CONFLICT)
}

///  Helper function that creates wrapper of any error and generate *GONE* response.
#[allow(non_snake_case)]
pub fn ErrorGone<T>(err: T) -> InternalError<T> {
    InternalError::new(err, StatusCode::GONE)
}

///  Helper function that creates wrapper of any error and generate *PRECONDITION FAILED* response.
#[allow(non_snake_case)]
pub fn ErrorPreconditionFailed<T>(err: T) -> InternalError<T> {
    InternalError::new(err, StatusCode::PRECONDITION_FAILED)
}

///  Helper function that creates wrapper of any error and generate *EXPECTATION FAILED* response.
#[allow(non_snake_case)]
pub fn ErrorExpectationFailed<T>(err: T) -> InternalError<T> {
    InternalError::new(err, StatusCode::EXPECTATION_FAILED)
}

///  Helper function that creates wrapper of any error and generate *INTERNAL SERVER ERROR* response.
#[allow(non_snake_case)]
pub fn ErrorInternalServerError<T>(err: T) -> InternalError<T> {
    InternalError::new(err, StatusCode::INTERNAL_SERVER_ERROR)
}

#[cfg(test)]
mod tests {
    use std::env;
    use std::error::Error as StdError;
    use std::io;
    use httparse;
    use http::{StatusCode, Error as HttpError};
    use cookie::ParseError as CookieParseError;
    use failure;
    use super::*;

    #[test]
    #[cfg(actix_nightly)]
    fn test_nightly() {
        let resp: HttpResponse = IoError::new(io::ErrorKind::Other, "test").error_response();
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
    }

    #[test]
    fn test_into_response() {
        let resp: HttpResponse = ParseError::Incomplete.error_response();
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);

        let resp: HttpResponse = HttpRangeError::InvalidRange.error_response();
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);

        let resp: HttpResponse = CookieParseError::EmptyName.error_response();
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);

        let resp: HttpResponse = MultipartError::Boundary.error_response();
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);

        let err: HttpError = StatusCode::from_u16(10000).err().unwrap().into();
        let resp: HttpResponse = err.error_response();
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
    }

    #[test]
    fn test_cause() {
        let orig = io::Error::new(io::ErrorKind::Other, "other");
        let desc = orig.description().to_owned();
        let e = ParseError::Io(orig);
        assert_eq!(format!("{}", e.cause().unwrap()), desc);
    }

    #[test]
    fn test_error_cause() {
        let orig = io::Error::new(io::ErrorKind::Other, "other");
        let desc = orig.description().to_owned();
        let e = Error::from(orig);
        assert_eq!(format!("{}", e.cause()), desc);
    }

    #[test]
    fn test_error_display() {
        let orig = io::Error::new(io::ErrorKind::Other, "other");
        let desc = orig.description().to_owned();
        let e = Error::from(orig);
        assert_eq!(format!("{}", e), desc);
    }

    #[test]
    fn test_error_http_response() {
        let orig = io::Error::new(io::ErrorKind::Other, "other");
        let e = Error::from(orig);
        let resp: HttpResponse = e.into();
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
    }

    #[test]
    fn test_range_error() {
        let e: HttpRangeError = HttpRangeParseError::InvalidRange.into();
        assert_eq!(e, HttpRangeError::InvalidRange);
        let e: HttpRangeError = HttpRangeParseError::NoOverlap.into();
        assert_eq!(e, HttpRangeError::NoOverlap);
    }

    #[test]
    fn test_expect_error() {
        let resp: HttpResponse = ExpectError::Encoding.error_response();
        assert_eq!(resp.status(), StatusCode::EXPECTATION_FAILED);
        let resp: HttpResponse = ExpectError::UnknownExpect.error_response();
        assert_eq!(resp.status(), StatusCode::EXPECTATION_FAILED);
    }

    macro_rules! from {
        ($from:expr => $error:pat) => {
            match ParseError::from($from) {
                e @ $error => {
                    assert!(format!("{}", e).len() >= 5);
                } ,
                e => panic!("{:?}", e)
            }
        }
    }

    macro_rules! from_and_cause {
        ($from:expr => $error:pat) => {
            match ParseError::from($from) {
                e @ $error => {
                    let desc = format!("{}", e.cause().unwrap());
                    assert_eq!(desc, $from.description().to_owned());
                },
                _ => panic!("{:?}", $from)
            }
        }
    }

    #[test]
    fn test_from() {
        from_and_cause!(io::Error::new(io::ErrorKind::Other, "other") => ParseError::Io(..));

        from!(httparse::Error::HeaderName => ParseError::Header);
        from!(httparse::Error::HeaderName => ParseError::Header);
        from!(httparse::Error::HeaderValue => ParseError::Header);
        from!(httparse::Error::NewLine => ParseError::Header);
        from!(httparse::Error::Status => ParseError::Status);
        from!(httparse::Error::Token => ParseError::Header);
        from!(httparse::Error::TooManyHeaders => ParseError::TooLarge);
        from!(httparse::Error::Version => ParseError::Version);
    }

    #[test]
    fn failure_error() {
        const NAME: &str = "RUST_BACKTRACE";
        let old_tb = env::var(NAME);
        env::set_var(NAME, "0");
        let error = failure::err_msg("Hello!");
        let resp: Error = error.into();
        assert_eq!(format!("{:?}", resp), "Compat { error: ErrorMessage { msg: \"Hello!\" } }\n\n");
        match old_tb {
            Ok(x) => env::set_var(NAME, x),
            _ => env::remove_var(NAME),
        }
    }
}