From d91570d0e61d7c06b0a4659b3803ced3e52ccf35 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 18 Dec 2024 20:48:40 +0000 Subject: [PATCH] add Error variant for FeatureDisabled Signed-off-by: Jason Volk --- src/core/error/mod.rs | 6 +++++- src/core/error/response.rs | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/core/error/mod.rs b/src/core/error/mod.rs index 12ba0797..f38178e6 100644 --- a/src/core/error/mod.rs +++ b/src/core/error/mod.rs @@ -98,6 +98,8 @@ pub enum Error { ContentDisposition(#[from] ruma::http_headers::ContentDispositionParseError), #[error("{0}")] Database(Cow<'static, str>), + #[error("Feature '{0}' is not available on this server.")] + FeatureDisabled(Cow<'static, str>), #[error("Remote server {0} responded with: {1}")] Federation(ruma::OwnedServerName, ruma::api::client::error::Error), #[error("{0} in {1}")] @@ -153,12 +155,13 @@ impl Error { /// Returns the Matrix error code / error kind #[inline] 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 { | Self::Federation(_, error) | Self::Ruma(error) => response::ruma_error_kind(error).clone(), | Self::BadRequest(kind, ..) | Self::Request(kind, ..) => kind.clone(), + | Self::FeatureDisabled(..) => FeatureDisabled, | _ => Unknown, } } @@ -172,6 +175,7 @@ impl Error { | Self::Federation(_, error) | Self::Ruma(error) => error.status_code, | Self::Request(kind, _, code) => response::status_code(kind, *code), | 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::Conflict(_) => StatusCode::CONFLICT, | _ => StatusCode::INTERNAL_SERVER_ERROR, diff --git a/src/core/error/response.rs b/src/core/error/response.rs index 568238c3..335fddab 100644 --- a/src/core/error/response.rs +++ b/src/core/error/response.rs @@ -66,7 +66,7 @@ pub(super) fn bad_request_code(kind: &ErrorKind) -> StatusCode { | Unrecognized => StatusCode::METHOD_NOT_ALLOWED, // 404 - | NotFound => StatusCode::NOT_FOUND, + | NotFound | NotImplemented | FeatureDisabled => StatusCode::NOT_FOUND, // 403 | GuestAccessForbidden