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
//! Tide error types.
use http::{HttpTryFrom, StatusCode};
use http_service::Body;

use crate::response::{IntoResponse, Response};

/// A specialized Result type for Tide.
pub type Result<T = Response> = std::result::Result<T, Error>;

/// A generic error.
#[derive(Debug)]
pub struct Error {
    resp: Response,
}

impl IntoResponse for Error {
    fn into_response(self) -> Response {
        self.resp
    }
}

struct Cause(Box<dyn std::error::Error + Send + Sync>);

impl From<Response> for Error {
    fn from(resp: Response) -> Error {
        Error { resp }
    }
}

impl From<StatusCode> for Error {
    fn from(status: StatusCode) -> Error {
        Error {
            resp: Response::new(status.as_u16()),
        }
    }
}

/// Extension methods for `Result`.
pub trait ResultExt<T>: Sized {
    /// Convert to an `Result`, treating the `Err` case as a client
    /// error (response code 400).
    fn client_err(self) -> Result<T> {
        self.with_err_status(400)
    }

    /// Convert to an `Result`, treating the `Err` case as a server
    /// error (response code 500).
    fn server_err(self) -> Result<T> {
        self.with_err_status(500)
    }

    /// Convert to an `Result`, wrapping the `Err` case with a custom
    /// response status.
    fn with_err_status<S>(self, status: S) -> Result<T>
    where
        StatusCode: HttpTryFrom<S>;
}

impl<T, E: std::error::Error + Send + Sync + 'static> ResultExt<T> for std::result::Result<T, E> {
    fn with_err_status<S>(self, status: S) -> Result<T>
    where
        StatusCode: HttpTryFrom<S>,
    {
        self.map_err(|e| Error {
            resp: http::Response::builder()
                .status(status)
                .extension(Cause(Box::new(e)))
                .body(Body::empty())
                .unwrap()
                .into(),
        })
    }
}