Merge pull request #211 from TheBlueMatt/2018-10-misc-cleanups
[rust-lightning] / src / util / errors.rs
1 //! Error types live here.
2
3 use std::fmt;
4
5 /// Indicates an error on the client's part (usually some variant of attempting to use too-low or
6 /// too-high values)
7 pub enum APIError {
8         /// Indicates the API was wholly misused (see err for more). Cases where these can be returned
9         /// are documented, but generally indicates some precondition of a function was violated.
10         APIMisuseError {
11                 /// A human-readable error message
12                 err: &'static str
13         },
14         /// Due to a high feerate, we were unable to complete the request.
15         /// For example, this may be returned if the feerate implies we cannot open a channel at the
16         /// requested value, but opening a larger channel would succeed.
17         FeeRateTooHigh {
18                 /// A human-readable error message
19                 err: String,
20                 /// The feerate which was too high.
21                 feerate: u64
22         },
23         /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
24         /// too-many-hops, etc).
25         RouteError {
26                 /// A human-readable error message
27                 err: &'static str
28         },
29         /// We were unable to complete the request as the Channel required to do so is unable to
30         /// complete the request (or was not found). This can take many forms, including disconnected
31         /// peer, channel at capacity, channel shutting down, etc.
32         ChannelUnavailable {
33                 /// A human-readable error message
34                 err: &'static str
35         }
36 }
37
38 impl fmt::Debug for APIError {
39         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40                 match *self {
41                         APIError::APIMisuseError {ref err} => f.write_str(err),
42                         APIError::FeeRateTooHigh {ref err, ref feerate} => write!(f, "{} feerate: {}", err, feerate),
43                         APIError::RouteError {ref err} => f.write_str(err),
44                         APIError::ChannelUnavailable {ref err} => f.write_str(err),
45                 }
46         }
47 }