client-api: Make Error non-exhaustive
This commit is contained in:
parent
c5f8137ba9
commit
4f4085a013
@ -13,6 +13,7 @@ Breaking changes:
|
||||
- The `ts` field in `Request` for `get_media_preview` is now `Option`.
|
||||
- The query parameter of `check_registration_token_validity` endpoint
|
||||
has been renamed from `registration_token` to `token`
|
||||
- `Error` is now non-exhaustive.
|
||||
|
||||
Improvements:
|
||||
|
||||
|
@ -298,7 +298,7 @@ pub struct StandardErrorBody {
|
||||
|
||||
/// A Matrix Error
|
||||
#[derive(Debug, Clone)]
|
||||
#[allow(clippy::exhaustive_structs)]
|
||||
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
||||
pub struct Error {
|
||||
/// The http status code.
|
||||
pub status_code: http::StatusCode,
|
||||
@ -312,6 +312,18 @@ pub struct Error {
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Constructs a new `Error` with the given status code and body.
|
||||
///
|
||||
/// This is equivalent to calling `body.into_error(status_code)`.
|
||||
pub fn new(status_code: http::StatusCode, body: ErrorBody) -> Self {
|
||||
Self {
|
||||
status_code,
|
||||
#[cfg(feature = "unstable-msc2967")]
|
||||
authenticate: None,
|
||||
body,
|
||||
}
|
||||
}
|
||||
|
||||
/// If `self` is a server error in the `errcode` + `error` format expected
|
||||
/// for client-server API endpoints, returns the error kind (`errcode`).
|
||||
pub fn error_kind(&self) -> Option<&ErrorKind> {
|
||||
@ -368,6 +380,8 @@ impl std::error::Error for Error {}
|
||||
|
||||
impl ErrorBody {
|
||||
/// Convert the ErrorBody into an Error by adding the http status code.
|
||||
///
|
||||
/// This is equivalent to calling `Error::new(status_code, self)`.
|
||||
pub fn into_error(self, status_code: http::StatusCode) -> Error {
|
||||
Error {
|
||||
status_code,
|
||||
|
Loading…
x
Reference in New Issue
Block a user