add Error variant for FeatureDisabled

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-12-18 20:48:40 +00:00 committed by strawberry
parent 3a2c5be4f4
commit d91570d0e6
2 changed files with 6 additions and 2 deletions

View file

@ -98,6 +98,8 @@ pub enum Error {
ContentDisposition(#[from] ruma::http_headers::ContentDispositionParseError), ContentDisposition(#[from] ruma::http_headers::ContentDispositionParseError),
#[error("{0}")] #[error("{0}")]
Database(Cow<'static, str>), Database(Cow<'static, str>),
#[error("Feature '{0}' is not available on this server.")]
FeatureDisabled(Cow<'static, str>),
#[error("Remote server {0} responded with: {1}")] #[error("Remote server {0} responded with: {1}")]
Federation(ruma::OwnedServerName, ruma::api::client::error::Error), Federation(ruma::OwnedServerName, ruma::api::client::error::Error),
#[error("{0} in {1}")] #[error("{0} in {1}")]
@ -153,12 +155,13 @@ impl Error {
/// Returns the Matrix error code / error kind /// Returns the Matrix error code / error kind
#[inline] #[inline]
pub fn kind(&self) -> ruma::api::client::error::ErrorKind { pub fn kind(&self) -> ruma::api::client::error::ErrorKind {
use ruma::api::client::error::ErrorKind::Unknown; use ruma::api::client::error::ErrorKind::{FeatureDisabled, Unknown};
match self { match self {
| Self::Federation(_, error) | Self::Ruma(error) => | Self::Federation(_, error) | Self::Ruma(error) =>
response::ruma_error_kind(error).clone(), response::ruma_error_kind(error).clone(),
| Self::BadRequest(kind, ..) | Self::Request(kind, ..) => kind.clone(), | Self::BadRequest(kind, ..) | Self::Request(kind, ..) => kind.clone(),
| Self::FeatureDisabled(..) => FeatureDisabled,
| _ => Unknown, | _ => Unknown,
} }
} }
@ -172,6 +175,7 @@ impl Error {
| Self::Federation(_, error) | Self::Ruma(error) => error.status_code, | Self::Federation(_, error) | Self::Ruma(error) => error.status_code,
| Self::Request(kind, _, code) => response::status_code(kind, *code), | Self::Request(kind, _, code) => response::status_code(kind, *code),
| Self::BadRequest(kind, ..) => response::bad_request_code(kind), | Self::BadRequest(kind, ..) => response::bad_request_code(kind),
| Self::FeatureDisabled(..) => response::bad_request_code(&self.kind()),
| Self::Reqwest(error) => error.status().unwrap_or(StatusCode::INTERNAL_SERVER_ERROR), | Self::Reqwest(error) => error.status().unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
| Self::Conflict(_) => StatusCode::CONFLICT, | Self::Conflict(_) => StatusCode::CONFLICT,
| _ => StatusCode::INTERNAL_SERVER_ERROR, | _ => StatusCode::INTERNAL_SERVER_ERROR,

View file

@ -66,7 +66,7 @@ pub(super) fn bad_request_code(kind: &ErrorKind) -> StatusCode {
| Unrecognized => StatusCode::METHOD_NOT_ALLOWED, | Unrecognized => StatusCode::METHOD_NOT_ALLOWED,
// 404 // 404
| NotFound => StatusCode::NOT_FOUND, | NotFound | NotImplemented | FeatureDisabled => StatusCode::NOT_FOUND,
// 403 // 403
| GuestAccessForbidden | GuestAccessForbidden