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
// RGB standard library
// Written in 2020 by
//     Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

// TODO: Consider moving parts of this file to common daemon modules (LNP/BP)

use std::collections::HashMap;
use std::io;
use tokio::task::JoinError;

use lnpbp::bitcoin::blockdata::transaction::ParseOutPointError;
use lnpbp::lnp;

#[derive(Debug, Display, Error, From)]
#[display(Debug)]
pub enum BootstrapError {
    TorNotYetSupported,

    #[from]
    IoError(io::Error),

    #[from]
    ArgParseError(String),

    #[from]
    ZmqSocketError(zmq::Error),

    #[from]
    MultithreadError(JoinError),

    MonitorSocketError(Box<dyn std::error::Error + Send>),

    #[from]
    MessageBusError(lnp::transport::Error),

    #[from]
    ElectrumError(electrum_client::Error),

    StorageError,

    #[from(crate::contracts::fungible::FileCacheError)]
    #[from(crate::contracts::fungible::SqlCacheError)]
    CacheError,

    Other,
}

impl From<&str> for BootstrapError {
    fn from(err: &str) -> Self {
        BootstrapError::ArgParseError(err.to_string())
    }
}

use lnpbp::hex;
use std::num::{ParseFloatError, ParseIntError};

#[derive(Clone, Copy, Debug, Display, Error, From)]
#[display(doc_comments)]
#[from(ParseFloatError)]
#[from(ParseIntError)]
#[from(ParseOutPointError)]
#[from(hex::Error)]
/// Error parsing data
pub struct ParseError;

#[derive(Clone, PartialEq, Eq, Debug, Display, Error, From)]
#[display(Debug)]
pub enum RuntimeError {
    #[from(std::io::Error)]
    Io,
    Zmq(ServiceSocketType, String, zmq::Error),
    #[from]
    Lnp(lnp::transport::Error),
    #[from(lnp::presentation::Error)]
    BrokenTransport,
    Internal(String),
}

impl RuntimeError {
    pub fn zmq_request(socket: &str, err: zmq::Error) -> Self {
        RuntimeError::Zmq(ServiceSocketType::Request, socket.to_string(), err)
    }

    pub fn zmq_reply(socket: &str, err: zmq::Error) -> Self {
        RuntimeError::Zmq(ServiceSocketType::Request, socket.to_string(), err)
    }

    pub fn zmq_publish(socket: &str, err: zmq::Error) -> Self {
        RuntimeError::Zmq(ServiceSocketType::Request, socket.to_string(), err)
    }

    pub fn zmq_subscribe(socket: &str, err: zmq::Error) -> Self {
        RuntimeError::Zmq(ServiceSocketType::Request, socket.to_string(), err)
    }
}

#[derive(Clone, PartialEq, Eq, Debug, Display, Error)]
#[display(Debug)]
pub enum RoutedError {
    Global(RuntimeError),
    RequestSpecific(ServiceError),
}

#[derive(Clone, PartialEq, Eq, Debug, Display, Error, From)]
#[display(Debug)]
pub enum ServiceErrorDomain {
    #[from(::std::io::Error)]
    Io,
    Stash,
    Storage(String),
    Index,
    #[from(crate::contracts::fungible::FileCacheError)]
    #[from(crate::contracts::fungible::SqlCacheError)]
    Cache,
    Multithreading,
    P2pwire,
    #[from]
    LnpRpc(lnp::presentation::Error),
    #[from]
    LnpTransport(lnp::transport::Error),
    Api(ApiErrorType),
    Monitoring,
    Bifrost,
    BpNode,
    LnpNode,
    Bitcoin,
    Lightning,
    Schema(String),
    Anchor(String),
    #[from]
    Internal(String),
}

#[derive(Clone, PartialEq, Eq, Debug, Display)]
#[display(Debug)]
pub enum ServiceErrorSource {
    Broker,
    Stash,
    Contract(String),
}

#[derive(Clone, PartialEq, Eq, Debug, Display)]
#[display(Debug)]
pub enum ServiceSocketType {
    Request,
    Reply,
    Publish,
    Subscribe,
}

#[derive(Clone, PartialEq, Eq, Debug, Display, Error)]
#[display(Debug)]
pub enum ApiErrorType {
    MalformedRequest { request: String },
    UnknownCommand { command: String },
    UnimplementedCommand,
    MissedArgument { request: String, argument: String },
    UnknownArgument { request: String, argument: String },
    MalformedArgument { request: String, argument: String },
    UnexpectedReply,
}

#[derive(Clone, PartialEq, Eq, Debug, Display, Error)]
#[display(Debug)]
pub struct ServiceError {
    pub domain: ServiceErrorDomain,
    pub service: ServiceErrorSource,
}

impl ServiceError {
    pub fn contract(domain: ServiceErrorDomain, contract_name: &str) -> Self {
        Self {
            domain,
            service: ServiceErrorSource::Contract(contract_name.to_string()),
        }
    }

    pub fn from_rpc(
        service: ServiceErrorSource,
        err: lnp::presentation::Error,
    ) -> Self {
        Self {
            domain: ServiceErrorDomain::from(err),
            service,
        }
    }
}

#[derive(Debug, Display, Error)]
#[display(Debug)]
pub struct ServiceErrorRepresentation {
    pub domain: String,
    pub service: String,
    pub name: String,
    pub description: String,
    pub info: HashMap<String, String>,
}