p2panda_rs/operation/
error.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3//! Error types for encoding, decoding and validating operations with schemas and regarding data
4//! types like operation fields, relations or plain operations.
5use thiserror::Error;
6
7/// Errors from `OperationBuilder` struct.
8#[derive(Error, Debug)]
9pub enum OperationBuilderError {
10    /// Handle errors from `operation::validate` module.
11    #[error(transparent)]
12    ValidateOperationError(#[from] ValidateOperationError),
13}
14
15/// Errors from `operation::encode` module.
16#[derive(Error, Debug)]
17pub enum EncodeOperationError {
18    /// CBOR encoder failed critically due to an IO issue.
19    #[error("cbor encoder failed {0}")]
20    EncoderIOFailed(String),
21
22    /// CBOR encoder could not serialize this value.
23    #[error("cbor encoder failed serializing value {0}")]
24    EncoderFailed(String),
25}
26
27/// Errors from `operation::decode` module.
28#[derive(Error, Debug)]
29pub enum DecodeOperationError {
30    /// CBOR decoder failed critically due to an IO issue.
31    #[error("cbor decoder failed {0}")]
32    DecoderIOFailed(String),
33
34    /// Invalid CBOR encoding detected.
35    #[error("invalid cbor encoding at byte {0}")]
36    InvalidCBOREncoding(usize),
37
38    /// Invalid p2panda operation encoding detected.
39    #[error("{0}")]
40    InvalidEncoding(String),
41
42    /// CBOR decoder exceeded maximum recursion limit.
43    #[error("cbor decoder exceeded recursion limit")]
44    RecursionLimitExceeded,
45}
46
47/// Errors from `operation::validate` module.
48#[derive(Error, Debug)]
49pub enum ValidateOperationError {
50    /// Claimed schema id did not match given schema.
51    #[error("operation schema id not matching with given schema: {0}, expected: {1}")]
52    SchemaNotMatching(String, String),
53
54    /// Expected `fields` in CREATE or UPDATE operation.
55    #[error("expected 'fields' in CREATE or UPDATE operation")]
56    ExpectedFields,
57
58    /// Unexpected `fields` in DELETE operation.
59    #[error("unexpected 'fields' in DELETE operation")]
60    UnexpectedFields,
61
62    /// Expected `previous` in UPDATE or DELETE operation.
63    #[error("expected 'previous' in UPDATE or DELETE operation")]
64    ExpectedPreviousOperations,
65
66    /// Unexpected `previous` in CREATE operation.
67    #[error("unexpected 'previous' in CREATE operation")]
68    UnexpectedPreviousOperations,
69
70    /// Handle errors from `schema::validate` module.
71    #[error(transparent)]
72    SchemaValidation(#[from] crate::schema::validate::error::ValidationError),
73
74    /// Handle errors from `entry::validate` module.
75    #[error(transparent)]
76    ValidateEntryError(#[from] crate::entry::error::ValidateEntryError),
77}
78
79/// Error types for methods of plain fields or operation fields.
80#[derive(Error, Debug)]
81pub enum FieldsError {
82    /// Detected duplicate field when adding a new one.
83    #[error("field '{0}' already exists")]
84    FieldDuplicate(String),
85
86    /// Tried to interact with an unknown field.
87    #[error("field does not exist")]
88    UnknownField,
89}
90
91/// Errors from `OperationId` struct.
92#[derive(Error, Debug)]
93pub enum OperationIdError {
94    /// Handle errors from `Hash` struct.
95    #[error(transparent)]
96    HashError(#[from] crate::hash::error::HashError),
97}
98
99/// Errors from `PlainValue` enum.
100#[derive(Error, Debug)]
101pub enum PlainValueError {
102    /// Error resulting from failure to parsing a byte string into a String.
103    #[error("attempted to parse non-utf8 bytes into string")]
104    BytesNotUtf8,
105
106    /// Handle errors when converting from an integer.
107    #[error(transparent)]
108    IntError(#[from] std::num::TryFromIntError),
109
110    /// Tried to parse a PlainValue from an unsupported cbor value.
111    #[error("data did not match any variant of untagged enum PlainValue")]
112    UnsupportedValue,
113}
114
115/// Errors from `Relation` struct.
116#[derive(Error, Debug)]
117pub enum RelationError {
118    /// Handle errors from `DocumentId` struct.
119    #[error(transparent)]
120    DocumentIdError(#[from] crate::document::error::DocumentIdError),
121}
122
123/// Errors from `PinnedRelation` struct.
124#[derive(Error, Debug)]
125pub enum PinnedRelationError {
126    /// Handle errors from `DocumentViewId` struct.
127    #[error(transparent)]
128    DocumentViewIdError(#[from] crate::document::error::DocumentViewIdError),
129}
130
131/// Errors from `RelationList` struct.
132#[derive(Error, Debug)]
133pub enum RelationListError {
134    /// Handle errors from `DocumentId` struct.
135    #[error(transparent)]
136    DocumentIdError(#[from] crate::document::error::DocumentIdError),
137}
138
139/// Errors from `PinnedRelationList` struct.
140#[derive(Error, Debug)]
141pub enum PinnedRelationListError {
142    /// Handle errors from `DocumentViewId` struct.
143    #[error(transparent)]
144    DocumentViewIdError(#[from] crate::document::error::DocumentViewIdError),
145}
146
147/// Errors from `OperationAction` enum.
148#[derive(Error, Debug)]
149#[allow(missing_copy_implementations)]
150pub enum OperationActionError {
151    /// Passed unknown operation action value.
152    #[error("unknown operation action {0}")]
153    UnknownAction(u64),
154}