1use ruma_common::{
2 IdParseError,
3 canonical_json::{CanonicalJsonType, RedactionError},
4 serde::Base64DecodeError,
5};
6use thiserror::Error;
7
8use crate::Ed25519VerificationError;
9
10#[derive(Debug, Error)]
12#[non_exhaustive]
13pub enum JsonError {
14 #[error("PDU is larger than maximum of 65535 bytes")]
16 PduTooLarge,
17
18 #[error("invalid type at `{path}`: expected {expected:?}, found {found:?}")]
20 InvalidType {
21 path: String,
23
24 expected: CanonicalJsonType,
26
27 found: CanonicalJsonType,
29 },
30
31 #[error("missing field: `{path}`")]
33 MissingField {
34 path: String,
36 },
37
38 #[error(transparent)]
40 Serde(#[from] serde_json::Error),
41}
42
43impl From<RedactionError> for JsonError {
44 fn from(err: RedactionError) -> Self {
45 match err {
46 RedactionError::InvalidType { path, expected, found } => {
47 JsonError::InvalidType { path, expected, found }
48 }
49 RedactionError::MissingField { path } => JsonError::MissingField { path },
50 #[allow(unreachable_patterns)]
51 _ => unreachable!(),
52 }
53 }
54}
55
56#[derive(Debug, Error)]
58#[non_exhaustive]
59pub enum VerificationError {
60 #[error("Invalid JSON: {0}")]
62 Json(#[from] JsonError),
63
64 #[error("Could not parse base64-encoded signature at `path`: {source}")]
66 InvalidBase64Signature {
67 path: String,
69
70 #[source]
72 source: Base64DecodeError,
73 },
74
75 #[error("Could not parse {identifier_type}: {source}")]
77 ParseIdentifier {
78 identifier_type: &'static str,
80
81 #[source]
83 source: IdParseError,
84 },
85
86 #[error("signature uses an unsupported algorithm")]
88 UnsupportedAlgorithm,
89
90 #[error("Could not find signatures for entity {0:?}")]
92 NoSignaturesForEntity(String),
93
94 #[error("Could not find public keys for entity {0:?}")]
96 NoPublicKeysForEntity(String),
97
98 #[error("Could not find supported signature for entity {0:?}")]
100 NoSupportedSignatureForEntity(String),
101
102 #[error(transparent)]
104 Ed25519(#[from] Ed25519VerificationError),
105}