ffsend_api/api/
request.rs

1use reqwest::{blocking::Response, StatusCode};
2use thiserror::Error;
3
4use crate::config::{HTTP_STATUS_EXPIRED, HTTP_STATUS_UNAUTHORIZED};
5use crate::ext::status_code::StatusCodeExt;
6
7/// Ensure the given response is successful. If it isn't, a corresponding `ResponseError` is returned.
8pub fn ensure_success(response: &Response) -> Result<(), ResponseError> {
9    // Get the status
10    let status = response.status();
11
12    // Stop if successful
13    if status.is_success() {
14        return Ok(());
15    }
16
17    // Handle the expired file error
18    if status == HTTP_STATUS_EXPIRED {
19        return Err(ResponseError::Expired);
20    }
21
22    // Handle the authentication issue error
23    if status == HTTP_STATUS_UNAUTHORIZED {
24        return Err(ResponseError::Unauthorized);
25    }
26
27    // Return the other error
28    Err(ResponseError::OtherHttp(status, status.err_text()))
29}
30
31#[derive(Error, Debug)]
32pub enum ResponseError {
33    /// This request lead to an expired file, or a file that never existed.
34    #[error("this file has expired or did never exist")]
35    Expired,
36
37    /// We were unauthorized to make this request.
38    /// This is usually because of an incorrect password.
39    #[error("unauthorized, are the credentials correct?")]
40    Unauthorized,
41
42    /// Some undefined error occurred with this response.
43    #[error("bad HTTP response: {}", _1)]
44    OtherHttp(StatusCode, String),
45
46    /// An undefined error message.
47    #[error("server responded with undefined error")]
48    Undefined,
49}