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
use std::{self, fmt, io};
use hyper;
use serde_json;

/// A network or validation error
#[derive(Debug)]
pub enum Error {
    DecodeJson(serde_json::Error),
    ConnectionError(Box<std::error::Error + Send + Sync + 'static>),
    InvalidToken,
    InvalidIssuer,
    InvalidAudience,
    InvalidHostedDomain,
}

impl std::error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::DecodeJson(ref err) => err.description(),
            Error::ConnectionError(ref err) => err.description(),
            Error::InvalidToken => "invalid token",
            Error::InvalidIssuer => "invalid issuer",
            Error::InvalidAudience => "invalid audience",
            Error::InvalidHostedDomain => "invalid hosted domain",
        }
    }

    fn cause(&self) -> Option<&std::error::Error> {
        match *self {
            Error::DecodeJson(ref err) => Some(err),
            Error::ConnectionError(ref err) => Some(&**err),
            _ => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::DecodeJson(ref err) => err.fmt(f),
            Error::ConnectionError(ref err) => err.fmt(f),
            Error::InvalidToken => f.write_str("Token was not recognized by google"),
            Error::InvalidIssuer => f.write_str("Token was not issued by google"),
            Error::InvalidAudience => f.write_str("Token is for a different google application"),
            Error::InvalidHostedDomain => f.write_str("User is not a member of the hosted domain(s)"),
        }
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::ConnectionError(Box::new(err))
    }
}

impl From<hyper::Error> for Error {
    fn from(err: hyper::Error) -> Error {
        Error::ConnectionError(Box::new(err))
    }
}

impl From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Error {
        Error::DecodeJson(err)
    }
}