Skip to main content

arminius_lib_events/
errors.rs

1// SPDX-FileCopyrightText: 2025 BWI GmbH
2//
3// SPDX-License-Identifier: Apache-2.0
4
5//! Error types for event validation, signing, and conversion operations.
6
7use ruma::RoomVersionId;
8
9/// Error when generating a stripped state event, but it's not a state event
10#[derive(Debug, thiserror::Error, PartialEq, Eq)]
11#[error("Tried to generate a stripped state event for a non-state event")]
12pub struct EventIsNotStateEvent;
13
14/// Error when encountering an unknown room version
15#[derive(Debug, thiserror::Error, Eq, PartialEq)]
16#[error("Unknown room version {0}")]
17pub struct UnknownRoomVersionError(pub RoomVersionId);
18
19/// Error when encountering an unknown room version when parsing from string
20#[derive(Debug, thiserror::Error, Eq, PartialEq)]
21#[error("Unknown room version {0}")]
22pub struct UnknownRoomVersionStringError(pub String);
23
24/// Error when deserializing an event from a PDU
25#[derive(Debug, thiserror::Error)]
26pub enum FromPduError {
27	/// Could not deserialize the PDU
28	#[error("Error deserializing database event: {0}")]
29	DeserializationError(#[from] serde_json::Error),
30	/// Error in building the contextualized object
31	#[error(transparent)]
32	EventContextualizationError(#[from] EventContextualizationError),
33}
34
35#[expect(missing_docs, reason = "The error messages and names is documentation enough here")]
36#[derive(Debug, thiserror::Error)]
37pub enum EventContextualizationError {
38	#[error("Tried to create an event with more than 20 prev_events, but at most 20 are allowed")]
39	TooManyPrevEvents,
40	#[error("Tried to create an event with more than 10 auth_events, but at most 10 are allowed")]
41	TooManyAuthEvents,
42	#[error(transparent)]
43	MismatchedEventRefsError(#[from] MismatchedEventRefsError),
44	/// Could not generate an ID for the PDU
45	#[error("Error generating event ID: {0}")]
46	EventIdGenerationError(#[from] EventIdGenerationError),
47}
48
49/// Error when mismatched event refs are provided
50#[expect(missing_docs, reason = "The error messages and names is documentation enough here")]
51#[derive(Debug, thiserror::Error)]
52pub enum MismatchedEventRefsError {
53	#[error("Invalid event refs: Expected V1 event refs, got V2")]
54	ExpectedV1GotV2,
55	#[error("Invalid event refs: Expected V2 event refs, got V1")]
56	ExpectedV2GotV1,
57}
58
59/// Error for when we can't generate a valid event
60#[expect(missing_docs, reason = "The error messages and names is documentation enough here")]
61#[derive(Debug, thiserror::Error)]
62#[error("Could not generate event ID: {0}")]
63pub enum EventIdGenerationError {
64	#[error("Tried generating a V1 event ID, but no servername provided")]
65	V1MissingServerName,
66	#[error("Tried generating a servername for an invalid event")]
67	InvalidEvent(String),
68}
69
70/// Errors that can occur when checking authorization.
71#[derive(Debug, thiserror::Error)]
72pub enum AuthorizationError {
73	/// Error converting event to PDU
74	#[error("Could not convert event to PDU for auth checks: {0}")]
75	EventConversionError(#[from] EventConversionError),
76	/// Authorization check failed
77	#[error("Authorization check failed: {0}")]
78	AuthorizationFailed(String),
79}
80
81/// Error type for validation failures.
82#[derive(Debug, thiserror::Error)]
83pub enum ValidationError {
84	/// The signature is invalid
85	#[error("Invalid signature: {0}")]
86	SignatureValidation(String),
87	/// Building canonical JSON failed
88	#[error("JSON canonicalization failed: {0}")]
89	JsonCanonicalization(String),
90	/// An error in handling JSON De-/serialization.
91	#[error(transparent)]
92	JsonSerDe(#[from] JsonSerDeError),
93	/// Checking authorization failed
94	#[error(transparent)]
95	AuthorizationError(#[from] AuthorizationError),
96	/// Error when attempting to perform redaction operations or operations on
97	/// redacted data.
98	#[error("An error occurred when handling redacted data: {0}")]
99	Redaction(#[from] RedactionError),
100}
101
102/// Represents an error occurring when attempting to handle redactions or
103/// redacted data.
104#[derive(Debug, thiserror::Error)]
105pub enum RedactionError {
106	/// A `ruma` redaction error.
107	#[error(transparent)]
108	Ruma(#[from] ruma::canonical_json::CanonicalJsonFieldError),
109	/// Expected a "content" field, but it was missing after the event has been
110	/// redacted.
111	#[error("Missing \"content\" field after redaction")]
112	MissingContent,
113}
114
115/// Error type for signing failures.
116#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
117pub enum SigningError {
118	/// Building canonical JSON failed
119	#[error("JSON canonicalization failed: {0}")]
120	JsonCanonicalization(String),
121	/// Signature creation failed
122	#[error("Signature error: {0}")]
123	SignatureCreation(String),
124	/// Content format error
125	#[error("Invalid content: {0}")]
126	InvalidContent(String),
127}
128
129/// Error type for the [`Contextualized`](crate::Contextualized) →
130/// [`Verified`](crate::Verified) state transition.
131#[derive(Debug, thiserror::Error)]
132pub enum SigningValidationError {
133	/// Signing the event failed
134	#[error(transparent)]
135	SigningError(#[from] SigningError),
136	/// Validating the event failed
137	#[error(transparent)]
138	ValidationError(#[from] ValidationError),
139}
140
141/// Type representing an error in handling JSON De-/serialization
142#[derive(Debug, thiserror::Error)]
143pub enum JsonSerDeError {
144	/// Content could not be deserialized from JSON.
145	#[error("Content deserialization error: {0}")]
146	ContentDeserialization(serde_json::Error),
147	/// JSON serialization failed.
148	#[error("JSON serialization error: {0}")]
149	JsonSerialization(serde_json::Error),
150}
151
152impl JsonSerDeError {
153	/// Shorthand to create [`Self::ContentDeserialization`] with a `&str`
154	/// message, using `serde_json::Error::custom(msg)`.
155	#[must_use]
156	pub fn custom_deserialization(msg: &str) -> Self {
157		Self::ContentDeserialization(<serde_json::Error as serde::de::Error>::custom(msg))
158	}
159}
160
161/// Error type for failures during event serialization or format conversion.
162#[derive(Debug, thiserror::Error)]
163pub enum EventConversionError {
164	/// An error in handling JSON De-/serialization.
165	#[error(transparent)]
166	Json(#[from] JsonSerDeError),
167}