Add the M_WRONG_ROOM_KEYS_VERSION ErrorKind variant

This error kind is a bit hidden in the spec, it's under the room key
backup section.

Spec URL: https://spec.matrix.org/v1.3/client-server-api/#put_matrixclientv3room_keyskeysroomid
This commit is contained in:
Damir Jelić 2023-10-24 16:19:12 +02:00
parent 6aa6b65258
commit e37918c9b8
3 changed files with 38 additions and 1 deletions

View File

@ -1,5 +1,9 @@
# [unreleased]
Improvements:
- Add a ErrorKind variant for the "M_WRONG_ROOM_KEYS_VERSION" Matrix error.
# 0.17.0
Breaking changes:

View File

@ -181,6 +181,12 @@ pub enum ErrorKind {
/// M_CONNECTION_TIMEOUT
ConnectionTimeout,
/// M_WRONG_ROOM_KEYS_VERSION
WrongRoomKeysVersion {
/// The currently active backup version.
current_version: Option<String>,
},
#[doc(hidden)]
_Custom { errcode: PrivOwnedStr, extra: Extra },
}
@ -237,6 +243,7 @@ impl AsRef<str> for ErrorKind {
Self::BadStatus { .. } => "M_BAD_STATUS",
Self::ConnectionFailed => "M_CONNECTION_FAILED",
Self::ConnectionTimeout => "M_CONNECTION_TIMEOUT",
Self::WrongRoomKeysVersion { .. } => "M_WRONG_ROOM_KEYS_VERSION",
Self::_Custom { errcode, .. } => &errcode.0,
}
}
@ -490,6 +497,7 @@ impl TryFrom<&AuthenticateError> for http::HeaderValue {
#[cfg(test)]
mod tests {
use assert_matches2::assert_matches;
use serde_json::{from_value as from_json_value, json};
use super::{ErrorKind, StandardErrorBody};
@ -506,6 +514,20 @@ mod tests {
assert_eq!(deserialized.message, "You are not authorized to ban users in this room.");
}
#[test]
fn deserialize_wrong_room_key_version() {
let deserialized: StandardErrorBody = from_json_value(json!({
"current_version": "42",
"errcode": "M_WRONG_ROOM_KEYS_VERSION",
"error": "Wrong backup version."
}))
.expect("We should be able to deserialize a wrong room keys version error");
assert_matches!(deserialized.kind, ErrorKind::WrongRoomKeysVersion { current_version });
assert_eq!(current_version.as_deref(), Some("42"));
assert_eq!(deserialized.message, "Wrong backup version.");
}
#[cfg(feature = "unstable-msc2967")]
#[test]
fn custom_authenticate_error_sanity() {
@ -537,7 +559,6 @@ mod tests {
#[cfg(feature = "unstable-msc2967")]
#[test]
fn deserialize_insufficient_scope() {
use assert_matches2::assert_matches;
use ruma_common::api::EndpointError;
use super::{AuthenticateError, Error, ErrorBody};

View File

@ -24,6 +24,7 @@ enum Field<'de> {
AdminContact,
Status,
Body,
CurrentVersion,
Other(Cow<'de, str>),
}
@ -37,6 +38,7 @@ impl<'de> Field<'de> {
"admin_contact" => Self::AdminContact,
"status" => Self::Status,
"body" => Self::Body,
"current_version" => Self::CurrentVersion,
_ => Self::Other(s),
}
}
@ -102,6 +104,7 @@ impl<'de> Visitor<'de> for ErrorKindVisitor {
let mut admin_contact = None;
let mut status = None;
let mut body = None;
let mut current_version = None;
let mut extra = BTreeMap::new();
macro_rules! set_field {
@ -126,6 +129,7 @@ impl<'de> Visitor<'de> for ErrorKindVisitor {
(@variant_containing admin_contact) => { ErrCode::ResourceLimitExceeded };
(@variant_containing status) => { ErrCode::BadStatus };
(@variant_containing body) => { ErrCode::BadStatus };
(@variant_containing current_version) => { ErrCode::WrongRoomKeysVersion };
(@inner $field:ident) => {
{
if $field.is_some() {
@ -145,6 +149,7 @@ impl<'de> Visitor<'de> for ErrorKindVisitor {
Field::AdminContact => set_field!(admin_contact),
Field::Status => set_field!(status),
Field::Body => set_field!(body),
Field::CurrentVersion => set_field!(current_version),
Field::Other(other) => match extra.entry(other.into_owned()) {
Entry::Vacant(v) => {
v.insert(map.next_value()?);
@ -238,6 +243,12 @@ impl<'de> Visitor<'de> for ErrorKindVisitor {
},
ErrCode::ConnectionFailed => ErrorKind::ConnectionFailed,
ErrCode::ConnectionTimeout => ErrorKind::ConnectionTimeout,
ErrCode::WrongRoomKeysVersion => ErrorKind::WrongRoomKeysVersion {
current_version: from_json_value(
current_version.ok_or_else(|| de::Error::missing_field("current_version"))?,
)
.map_err(de::Error::custom)?,
},
ErrCode::_Custom(errcode) => ErrorKind::_Custom { errcode, extra },
})
}
@ -293,6 +304,7 @@ enum ErrCode {
BadStatus,
ConnectionFailed,
ConnectionTimeout,
WrongRoomKeysVersion,
_Custom(PrivOwnedStr),
}