p2panda_rs/document/
error.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3//! Error types for creating or materializing documents and document views and validating the
4//! format of document ids and document view ids.
5use thiserror::Error;
6
7use crate::operation::OperationId;
8
9/// Error types for methods of `DocumentBuilder` struct.
10#[derive(Error, Debug)]
11pub enum DocumentBuilderError {
12    /// To resolve a document the schema must be set.
13    #[error("Schema must be set")]
14    SchemaMustBeSet,
15
16    /// An operation with invalid id or previous operations was added to the document.
17    #[error("operation {0} cannot be connected to the document graph")]
18    InvalidOperationLink(OperationId),
19
20    /// A document can only contain one CREATE operation.
21    #[error("multiple CREATE operations found when building operation graph")]
22    MultipleCreateOperations,
23
24    /// Handle errors from validating CBOR schemas.
25    #[error(transparent)]
26    DocumentViewError(#[from] DocumentViewError),
27
28    /// Handle errors when sorting the graph.
29    #[error(transparent)]
30    GraphSortingError(#[from] crate::graph::error::GraphError),
31
32    /// Handle errors from DocumentReducer.
33    #[error(transparent)]
34    DocumentReducerError(#[from] DocumentReducerError),
35}
36
37/// Error types for methods of `Document` struct.
38#[derive(Error, Debug)]
39pub enum DocumentError {
40    /// Operation passed to commit does not refer to this documents current view.
41    #[error("operation {0} does not update the documents current view")]
42    PreviousDoesNotMatch(OperationId),
43
44    /// Operation passed to commit does not the same schema as the document.
45    #[error("Operation {0} does not match the documents schema")]
46    InvalidSchemaId(OperationId),
47
48    /// Cannot perform a commit on a deleted document.
49    #[error("Cannot perform a commit on a deleted document")]
50    UpdateOnDeleted,
51
52    /// Cannot perform a commit with a create operation.
53    #[error("Cannot update an existing document with a create operation")]
54    CommitCreate,
55
56    /// Handle errors coming from DocumentView.
57    #[error(transparent)]
58    DocumentViewError(#[from] DocumentViewError),
59
60    /// Handle errors when sorting the graph.
61    #[error(transparent)]
62    GraphSortingError(#[from] crate::graph::error::GraphError),
63}
64
65/// Error types for methods of `Document` struct.
66#[derive(Error, Debug)]
67pub enum DocumentReducerError {
68    /// The first operation of a document must be a CREATE.
69    #[error("The first operation of a document must be a CREATE")]
70    FirstOperationNotCreate,
71
72    /// Handle errors from Document.
73    #[error(transparent)]
74    DocumentError(#[from] DocumentError),
75}
76
77/// Custom error types for `DocumentView`.
78#[derive(Error, Debug)]
79#[allow(missing_copy_implementations)]
80pub enum DocumentViewError {
81    /// TryFrom operation must be CREATE.
82    #[error("operation must be instantiated from a CREATE operation")]
83    NotCreateOperation,
84
85    /// Operation passed to `update()` must be UPDATE or DELETE.
86    #[error("operation passed to update() must be UPDATE or DELETE")]
87    NotUpdateOrDeleteOperation,
88}
89
90/// Error types for `DocumentViewId`.
91#[derive(Error, Debug)]
92pub enum DocumentViewIdError {
93    /// Document view ids must contain sorted operation ids.
94    #[error("expected sorted operation ids in document view id")]
95    UnsortedOperationIds,
96
97    /// Document view ids must contain at least one operation ids.
98    #[error("expected one or more operation ids")]
99    ZeroOperationIds,
100
101    /// Handle errors from validating operation id hashes.
102    #[error(transparent)]
103    InvalidOperationId(#[from] crate::operation::error::OperationIdError),
104}
105
106/// Error types for `DocumentId`.
107#[derive(Error, Debug)]
108pub enum DocumentIdError {
109    /// Handle errors from validating operation ids.
110    #[error(transparent)]
111    OperationIdError(#[from] crate::operation::error::OperationIdError),
112}