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
//! # Error and Result Type

use std::error;
use std::fmt;

use reqwest;
use serde::{Deserialize, Serialize};

/// A result type that uses [`FirebaseError`] as an error type
pub type Result<T> = std::result::Result<T, FirebaseError>;

/// The main error type used throughout this crate. It wraps / converts from a few other error
/// types and implements [error::Error] so that you can use it in any situation where the
/// standard error type is expected.
#[derive(Debug)]
pub enum FirebaseError {
    /// Generic errors are very rarely used and only used if no other error type matches
    Generic(&'static str),
    /// If the http status code is != 200 and no Google error response is attached
    /// (see https://firebase.google.com/docs/reference/rest/auth#section-error-format)
    /// then this error type will be returned
    UnexpectedResponse(&'static str, reqwest::StatusCode, String, String),
    /// An error returned by the Firestore API - Contains the numeric code, a string code and
    /// a context. If the APIError happened on a document query or mutation, the document
    /// path will be set as context.
    /// If the APIError happens on a user_* method, the user id will be set as context.
    /// For example: 400, CREDENTIAL_TOO_OLD_LOGIN_AGAIN
    APIError(usize, String, String),
    /// An error caused by the http library. This only happens if the http request is badly
    /// formatted (too big, invalid characters) or if the server did strange things
    /// (connection abort, ssl verification error).
    Request(reqwest::Error),
    /// Should not happen. If jwt encoding / decoding fails or an value cannot be extracted or
    /// a jwt is badly formatted or corrupted
    JWT(biscuit::errors::Error),
    JWTValidation(biscuit::errors::ValidationError),
    /// Serialisation failed
    Ser {
        doc: Option<String>,
        ser: serde_json::Error,
    },
    /// When the credentials.json file contains an invalid private key this error is returned
    RSA(ring::error::KeyRejected),
    /// Disk access errors
    IO(std::io::Error),
}

impl std::convert::From<std::io::Error> for FirebaseError {
    fn from(error: std::io::Error) -> Self {
        FirebaseError::IO(error)
    }
}

impl std::convert::From<ring::error::KeyRejected> for FirebaseError {
    fn from(error: ring::error::KeyRejected) -> Self {
        FirebaseError::RSA(error)
    }
}

impl std::convert::From<serde_json::Error> for FirebaseError {
    fn from(error: serde_json::Error) -> Self {
        FirebaseError::Ser { doc: None, ser: error }
    }
}

impl std::convert::From<biscuit::errors::Error> for FirebaseError {
    fn from(error: biscuit::errors::Error) -> Self {
        FirebaseError::JWT(error)
    }
}

impl std::convert::From<biscuit::errors::ValidationError> for FirebaseError {
    fn from(error: biscuit::errors::ValidationError) -> Self {
        FirebaseError::JWTValidation(error)
    }
}

impl std::convert::From<reqwest::Error> for FirebaseError {
    fn from(error: reqwest::Error) -> Self {
        FirebaseError::Request(error)
    }
}

impl fmt::Display for FirebaseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            FirebaseError::Generic(m) => write!(f, "{}", m),
            FirebaseError::APIError(code, ref m, ref context) => {
                write!(f, "API Error! Code {} - {}. Context: {}", code, m, context)
            }
            FirebaseError::UnexpectedResponse(m, status, ref text, ref source) => {
                writeln!(f, "{} - {}", &m, status)?;
                writeln!(f, "{}", text)?;
                writeln!(f, "{}", source)?;
                Ok(())
            }
            FirebaseError::Request(ref e) => e.fmt(f),
            FirebaseError::JWT(ref e) => e.fmt(f),
            FirebaseError::JWTValidation(ref e) => e.fmt(f),
            FirebaseError::RSA(ref e) => e.fmt(f),
            FirebaseError::IO(ref e) => e.fmt(f),
            FirebaseError::Ser { ref doc, ref ser } => {
                if let Some(doc) = doc {
                    writeln!(f, "{} in document {}", ser, doc)
                } else {
                    ser.fmt(f)
                }
            }
        }
    }
}

impl error::Error for FirebaseError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            FirebaseError::Generic(ref _m) => None,
            FirebaseError::UnexpectedResponse(_, _, _, _) => None,
            FirebaseError::APIError(_, _, _) => None,
            FirebaseError::Request(ref e) => Some(e),
            FirebaseError::JWT(ref e) => Some(e),
            FirebaseError::JWTValidation(ref e) => Some(e),
            FirebaseError::RSA(_) => None,
            FirebaseError::IO(ref e) => Some(e),
            FirebaseError::Ser { ref ser, .. } => Some(ser),
        }
    }
}

#[derive(Default, Serialize, Deserialize)]
struct GoogleRESTApiError {
    pub message: String,
    pub domain: String,
    pub reason: String,
}

#[derive(Default, Serialize, Deserialize)]
struct GoogleRESTApiErrorInfo {
    pub code: usize,
    pub message: String,
    pub errors: Option<Vec<GoogleRESTApiError>>,
}

#[derive(Default, Serialize, Deserialize)]
struct GoogleRESTApiErrorWrapper {
    pub error: Option<GoogleRESTApiErrorInfo>,
}

/// If the given reqwest response is status code 200, nothing happens
/// Otherwise the response will be analysed if it contains a Google API Error response.
/// See https://firebase.google.com/docs/reference/rest/auth#section-error-response
///
/// Arguments:
/// - response: The http requests response. Must be mutable, because the contained value will be extracted in an error case
/// - context: A function that will be called in an error case that returns a context string
pub(crate) fn extract_google_api_error(response: &mut reqwest::Response, context: impl Fn() -> String) -> Result<()> {
    if response.status() == 200 {
        // The boring case
        return Ok(());
    }

    let google_api_error_wrapper: std::result::Result<GoogleRESTApiErrorWrapper, _> =
        serde_json::from_str(&response.text()?);

    match google_api_error_wrapper {
        Ok(google_api_error_wrapper) => {
            if let Some(google_api_error) = google_api_error_wrapper.error {
                return Err(FirebaseError::APIError(
                    google_api_error.code,
                    google_api_error.message.to_owned(),
                    context(),
                ));
            }
        }
        Err(_) => {}
    };

    Err(FirebaseError::UnexpectedResponse(
        "",
        response.status(),
        response.text()?,
        context(),
    ))
}