1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use ed25519_dalek::ed25519;
use std::convert::Infallible;
use thiserror::Error;

use crate::governance::error::RequestError;

#[derive(Error, Debug)]
pub enum Error {
    #[error("Schema Creation Error")]
    SchemaCreationError,
    #[error("Errors that can never happen")]
    InfalibleError {
        #[from]
        source: Infallible,
    },

    #[error("Unknown error: `{0}`")]
    UnknownError(String),

    #[error("Verification error: {0}")]
    VerificationError(String),

    #[error("`{0}`")]
    PayloadError(String),

    #[error("Base58 Decoding error")]
    Base64DecodingError {
        #[from]
        source: base64::DecodeError,
    },

    #[error("Ed25519 error")]
    Ed25519Error {
        #[from]
        source: ed25519::Error,
    },

    #[error("Serde JSON error")]
    SerdeJson {
        #[from]
        source: serde_json::Error,
    },

    #[error("MessagePack serialize error")]
    MsgPackSerialize {
        #[from]
        source: rmp_serde::encode::Error,
    },

    #[error("MessagePack deserialize error")]
    MsgPackDeserialize {
        #[from]
        source: rmp_serde::decode::Error,
    },

    #[error("Event error: {0}")]
    EventError(String),

    #[error("Seed error: {0}")]
    SeedError(String),

    #[error("Semantic error: {0}")]
    SemanticError(String),

    #[error("Invalid identifier: {0}")]
    InvalidIdentifier(String),

    #[error("Sign error: {0}")]
    SignError(String),

    #[error("No signature error: {0}")]
    NoSignatureError(String),

    #[error("Key pair error: {0}")]
    KeyPairError(String),

    #[error("TAPLE error: {0}")]
    TapleError(String),

    #[error("Store error: {0}")]
    StoreError(String),

    #[error("Duplicate Event")]
    DuplicateEvent,

    #[error("Event out of order")]
    OutOfOrder,

    #[error("Schema not found")]
    SchemaNotFoundError,

    #[error("Subject not found")]
    SubjectNotFoundError,

    #[error("Memory Database write fails")]
    MemoryDBWriteFailed,

    #[error("Serialization with Borsh fails")]
    BorshSerializationFailed,
}

#[derive(Error, Debug, Clone, PartialEq)]
pub enum ChannelErrors {
    #[error("Channel is closed at the other end. Cannot send data")]
    ChannelClosed,
    #[error("Consumer queue is full.")]
    FullQueue,
}

#[derive(Error, Debug, PartialEq, Clone)]
pub enum SubjectError {
    #[error("Event request type is not Create")]
    NotCreateEvent,
    #[error("Event request type is not State")]
    NotStateEvent,
    #[error("An event is already created waiting to get quorum")]
    EventAlreadyProcessing,
    #[error("An event which is already applied is not in the database")]
    EventAlreadyAppliedNotFound,
    #[error("Event SN is not 0")]
    SnNot0,
    #[error("Event sourcing is not in order")]
    EventSourcingNotInOrder(u64, u64),
    #[error("Hash of Subject_data after apply does not match to event subject_data_hash")]
    EventSourcingHashNotEqual,
    #[error("Applying to Subject without data")]
    ApplyInEmptySubject,
    #[error("Subject Not Found")]
    SubjectNotFound,
    #[error("We are not the owner of the subject")]
    NotOwnerOfSubject,
    #[error("Event Content failed at serialization")]
    EventContentSerializationFailed,
    #[error("Subject Signature Failed")]
    SubjectSignatureFailed,
    #[error("Subject has no data")]
    SubjectHasNoData,
    #[error("Delete Signatures Failed")]
    DeleteSignaturesFailed,
    #[error("Schema Validation Failed")]
    SchemaValidationFailed,
    #[error("Schema does not compile")]
    SchemaDoesNotCompile,
    #[error("Error in criptography")]
    CryptoError(String),
    #[error("InvalidPayload {0}")]
    InvalidPayload(String),
    #[error("Error parsing json string")]
    ErrorParsingJsonString(String),
    #[error("Error applying patch")]
    ErrorApplyingPatch(String),
    #[error("Duplicated schema or member")]
    DuplicatedSchemaOrMember,
    #[error("Policies Missing for Some Schema")]
    PoliciesMissing,
    #[error("Invalid Policies Id")]
    InvalidPoliciesId,
    #[error("Invalid Member in Policies")]
    InvalidMemberInPolicies,
    #[error("Invalid member identifier {0}")]
    InvalidMemberIdentifier(String),
    #[error("JSON-PATCH on Create Event not allowed")]
    InvalidUseOfJSONPATCH,
    #[error("Approvers is not subset of validators")]
    ApproversAreNotValidators,
    #[error("Error creating subject id")]
    ErrorCreatingSubjectId,
    #[error("Signature Creation Fails: {0}")]
    SignatureCreationFails(String),
    #[error("Signature Verify Fails: {0}")]
    SignatureVerifyFails(String),
    #[error("Signature Repeated: {0}")]
    RepeatedSignature(String),
    #[error("Signers Error: {0}")]
    SignersError(String),
}

#[derive(Error, Debug, Clone, PartialEq)]
#[allow(dead_code)]
pub enum ProtocolErrors {
    #[error("Errors that can never happen")]
    InfalibleError {
        #[from]
        source: Infallible,
    },
    #[error("Secret Key not found")]
    SignatureError,
    #[error("Channel unavaible")]
    ChannelError {
        #[from]
        source: crate::commons::errors::ChannelErrors,
    },
    #[error("Oneshot channel not available")]
    OneshotUnavailable,
    #[error("Ledger response not expected")]
    UnexpectedLedgerResponse,
    // #[error("Ledger error")]
    // LedgerError {
    //     #[from]
    //     source: LedgerManagerError,
    // },
    #[error("Not validator")]
    NotValidator,
    #[error("Governance error")]
    GovernanceError {
        #[from]
        source: RequestError,
    },
}

/// Errors that can occur during the generation of a [ListenAddr].
#[derive(Error, Debug, Clone)]
pub enum ListenAddrErrors {
    #[error("Invalid ListenAddr provided. Neither a valid IP or memory multiaddress")]
    InvalidListenAddr,
    #[error("Invalid multiaddres protocol specified. Supported are IP and Memory")]
    InvalidProtocolSpecified,
    #[error("The IPv4 specified is not valid")]
    InvalidIP4,
    #[error("The IPv6 specified is not valid")]
    InvalidIP6,
    #[error("No transport protocol specified")]
    NoTransportProtocolSpecified,
    #[error("Transpor protocol must be tcp")]
    NoTCP,
    #[error("Port must be a 32 bits unsigned")]
    NoU32Port,
    #[error("An IP must be combined with a tcp port")]
    InvalidCombination,
}