|
1 | 1 | //! Errors related to room event decryption.
|
2 | 2 |
|
3 | 3 | use js_sys::JsString;
|
4 |
| -use matrix_sdk_common::deserialized_responses::VerificationLevel; |
| 4 | +use matrix_sdk_common::deserialized_responses::{VerificationLevel, WithheldCode}; |
5 | 5 | use matrix_sdk_crypto::{vodozemac, MegolmError};
|
6 | 6 | use wasm_bindgen::prelude::wasm_bindgen;
|
7 | 7 |
|
@@ -55,52 +55,34 @@ impl MegolmDecryptionError {
|
55 | 55 |
|
56 | 56 | impl From<MegolmError> for MegolmDecryptionError {
|
57 | 57 | fn from(value: MegolmError) -> Self {
|
| 58 | + let decryption_error = |code: DecryptionErrorCode, |
| 59 | + maybe_withheld: Option<&WithheldCode>| |
| 60 | + -> MegolmDecryptionError { |
| 61 | + let description = value.to_string().into(); |
| 62 | + let maybe_withheld = maybe_withheld.map(|code| code.to_string().to_owned().into()); |
| 63 | + MegolmDecryptionError { code, description, maybe_withheld } |
| 64 | + }; |
| 65 | + |
58 | 66 | match &value {
|
59 |
| - MegolmError::MissingRoomKey(withheld_code) => MegolmDecryptionError { |
60 |
| - code: DecryptionErrorCode::MissingRoomKey, |
61 |
| - description: value.to_string().into(), |
62 |
| - maybe_withheld: withheld_code |
63 |
| - .as_ref() |
64 |
| - .map(|code| code.to_string().to_owned().into()), |
65 |
| - }, |
| 67 | + MegolmError::MissingRoomKey(withheld_code) => { |
| 68 | + decryption_error(DecryptionErrorCode::MissingRoomKey, withheld_code.as_ref()) |
| 69 | + } |
66 | 70 | MegolmError::Decryption(vodozemac::megolm::DecryptionError::UnknownMessageIndex(
|
67 | 71 | ..,
|
68 |
| - )) => MegolmDecryptionError { |
69 |
| - code: DecryptionErrorCode::UnknownMessageIndex, |
70 |
| - description: value.to_string().into(), |
71 |
| - maybe_withheld: None, |
72 |
| - }, |
73 |
| - MegolmError::MismatchedIdentityKeys { .. } => MegolmDecryptionError { |
74 |
| - code: DecryptionErrorCode::UnknownMessageIndex, |
75 |
| - description: value.to_string().into(), |
76 |
| - maybe_withheld: None, |
77 |
| - }, |
| 72 | + )) => decryption_error(DecryptionErrorCode::UnknownMessageIndex, None), |
| 73 | + MegolmError::MismatchedIdentityKeys { .. } => { |
| 74 | + decryption_error(DecryptionErrorCode::UnknownMessageIndex, None) |
| 75 | + } |
78 | 76 | MegolmError::SenderIdentityNotTrusted(VerificationLevel::VerificationViolation) => {
|
79 |
| - MegolmDecryptionError { |
80 |
| - code: DecryptionErrorCode::SenderIdentityVerificationViolation, |
81 |
| - description: value.to_string().into(), |
82 |
| - maybe_withheld: None, |
83 |
| - } |
| 77 | + decryption_error(DecryptionErrorCode::SenderIdentityVerificationViolation, None) |
84 | 78 | }
|
85 | 79 | MegolmError::SenderIdentityNotTrusted(VerificationLevel::UnsignedDevice) => {
|
86 |
| - MegolmDecryptionError { |
87 |
| - code: DecryptionErrorCode::UnsignedSenderDevice, |
88 |
| - description: value.to_string().into(), |
89 |
| - maybe_withheld: None, |
90 |
| - } |
| 80 | + decryption_error(DecryptionErrorCode::UnsignedSenderDevice, None) |
91 | 81 | }
|
92 | 82 | MegolmError::SenderIdentityNotTrusted(VerificationLevel::None(..)) => {
|
93 |
| - MegolmDecryptionError { |
94 |
| - code: DecryptionErrorCode::UnknownSenderDevice, |
95 |
| - description: value.to_string().into(), |
96 |
| - maybe_withheld: None, |
97 |
| - } |
| 83 | + decryption_error(DecryptionErrorCode::UnknownSenderDevice, None) |
98 | 84 | }
|
99 |
| - _ => MegolmDecryptionError { |
100 |
| - code: DecryptionErrorCode::UnableToDecrypt, |
101 |
| - description: value.to_string().into(), |
102 |
| - maybe_withheld: None, |
103 |
| - }, |
| 85 | + _ => decryption_error(DecryptionErrorCode::UnableToDecrypt, None), |
104 | 86 | }
|
105 | 87 | }
|
106 | 88 | }
|
0 commit comments