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
use std::{error::Error, fmt::Display};

use http::Response;
use mas_axum_utils::axum::body::Bytes;
use serde::Deserialize;
use tracing::debug;

/// Represents a Matrix error
/// Ref: <https://spec.matrix.org/v1.10/client-server-api/#standard-error-response>
#[derive(Debug, Deserialize)]
struct MatrixError {
    errcode: String,
    error: String,
}

/// Represents an error received from the homeserver.
/// Where possible, we capture the Matrix error from the JSON response body.
///
/// Note that the `CatchHttpCodes` layer already captures the `StatusCode` for
/// us; we don't need to do that twice.
#[derive(Debug)]
pub(crate) struct HomeserverError {
    matrix_error: Option<MatrixError>,
}

impl Display for HomeserverError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(matrix_error) = &self.matrix_error {
            write!(f, "{matrix_error}")
        } else {
            write!(f, "(no specific error)")
        }
    }
}

impl Error for HomeserverError {}

impl HomeserverError {
    /// Return the error code (`errcode`)
    pub fn errcode(&self) -> Option<&str> {
        self.matrix_error.as_ref().map(|me| me.errcode.as_str())
    }
}

/// Parses a JSON-encoded Matrix error from the response body
/// Spec reference: <https://spec.matrix.org/v1.10/client-server-api/#standard-error-response>
#[allow(clippy::needless_pass_by_value)]
pub(crate) fn catch_homeserver_error(response: Response<Bytes>) -> HomeserverError {
    let matrix_error: Option<MatrixError> = match serde_json::from_slice(response.body().as_ref()) {
        Ok(body) => Some(body),
        Err(err) => {
            debug!("failed to deserialise expected homeserver error: {err:?}");
            None
        }
    };
    HomeserverError { matrix_error }
}

impl Display for MatrixError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let MatrixError { errcode, error } = &self;
        write!(f, "{errcode}: {error}")
    }
}