forked from MettaChain/PropChain-contract
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.rs
More file actions
683 lines (638 loc) · 28.1 KB
/
Copy patherrors.rs
File metadata and controls
683 lines (638 loc) · 28.1 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
//! Shared error handling framework for PropChain contracts
//!
//! This module provides a unified error handling system with:
//! - Base error trait that all contract errors implement
//! - Common error variants reusable across contracts
//! - Numeric error codes for external API integration
//! - Full Debug, Display, and From trait implementations
//! - [`ErrorMessage`]: structured error snapshot combining code, category, message, and i18n key
//! - [`ContractError::to_error_message()`]: default method to produce an `ErrorMessage`
//! - [`ContractError::error_i18n_key()`]: default method returning a localization key
use core::fmt;
use scale::{Decode, Encode};
#[cfg(feature = "std")]
use scale_info::TypeInfo;
// =============================================================================
// Standardized Error Message
// =============================================================================
/// Structured snapshot of all error information for a single error instance.
///
/// Suitable for logging and client-side display. All string fields are `&'static str`
/// for `no_std` / no-heap compatibility. This type is not SCALE-encoded since
/// `&'static str` does not implement `Decode`; use it purely in-memory.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ErrorMessage {
/// Numeric error code, globally unique across all PropChain contracts.
pub code: u32,
/// Top-level domain that produced this error.
pub category: ErrorCategory,
/// Short human-readable message (matches `error_description`).
pub message: &'static str,
/// Longer technical description suitable for logs and developer tooling.
pub description: &'static str,
/// Dot-separated localization key for client-side message lookup.
/// Format: `"<category>.<variant_snake_case>"`, e.g. `"compliance.not_verified"`.
pub i18n_key: &'static str,
}
// =============================================================================
// Base Error Trait
// =============================================================================
/// Base trait for all PropChain contract errors.
/// All contract-specific error enums must implement this trait.
pub trait ContractError: fmt::Debug + fmt::Display + Encode + Decode {
/// Returns the numeric error code for this error variant.
/// Used for external API integration and monitoring.
fn error_code(&self) -> u32;
/// Returns a human-readable description of the error.
fn error_description(&self) -> &'static str;
/// Returns the category of this error.
fn error_category(&self) -> ErrorCategory {
match self.error_code() {
1..=999 => ErrorCategory::Common,
1000..=1999 => ErrorCategory::PropertyToken,
2000..=2999 => ErrorCategory::Escrow,
3000..=3999 => ErrorCategory::Bridge,
4000..=4999 => ErrorCategory::Oracle,
5000..=5999 => ErrorCategory::Fees,
6000..=6999 => ErrorCategory::Compliance,
7000..=7999 => ErrorCategory::Dex,
8000..=8999 => ErrorCategory::Governance,
9000..=9999 => ErrorCategory::Staking,
10000..=10999 => ErrorCategory::Monitoring,
11000..=11999 => ErrorCategory::EventBus,
_ => ErrorCategory::Unknown,
}
}
/// Returns a dot-separated localization key for client-side message lookup.
///
/// Format: `"<category>.<variant_snake_case>"`, e.g. `"compliance.not_verified"`.
/// Override this in each error type to provide a precise key.
fn error_i18n_key(&self) -> &'static str {
"unknown.error"
}
/// Constructs a complete [`ErrorMessage`] snapshot from this error.
/// No allocation is performed; all fields are `'static`.
fn to_error_message(&self) -> ErrorMessage {
ErrorMessage {
code: self.error_code(),
category: self.error_category(),
message: self.error_description(),
description: self.error_description(),
i18n_key: self.error_i18n_key(),
}
}
}
/// Error categories for classification and monitoring
#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(TypeInfo))]
pub enum ErrorCategory {
Common,
PropertyToken,
Escrow,
Bridge,
Oracle,
Fees,
Compliance,
Dex,
Governance,
Staking,
Monitoring,
EventBus,
Unknown,
}
impl fmt::Display for ErrorCategory {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorCategory::Common => write!(f, "Common"),
ErrorCategory::PropertyToken => write!(f, "PropertyToken"),
ErrorCategory::Escrow => write!(f, "Escrow"),
ErrorCategory::Bridge => write!(f, "Bridge"),
ErrorCategory::Oracle => write!(f, "Oracle"),
ErrorCategory::Fees => write!(f, "Fees"),
ErrorCategory::Compliance => write!(f, "Compliance"),
ErrorCategory::Dex => write!(f, "Dex"),
ErrorCategory::Governance => write!(f, "Governance"),
ErrorCategory::Staking => write!(f, "Staking"),
ErrorCategory::Monitoring => write!(f, "Monitoring"),
ErrorCategory::EventBus => write!(f, "EventBus"),
ErrorCategory::Unknown => write!(f, "Unknown"),
}
}
}
// =============================================================================
// Common Error Variants
// =============================================================================
/// Common error variants that can be used across multiple contracts
#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(TypeInfo))]
pub enum CommonError {
/// Unauthorized access - caller lacks required permissions
Unauthorized = 1,
/// Invalid parameters provided to function
InvalidParameters = 2,
/// Resource not found (generic)
NotFound = 3,
/// Insufficient funds or balance
InsufficientFunds = 4,
/// Operation not allowed in current state
InvalidState = 5,
/// Internal contract error
InternalError = 6,
/// Serialization/deserialization error
CodecError = 7,
/// Feature not yet implemented
NotImplemented = 8,
/// Operation timed out
Timeout = 9,
/// Duplicate operation or resource
Duplicate = 10,
}
impl fmt::Display for CommonError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CommonError::Unauthorized => {
write!(f, "Unauthorized: caller lacks required permissions")
}
CommonError::InvalidParameters => write!(f, "Invalid parameters provided to function"),
CommonError::NotFound => write!(f, "Resource not found"),
CommonError::InsufficientFunds => write!(f, "Insufficient funds or balance"),
CommonError::InvalidState => write!(f, "Operation not allowed in current state"),
CommonError::InternalError => write!(f, "Internal contract error occurred"),
CommonError::CodecError => write!(f, "Serialization/deserialization error"),
CommonError::NotImplemented => write!(f, "Feature not yet implemented"),
CommonError::Timeout => write!(f, "Operation timed out"),
CommonError::Duplicate => write!(f, "Duplicate operation or resource"),
}
}
}
impl ContractError for CommonError {
fn error_code(&self) -> u32 {
*self as u32
}
fn error_description(&self) -> &'static str {
match self {
CommonError::Unauthorized => {
"Caller does not have permission to perform this operation"
}
CommonError::InvalidParameters => "One or more function parameters are invalid",
CommonError::NotFound => "The requested resource does not exist",
CommonError::InsufficientFunds => "Account has insufficient balance for this operation",
CommonError::InvalidState => "Cannot perform this operation in the current state",
CommonError::InternalError => "An internal error occurred in the contract",
CommonError::CodecError => "Failed to encode or decode data",
CommonError::NotImplemented => "This feature is not yet implemented",
CommonError::Timeout => "The operation exceeded its time limit",
CommonError::Duplicate => "This operation or resource already exists",
}
}
fn error_category(&self) -> ErrorCategory {
ErrorCategory::Common
}
fn error_i18n_key(&self) -> &'static str {
match self {
CommonError::Unauthorized => "common.unauthorized",
CommonError::InvalidParameters => "common.invalid_parameters",
CommonError::NotFound => "common.not_found",
CommonError::InsufficientFunds => "common.insufficient_funds",
CommonError::InvalidState => "common.invalid_state",
CommonError::InternalError => "common.internal_error",
CommonError::CodecError => "common.codec_error",
CommonError::NotImplemented => "common.not_implemented",
CommonError::Timeout => "common.timeout",
CommonError::Duplicate => "common.duplicate",
}
}
}
// =============================================================================
// Error Code Constants
// =============================================================================
/// Common error codes (1-999)
pub mod common_codes {
pub const UNAUTHORIZED: u32 = 1;
pub const INVALID_PARAMETERS: u32 = 2;
pub const NOT_FOUND: u32 = 3;
pub const INSUFFICIENT_FUNDS: u32 = 4;
pub const INVALID_STATE: u32 = 5;
pub const INTERNAL_ERROR: u32 = 6;
pub const CODEC_ERROR: u32 = 7;
pub const NOT_IMPLEMENTED: u32 = 8;
pub const TIMEOUT: u32 = 9;
pub const DUPLICATE: u32 = 10;
}
/// PropertyToken error codes (1000-1999)
pub mod property_token_codes {
pub const TOKEN_NOT_FOUND: u32 = 1001;
pub const UNAUTHORIZED_TRANSFER: u32 = 1002;
pub const PROPERTY_NOT_FOUND: u32 = 1003;
pub const INVALID_METADATA: u32 = 1004;
pub const DOCUMENT_NOT_FOUND: u32 = 1005;
pub const COMPLIANCE_FAILED: u32 = 1006;
pub const BRIDGE_NOT_SUPPORTED: u32 = 1007;
pub const INVALID_CHAIN: u32 = 1008;
pub const BRIDGE_LOCKED: u32 = 1009;
pub const INSUFFICIENT_SIGNATURES: u32 = 1010;
pub const REQUEST_EXPIRED: u32 = 1011;
pub const INVALID_REQUEST: u32 = 1012;
pub const BRIDGE_PAUSED: u32 = 1013;
pub const GAS_LIMIT_EXCEEDED: u32 = 1014;
pub const METADATA_CORRUPTION: u32 = 1015;
pub const INVALID_BRIDGE_OPERATOR: u32 = 1016;
pub const DUPLICATE_BRIDGE_REQUEST: u32 = 1017;
pub const BRIDGE_TIMEOUT: u32 = 1018;
pub const ALREADY_SIGNED: u32 = 1019;
pub const INSUFFICIENT_BALANCE: u32 = 1020;
pub const INVALID_AMOUNT: u32 = 1021;
pub const PROPOSAL_NOT_FOUND: u32 = 1022;
pub const PROPOSAL_CLOSED: u32 = 1023;
pub const ASK_NOT_FOUND: u32 = 1024;
pub const BATCH_SIZE_EXCEEDED: u32 = 1025;
// KYC-based transfer restriction error codes
pub const SENDER_NOT_VERIFIED: u32 = 1026;
pub const RECIPIENT_NOT_VERIFIED: u32 = 1027;
pub const VERIFICATION_LEVEL_INSUFFICIENT: u32 = 1028;
pub const TRANSFER_QUOTA_EXCEEDED: u32 = 1029;
pub const ACCOUNT_BLACKLISTED: u32 = 1030;
pub const ACCOUNT_NOT_WHITELISTED: u32 = 1031;
pub const HOLD_PERIOD_NOT_MET: u32 = 1032;
pub const SENDER_RISK_LEVEL_TOO_HIGH: u32 = 1033;
pub const RECIPIENT_RISK_LEVEL_TOO_HIGH: u32 = 1034;
pub const HIGH_RISK_ACCOUNT: u32 = 1035;
pub const STAKE_NOT_FOUND: u32 = 1026;
pub const LOCK_ACTIVE: u32 = 1027;
pub const NO_REWARDS: u32 = 1028;
pub const INSUFFICIENT_REWARD_POOL: u32 = 1029;
pub const ALREADY_STAKED: u32 = 1030;
pub const REENTRANT_CALL: u32 = 1031;
}
/// Escrow error codes (2000-2999)
pub mod escrow_codes {
pub const ESCROW_NOT_FOUND: u32 = 2001;
pub const UNAUTHORIZED_ACCESS: u32 = 2002;
pub const INVALID_STATUS: u32 = 2003;
pub const INSUFFICIENT_ESCROW_FUNDS: u32 = 2004;
pub const CONDITIONS_NOT_MET: u32 = 2005;
pub const SIGNATURE_THRESHOLD_NOT_MET: u32 = 2006;
pub const ALREADY_SIGNED_ESCROW: u32 = 2007;
pub const DOCUMENT_NOT_FOUND: u32 = 2008;
pub const DISPUTE_ACTIVE: u32 = 2009;
pub const TIME_LOCK_ACTIVE: u32 = 2010;
pub const INVALID_CONFIGURATION: u32 = 2011;
pub const ESCROW_ALREADY_FUNDED: u32 = 2012;
pub const PARTICIPANT_NOT_FOUND: u32 = 2013;
pub const REENTRANT_CALL: u32 = 2014;
// Multi-step approval error codes
pub const APPROVAL_REQUEST_NOT_FOUND: u32 = 2015;
pub const APPROVAL_REQUEST_EXPIRED: u32 = 2016;
pub const APPROVAL_REQUEST_ALREADY_EXECUTED: u32 = 2017;
pub const APPROVAL_REQUEST_CANCELLED: u32 = 2018;
pub const LARGE_TRANSFER_APPROVAL_REQUIRED: u32 = 2019;
// Fee-related error codes
pub const FEE_RATE_TOO_HIGH: u32 = 2020;
pub const INVALID_FEE_AMOUNT: u32 = 2021;
}
/// Bridge error codes (3000-3999)
pub mod bridge_codes {
pub const BRIDGE_UNAUTHORIZED: u32 = 3001;
pub const BRIDGE_TOKEN_NOT_FOUND: u32 = 3002;
pub const BRIDGE_INVALID_CHAIN: u32 = 3003;
pub const BRIDGE_NOT_SUPPORTED: u32 = 3004;
pub const BRIDGE_INSUFFICIENT_SIGNATURES: u32 = 3005;
pub const BRIDGE_REQUEST_EXPIRED: u32 = 3006;
pub const BRIDGE_ALREADY_SIGNED: u32 = 3007;
pub const BRIDGE_INVALID_REQUEST: u32 = 3008;
pub const BRIDGE_PAUSED: u32 = 3009;
pub const BRIDGE_INVALID_METADATA: u32 = 3010;
pub const BRIDGE_DUPLICATE_REQUEST: u32 = 3011;
pub const BRIDGE_GAS_LIMIT_EXCEEDED: u32 = 3012;
pub const BRIDGE_RATE_LIMIT_EXCEEDED: u32 = 3013;
pub const REENTRANT_CALL: u32 = 3014;
pub const BRIDGE_TRANSACTION_NOT_FOUND: u32 = 3015;
pub const BRIDGE_INVALID_STATUS_TRANSITION: u32 = 3016;
pub const BRIDGE_OPERATION_PAUSED: u32 = 3017;
pub const BRIDGE_NOT_GUARDIAN: u32 = 3018;
pub const BRIDGE_TRAVEL_RULE_DATA_REQUIRED: u32 = 3019;
pub const BRIDGE_TRAVEL_RULE_DATA_ALREADY_SUBMITTED: u32 = 3020;
}
/// Oracle error codes (4000-4999)
pub mod oracle_codes {
pub const ORACLE_PROPERTY_NOT_FOUND: u32 = 4001;
pub const ORACLE_INSUFFICIENT_SOURCES: u32 = 4002;
pub const ORACLE_INVALID_VALUATION: u32 = 4003;
pub const ORACLE_UNAUTHORIZED: u32 = 4004;
pub const ORACLE_SOURCE_NOT_FOUND: u32 = 4005;
pub const ORACLE_INVALID_PARAMETERS: u32 = 4006;
pub const ORACLE_PRICE_FEED_ERROR: u32 = 4007;
pub const ORACLE_ALERT_NOT_FOUND: u32 = 4008;
pub const ORACLE_INSUFFICIENT_REPUTATION: u32 = 4009;
pub const ORACLE_SOURCE_ALREADY_EXISTS: u32 = 4010;
pub const ORACLE_REQUEST_PENDING: u32 = 4011;
pub const ORACLE_BATCH_SIZE_EXCEEDED: u32 = 4012;
}
/// Fee error codes (5000-5999)
pub mod fee_codes {
pub const FEE_UNAUTHORIZED: u32 = 5001;
pub const FEE_AUCTION_NOT_FOUND: u32 = 5002;
pub const FEE_AUCTION_ENDED: u32 = 5003;
pub const FEE_AUCTION_NOT_ENDED: u32 = 5004;
pub const FEE_BID_TOO_LOW: u32 = 5005;
pub const FEE_ALREADY_SETTLED: u32 = 5006;
pub const FEE_INVALID_CONFIG: u32 = 5007;
pub const FEE_INVALID_PROPERTY: u32 = 5008;
pub const FEE_ARITHMETIC_ERROR: u32 = 5009;
pub const FEE_BID_DEADLINE_NOT_REACHED: u32 = 5010;
pub const FEE_SELF_BID_NOT_ALLOWED: u32 = 5011;
pub const FEE_INSUFFICIENT_VALUE: u32 = 5012;
pub const FEE_TRANSFER_FAILED: u32 = 5013;
}
/// Compliance error codes (6000-6999)
pub mod compliance_codes {
pub const COMPLIANCE_UNAUTHORIZED: u32 = 6001;
pub const COMPLIANCE_CHECK_FAILED: u32 = 6002;
pub const COMPLIANCE_NOT_VERIFIED: u32 = 6003;
pub const COMPLIANCE_DOCUMENT_MISSING: u32 = 6004;
pub const COMPLIANCE_EXPIRED: u32 = 6005;
pub const COMPLIANCE_HIGH_RISK: u32 = 6006;
pub const COMPLIANCE_PROHIBITED_JURISDICTION: u32 = 6007;
pub const COMPLIANCE_ALREADY_VERIFIED: u32 = 6008;
pub const COMPLIANCE_CONSENT_NOT_GIVEN: u32 = 6009;
pub const COMPLIANCE_INVALID_RISK_SCORE: u32 = 6010;
pub const COMPLIANCE_JURISDICTION_NOT_SUPPORTED: u32 = 6011;
pub const COMPLIANCE_INVALID_DOCUMENT_TYPE: u32 = 6012;
pub const COMPLIANCE_DATA_RETENTION_EXPIRED: u32 = 6013;
pub const COMPLIANCE_SANCTIONS_CHECK_FAILED: u32 = 6014;
pub const REENTRANT_CALL: u32 = 6015;
}
/// DEX error codes (7000-7999)
pub mod dex_codes {
pub const DEX_UNAUTHORIZED: u32 = 7001;
pub const DEX_INVALID_PAIR: u32 = 7002;
pub const DEX_POOL_NOT_FOUND: u32 = 7003;
pub const DEX_INSUFFICIENT_LIQUIDITY: u32 = 7004;
pub const DEX_SLIPPAGE_EXCEEDED: u32 = 7005;
pub const DEX_ORDER_NOT_FOUND: u32 = 7006;
pub const DEX_INVALID_ORDER: u32 = 7007;
pub const DEX_ORDER_NOT_EXECUTABLE: u32 = 7008;
pub const DEX_REWARD_UNAVAILABLE: u32 = 7009;
pub const DEX_PROPOSAL_NOT_FOUND: u32 = 7010;
pub const DEX_PROPOSAL_CLOSED: u32 = 7011;
pub const DEX_ALREADY_VOTED: u32 = 7012;
pub const DEX_INVALID_BRIDGE_ROUTE: u32 = 7013;
pub const DEX_CROSS_CHAIN_TRADE_NOT_FOUND: u32 = 7014;
pub const DEX_INSUFFICIENT_GOVERNANCE_BALANCE: u32 = 7015;
pub const REENTRANT_CALL: u32 = 7016;
pub const DEX_INVALID_REQUEST: u32 = 7016;
pub const DEX_TIMELOCK_REQUIRED: u32 = 7016;
pub const DEX_TIMELOCK_ACTIVE: u32 = 7017;
pub const DEX_ADMIN_ACTION_NOT_FOUND: u32 = 7018;
pub const DEX_ADMIN_ACTION_ALREADY_FINALIZED: u32 = 7019;
}
/// Governance error codes (8000-8999)
pub mod governance_codes {
pub const GOVERNANCE_UNAUTHORIZED: u32 = 8001;
pub const GOVERNANCE_PROPOSAL_NOT_FOUND: u32 = 8002;
pub const GOVERNANCE_ALREADY_VOTED: u32 = 8003;
pub const GOVERNANCE_PROPOSAL_CLOSED: u32 = 8004;
pub const GOVERNANCE_THRESHOLD_NOT_MET: u32 = 8005;
pub const GOVERNANCE_TIMELOCK_ACTIVE: u32 = 8006;
pub const GOVERNANCE_INVALID_THRESHOLD: u32 = 8007;
pub const GOVERNANCE_SIGNER_EXISTS: u32 = 8008;
pub const GOVERNANCE_SIGNER_NOT_FOUND: u32 = 8009;
pub const GOVERNANCE_MIN_SIGNERS: u32 = 8010;
pub const GOVERNANCE_MAX_PROPOSALS: u32 = 8011;
pub const GOVERNANCE_NOT_A_SIGNER: u32 = 8012;
pub const GOVERNANCE_PROPOSAL_EXPIRED: u32 = 8013;
/// Signer roster changes are blocked while proposals are actively voting.
pub const GOVERNANCE_SIGNER_CHANGES_LOCKED: u32 = 8014;
}
/// Staking error codes (9000-9999)
pub mod staking_codes {
pub const STAKING_UNAUTHORIZED: u32 = 9001;
pub const STAKING_INSUFFICIENT_AMOUNT: u32 = 9002;
pub const STAKING_NOT_FOUND: u32 = 9003;
pub const STAKING_LOCK_ACTIVE: u32 = 9004;
pub const STAKING_NO_REWARDS: u32 = 9005;
pub const STAKING_INSUFFICIENT_POOL: u32 = 9006;
pub const STAKING_INVALID_CONFIG: u32 = 9007;
pub const STAKING_ALREADY_STAKED: u32 = 9008;
pub const STAKING_INVALID_DELEGATE: u32 = 9009;
pub const STAKING_ZERO_AMOUNT: u32 = 9010;
pub const REENTRANT_CALL: u32 = 9011;
pub const STAKING_NO_VOTING_POWER: u32 = 9012;
pub const STAKING_PROPOSAL_NOT_FOUND: u32 = 9013;
pub const STAKING_PROPOSAL_CLOSED: u32 = 9014;
pub const STAKING_ALREADY_VOTED: u32 = 9015;
pub const STAKING_VOTING_ACTIVE: u32 = 9016;
pub const STAKING_VOTING_ENDED: u32 = 9017;
pub const STAKING_QUORUM_NOT_REACHED: u32 = 9018;
pub const STAKING_TOO_MANY_PROPOSALS: u32 = 9019;
}
/// Monitoring error codes (10000-10999)
pub mod monitoring_codes {
pub const MONITORING_UNAUTHORIZED: u32 = 10001;
pub const MONITORING_CONTRACT_PAUSED: u32 = 10002;
pub const MONITORING_INVALID_THRESHOLD: u32 = 10003;
pub const MONITORING_SUBSCRIBER_LIMIT_REACHED: u32 = 10004;
pub const MONITORING_SUBSCRIBER_NOT_FOUND: u32 = 10005;
pub const MONITORING_HEALTH_CHECK_FAILED: u32 = 10006;
}
/// EventBus error codes (11000-11999)
pub mod event_bus_codes {
pub const EVENT_BUS_UNAUTHORIZED: u32 = 11001;
pub const EVENT_BUS_TOPIC_NOT_FOUND: u32 = 11002;
pub const EVENT_BUS_ALREADY_SUBSCRIBED: u32 = 11003;
pub const EVENT_BUS_NOT_SUBSCRIBED: u32 = 11004;
pub const EVENT_BUS_MAX_SUBSCRIBERS_REACHED: u32 = 11005;
pub const EVENT_BUS_SUBSCRIBER_CALL_FAILED: u32 = 11006;
pub const EVENT_BUS_REENTRANT_CALL: u32 = 11007;
}
/// Lending error codes (12000-12999)
pub mod lending_codes {
pub const LENDING_UNAUTHORIZED: u32 = 12001;
pub const LENDING_PROPERTY_NOT_FOUND: u32 = 12002;
pub const LENDING_INSUFFICIENT_COLLATERAL: u32 = 12003;
pub const LENDING_LOAN_NOT_FOUND: u32 = 12004;
pub const LENDING_LOAN_NOT_ACTIVE: u32 = 12005;
pub const LENDING_POOL_NOT_FOUND: u32 = 12006;
pub const LENDING_INSUFFICIENT_LIQUIDITY: u32 = 12007;
pub const LENDING_POSITION_NOT_FOUND: u32 = 12008;
pub const LENDING_LIQUIDATION_THRESHOLD_NOT_MET: u32 = 12009;
pub const LENDING_INVALID_PARAMETERS: u32 = 12010;
pub const LENDING_PROPOSAL_NOT_FOUND: u32 = 12011;
pub const LENDING_RESTRUCTURING_NOT_FOUND: u32 = 12012;
pub const LENDING_INSUFFICIENT_VOTES: u32 = 12013;
pub const LENDING_SERVICER_NOT_FOUND: u32 = 12014;
pub const LENDING_PAYMENT_SCHEDULE_NOT_FOUND: u32 = 12015;
pub const LENDING_REENTRANT_CALL: u32 = 12016;
pub const LENDING_KEY_ROTATION_COOLDOWN: u32 = 12017;
pub const LENDING_KEY_ROTATION_EXPIRED: u32 = 12018;
pub const LENDING_NO_PENDING_ROTATION: u32 = 12019;
pub const LENDING_ROTATION_UNAUTHORIZED: u32 = 12020;
pub const LENDING_REQUEST_EXPIRED: u32 = 12021;
}
/// Insurance error codes (14000-14999)
pub mod insurance_codes {
pub const INSURANCE_UNAUTHORIZED: u32 = 14001;
pub const INSURANCE_POLICY_NOT_FOUND: u32 = 14002;
pub const INSURANCE_CLAIM_NOT_FOUND: u32 = 14003;
pub const INSURANCE_POOL_NOT_FOUND: u32 = 14004;
pub const INSURANCE_POLICY_ALREADY_ACTIVE: u32 = 14005;
pub const INSURANCE_POLICY_EXPIRED: u32 = 14006;
pub const INSURANCE_POLICY_INACTIVE: u32 = 14007;
pub const INSURANCE_INSUFFICIENT_PREMIUM: u32 = 14008;
pub const INSURANCE_INSUFFICIENT_POOL_FUNDS: u32 = 14009;
pub const INSURANCE_CLAIM_ALREADY_PROCESSED: u32 = 14010;
pub const INSURANCE_CLAIM_EXCEEDS_COVERAGE: u32 = 14011;
pub const INSURANCE_INVALID_PARAMETERS: u32 = 14012;
pub const INSURANCE_ORACLE_VERIFICATION_FAILED: u32 = 14013;
pub const INSURANCE_REINSURANCE_CAPACITY_EXCEEDED: u32 = 14014;
pub const INSURANCE_TOKEN_NOT_FOUND: u32 = 14015;
pub const INSURANCE_TRANSFER_FAILED: u32 = 14016;
pub const INSURANCE_COOLDOWN_PERIOD_ACTIVE: u32 = 14017;
pub const INSURANCE_PROPERTY_NOT_INSURABLE: u32 = 14018;
pub const INSURANCE_DUPLICATE_CLAIM: u32 = 14019;
pub const INSURANCE_REENTRANT_CALL: u32 = 14020;
pub const INSURANCE_RISK_ASSESSMENT_NOT_FOUND: u32 = 14021;
pub const INSURANCE_RISK_ASSESSMENT_EXPIRED: u32 = 14022;
pub const INSURANCE_INVALID_RISK_FACTORS: u32 = 14023;
pub const INSURANCE_RISK_MODEL_GENERATION_FAILED: u32 = 14024;
pub const INSURANCE_FRAUD_ASSESSMENT_NOT_FOUND: u32 = 14025;
pub const INSURANCE_HIGH_FRAUD_RISK: u32 = 14026;
pub const INSURANCE_FRAUD_PATTERN_NOT_FOUND: u32 = 14027;
pub const INSURANCE_INVALID_FRAUD_INDICATOR: u32 = 14028;
pub const INSURANCE_REINSURANCE_AGREEMENT_NOT_FOUND: u32 = 14029;
pub const INSURANCE_REINSURANCE_AGREEMENT_EXPIRED: u32 = 14030;
pub const INSURANCE_REINSURANCE_AGREEMENT_INACTIVE: u32 = 14031;
pub const INSURANCE_TRIGGER_NOT_FOUND: u32 = 14032;
pub const INSURANCE_TRIGGER_INACTIVE: u32 = 14033;
pub const INSURANCE_TRIGGER_ALREADY_FIRED: u32 = 14034;
pub const INSURANCE_TRIGGER_CONDITION_NOT_MET: u32 = 14035;
pub const INSURANCE_INVALID_PAYOUT_MODE: u32 = 14036;
pub const INSURANCE_PARAMETRIC_POLICY_NOT_FOUND: u32 = 14037;
pub const INSURANCE_PARAMETRIC_POLICY_INACTIVE: u32 = 14038;
pub const INSURANCE_PARAMETRIC_POLICY_ALREADY_TRIGGERED: u32 = 14039;
pub const INSURANCE_CIRCUIT_BREAKER_ACTIVE: u32 = 14040;
pub const INSURANCE_SINGLE_PAYOUT_LIMIT_EXCEEDED: u32 = 14041;
pub const INSURANCE_DAILY_PAYOUT_LIMIT_EXCEEDED: u32 = 14042;
pub const INSURANCE_KEY_ROTATION_COOLDOWN: u32 = 14043;
pub const INSURANCE_KEY_ROTATION_EXPIRED: u32 = 14044;
pub const INSURANCE_NO_PENDING_ROTATION: u32 = 14045;
pub const INSURANCE_ROTATION_UNAUTHORIZED: u32 = 14046;
pub const INSURANCE_REQUEST_EXPIRED: u32 = 14047;
}
// =============================================================================
// ErrorExt Macro
// =============================================================================
/// Implements `ContractError` and `core::fmt::Display` for a fieldless error
/// enum with minimal boilerplate.
///
/// The macro generates:
/// - `impl $crate::errors::ContractError for $ty` with `error_code()`,
/// `error_description()`, and `error_category()`
/// - `impl core::fmt::Display for $ty` delegating to `ContractError::error_description()`
///
/// # Usage
///
/// ```ignore
/// use propchain_traits::errors::{error_ext, ContractError, ErrorCategory};
/// use propchain_traits::errors::lending_codes;
///
/// #[derive(Debug, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
/// pub enum MyError {
/// Unauthorized,
/// NotFound,
/// }
///
/// error_ext! {
/// MyError,
/// ErrorCategory::Common,
/// [
/// (Unauthorized, lending_codes::LENDING_UNAUTHORIZED, "Caller does not have permission"),
/// (NotFound, lending_codes::LENDING_PROPERTY_NOT_FOUND, "Resource not found"),
/// ]
/// }
/// ```
#[macro_export]
macro_rules! error_ext {
(
$ty:ty,
$category:expr,
[
$(($variant:ident, $code:expr, $desc:expr)),+ $(,)?
]
) => {
impl $crate::errors::ContractError for $ty {
fn error_code(&self) -> u32 {
match self {
$( Self::$variant => $code, )+
}
}
fn error_description(&self) -> &'static str {
match self {
$( Self::$variant => $desc, )+
}
}
fn error_category(&self) -> $crate::errors::ErrorCategory {
$category
}
}
impl ::core::fmt::Display for $ty {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.write_str(<$ty as $crate::errors::ContractError>::error_description(self))
}
}
};
}
pub use error_ext;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn common_error_i18n_keys_are_correct() {
assert_eq!(
CommonError::Unauthorized.error_i18n_key(),
"common.unauthorized"
);
assert_eq!(CommonError::NotFound.error_i18n_key(), "common.not_found");
assert_eq!(CommonError::Duplicate.error_i18n_key(), "common.duplicate");
}
#[test]
fn to_error_message_populates_all_fields() {
let msg = CommonError::Unauthorized.to_error_message();
assert_eq!(msg.code, common_codes::UNAUTHORIZED);
assert_eq!(msg.category, ErrorCategory::Common);
assert_eq!(msg.i18n_key, "common.unauthorized");
assert!(!msg.description.is_empty());
}
#[test]
fn oracle_batch_size_exceeded_constant_matches_value() {
assert_eq!(oracle_codes::ORACLE_BATCH_SIZE_EXCEEDED, 4012);
}
#[test]
fn compliance_codes_are_unique() {
let mut codes = vec![
compliance_codes::COMPLIANCE_UNAUTHORIZED,
compliance_codes::COMPLIANCE_NOT_VERIFIED,
compliance_codes::COMPLIANCE_CHECK_FAILED,
compliance_codes::COMPLIANCE_DOCUMENT_MISSING,
compliance_codes::COMPLIANCE_EXPIRED,
compliance_codes::COMPLIANCE_HIGH_RISK,
compliance_codes::COMPLIANCE_PROHIBITED_JURISDICTION,
compliance_codes::COMPLIANCE_ALREADY_VERIFIED,
compliance_codes::COMPLIANCE_CONSENT_NOT_GIVEN,
compliance_codes::COMPLIANCE_INVALID_RISK_SCORE,
compliance_codes::COMPLIANCE_JURISDICTION_NOT_SUPPORTED,
compliance_codes::COMPLIANCE_INVALID_DOCUMENT_TYPE,
compliance_codes::COMPLIANCE_DATA_RETENTION_EXPIRED,
compliance_codes::COMPLIANCE_SANCTIONS_CHECK_FAILED,
compliance_codes::REENTRANT_CALL,
];
let len = codes.len();
codes.sort();
codes.dedup();
assert_eq!(
codes.len(),
len,
"duplicate compliance error codes detected"
);
}
}