96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1 | use thiserror::Error; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3 | use matrix_sdk::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4 | HttpError, RefreshTokenError, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 5 | authentication::oauth::error::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 6 | OAuthAuthorizationCodeError, OAuthDiscoveryError, OAuthError, OAuthTokenRevocationError, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 7 | RequestTokenError, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 8 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | encryption::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | identities::{ManualVerifyError, RequestVerificationError}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | recovery::RecoveryError, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | secret_storage::{ImportError, SecretStorageError}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | use crate::CommandErrorKind; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | #[derive(Debug, Error)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | #[error("{message}")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | pub(crate) struct CommandError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | pub(crate) kind: CommandErrorKind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | message: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | #[source] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | source: Option<Box<dyn std::error::Error + Send + Sync>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | impl CommandError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | pub(crate) fn new(kind: CommandErrorKind, message: impl Into<String>) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | message: message.into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | source: None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | pub(crate) fn with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | kind: CommandErrorKind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | message: impl Into<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | source: impl std::error::Error + Send + Sync + 'static, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | ) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | message: message.into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | source: Some(Box::new(source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 47 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | pub(crate) fn from_sdk(message: impl Into<String>, source: matrix_sdk::Error) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | Self::with_source(classify_sdk_error(&source), message, source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | pub(crate) fn from_http(message: impl Into<String>, source: HttpError) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | Self::with_source(classify_http_error(&source), message, source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | pub(crate) fn from_refresh_token( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | message: impl Into<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | source: RefreshTokenError, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | ) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | Self::with_source(classify_refresh_token_error(&source), message, source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 60 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | pub(crate) fn from_recovery(message: impl Into<String>, source: RecoveryError) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | let kind = match &source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | RecoveryError::Sdk(source) => classify_sdk_error(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | RecoveryError::SecretStorage(SecretStorageError::Sdk(source)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | classify_sdk_error(source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 68 | RecoveryError::SecretStorage(SecretStorageError::ImportError { error, .. }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 69 | match error { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | ImportError::Sdk(source) => classify_sdk_error(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | _ => CommandErrorKind::RecoveryRejected, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | RecoveryError::SecretStorage(SecretStorageError::Verification(source)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | match source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | ManualVerifyError::Http(source) => classify_http_error(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 77 | _ => CommandErrorKind::RecoveryRejected, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 79 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 80 | RecoveryError::SecretStorage(SecretStorageError::SecretStorageKey(_)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 81 | CommandErrorKind::RecoveryRejected |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 82 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 83 | RecoveryError::SecretStorage(SecretStorageError::Storage(_)) => CommandErrorKind::Store, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 84 | RecoveryError::BackupExistsOnServer | RecoveryError::SecretStorage(_) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 85 | CommandErrorKind::RecoveryRejected |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 86 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 87 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 88 | Self::with_source(kind, message, source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 89 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 90 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 91 | pub(crate) fn from_verification_request( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 92 | message: impl Into<String>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 93 | source: RequestVerificationError, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 94 | ) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 95 | let kind = match &source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 96 | RequestVerificationError::Sdk(source) => classify_sdk_error(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 97 | RequestVerificationError::RoomCreation(_) => CommandErrorKind::Encryption, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 98 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 99 | Self::with_source(kind, message, source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 100 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 101 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 102 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 103 | pub(crate) fn classify_sdk_error(source: &matrix_sdk::Error) -> CommandErrorKind { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 104 | match source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 105 | matrix_sdk::Error::Http(source) => classify_http_error(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 106 | matrix_sdk::Error::OAuth(source) => classify_oauth_error(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 107 | matrix_sdk::Error::AuthenticationRequired => CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 108 | matrix_sdk::Error::BadCryptoStoreState |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 109 | | matrix_sdk::Error::CryptoStoreError(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | | matrix_sdk::Error::CrossProcessLockError(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | | matrix_sdk::Error::StateStore(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 112 | | matrix_sdk::Error::EventCacheStore(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 113 | | matrix_sdk::Error::MediaStore(_) => CommandErrorKind::Store, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 114 | matrix_sdk::Error::NoOlmMachine |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 115 | | matrix_sdk::Error::OlmError(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 116 | | matrix_sdk::Error::MegolmError(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 117 | | matrix_sdk::Error::DecryptorError(_) => CommandErrorKind::Encryption, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 118 | matrix_sdk::Error::Media(_) => CommandErrorKind::MediaUnavailable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 119 | matrix_sdk::Error::Timeout => CommandErrorKind::Network, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 120 | _ => CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | fn classify_http_error(source: &HttpError) -> CommandErrorKind { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | match source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | HttpError::Reqwest(_) => CommandErrorKind::Network, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | HttpError::Api(_) => CommandErrorKind::Server, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | HttpError::RefreshToken(source) => classify_refresh_token_error(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | HttpError::Cached(source) => classify_http_error(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | _ => CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | fn classify_refresh_token_error(source: &RefreshTokenError) -> CommandErrorKind { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | match source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | RefreshTokenError::RefreshTokenRequired => CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | RefreshTokenError::MatrixAuth(source) => classify_http_error(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | RefreshTokenError::OAuth(source) => classify_oauth_error(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | fn classify_oauth_error(source: &OAuthError) -> CommandErrorKind { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | let classify_token_request_error = |source: &_| match source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | RequestTokenError::Request(_) => CommandErrorKind::Network, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | RequestTokenError::ServerResponse(_) => CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | _ => CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | match source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | OAuthError::Discovery(source) => match source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 150 | OAuthDiscoveryError::Http(source) => classify_http_error(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | OAuthDiscoveryError::Oidc(source) => classify_token_request_error(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | _ => CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 154 | OAuthError::RefreshToken(source) => classify_token_request_error(source), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 155 | OAuthError::AuthorizationCode(OAuthAuthorizationCodeError::RequestToken(source)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | classify_token_request_error(source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 157 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | OAuthError::AuthorizationCode(_) => CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | OAuthError::Logout(source) => match source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | OAuthTokenRevocationError::Revoke(RequestTokenError::Request(_)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | CommandErrorKind::Network |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | OAuthTokenRevocationError::Revoke(RequestTokenError::ServerResponse(_)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | CommandErrorKind::Unauthenticated |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | _ => CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 167 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 168 | _ => CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 169 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 170 | } |