dusk_core/
error.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7//! Error-type for dusk-core.
8
9use alloc::string::String;
10use core::fmt;
11
12/// The dusk-core error type.
13#[derive(Debug, Clone, PartialEq)]
14pub enum Error {
15    /// There is not sufficient balance to cover the transaction costs.
16    InsufficientBalance,
17    /// A transaction input has been used already.
18    Replay,
19    /// The input-note doesn't belong to the given key.
20    PhoenixOwnership,
21    /// The transaction circuit wasn't found or is incorrect.
22    PhoenixCircuit(String),
23    /// The transaction circuit prover wasn't found or couldn't be created.
24    PhoenixProver(String),
25    /// Dusk-bytes InvalidData error
26    InvalidData,
27    /// Dusk-bytes BadLength error
28    BadLength(usize, usize),
29    /// Dusk-bytes InvalidChar error
30    InvalidChar(char, usize),
31    /// Rkyv serialization.
32    Rkyv(String),
33    /// The provided memo is too large. Contains the memo size used. The max
34    /// size is [`MAX_MEMO_SIZE`].
35    ///
36    /// [`MAX_MEMO_SIZE`]: crate::transfer::data::MAX_MEMO_SIZE
37    MemoTooLarge(usize),
38}
39
40impl fmt::Display for Error {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        write!(f, "Dusk-Core Error: {:?}", &self)
43    }
44}
45
46impl From<phoenix_core::Error> for Error {
47    fn from(core_error: phoenix_core::Error) -> Self {
48        #[allow(clippy::match_same_arms)]
49        match core_error {
50            phoenix_core::Error::InvalidNoteType(_) => Self::InvalidData,
51            phoenix_core::Error::MissingViewKey => Self::PhoenixOwnership,
52            phoenix_core::Error::InvalidEncryption => Self::PhoenixOwnership,
53            phoenix_core::Error::InvalidData => Self::InvalidData,
54            phoenix_core::Error::BadLength(found, expected) => {
55                Self::BadLength(found, expected)
56            }
57            phoenix_core::Error::InvalidChar(ch, index) => {
58                Self::InvalidChar(ch, index)
59            }
60        }
61    }
62}
63
64impl From<dusk_bytes::Error> for Error {
65    fn from(bytes_error: dusk_bytes::Error) -> Self {
66        match bytes_error {
67            dusk_bytes::Error::InvalidData => Self::InvalidData,
68            dusk_bytes::Error::BadLength { found, expected } => {
69                Self::BadLength(found, expected)
70            }
71            dusk_bytes::Error::InvalidChar { ch, index } => {
72                Self::InvalidChar(ch, index)
73            }
74        }
75    }
76}