use std::{error::Error, fmt::Display};
use http::Response;
use mas_axum_utils::axum::body::Bytes;
use serde::Deserialize;
use tracing::debug;
#[derive(Debug, Deserialize)]
struct MatrixError {
errcode: String,
error: String,
}
#[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 {
pub fn errcode(&self) -> Option<&str> {
self.matrix_error.as_ref().map(|me| me.errcode.as_str())
}
}
#[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}")
}
}