11// SPDX-License-Identifier: CC0-1.0
22
3- use std:: { error, fmt, io} ;
4-
5- use bitcoin:: hex;
63use types:: v17:: {
74 GetBlockHeaderError , GetBlockHeaderVerboseError , GetBlockVerboseOneError ,
85 GetRawTransactionVerboseError ,
@@ -13,137 +10,32 @@ use types::v29::{
1310 GetBlockVerboseOneError as GetBlockVerboseOneErrorV29 ,
1411} ;
1512
16- /// The error type for errors produced in this library.
17- #[ derive( Debug ) ]
18- pub enum Error {
19- JsonRpc ( jsonrpc:: error:: Error ) ,
20- HexToArray ( hex:: HexToArrayError ) ,
21- HexToBytes ( hex:: HexToBytesError ) ,
22- Json ( serde_json:: error:: Error ) ,
23- BitcoinSerialization ( bitcoin:: consensus:: encode:: FromHexError ) ,
24- Io ( io:: Error ) ,
25- InvalidCookieFile ,
26- /// The JSON result had an unexpected structure.
27- UnexpectedStructure ,
28- /// The daemon returned an error string.
29- Returned ( String ) ,
30- /// The server version did not match what was expected.
31- ServerVersion ( UnexpectedServerVersionError ) ,
32- /// Missing user/password.
33- MissingUserPassword ,
34- }
35-
36- impl From < jsonrpc:: error:: Error > for Error {
37- fn from ( e : jsonrpc:: error:: Error ) -> Error { Error :: JsonRpc ( e) }
38- }
39-
40- impl From < hex:: HexToArrayError > for Error {
41- fn from ( e : hex:: HexToArrayError ) -> Self { Self :: HexToArray ( e) }
42- }
43-
44- impl From < hex:: HexToBytesError > for Error {
45- fn from ( e : hex:: HexToBytesError ) -> Self { Self :: HexToBytes ( e) }
46- }
47-
48- impl From < serde_json:: error:: Error > for Error {
49- fn from ( e : serde_json:: error:: Error ) -> Error { Error :: Json ( e) }
50- }
51-
52- impl From < bitcoin:: consensus:: encode:: FromHexError > for Error {
53- fn from ( e : bitcoin:: consensus:: encode:: FromHexError ) -> Error { Error :: BitcoinSerialization ( e) }
54- }
55-
56- impl From < io:: Error > for Error {
57- fn from ( e : io:: Error ) -> Error { Error :: Io ( e) }
58- }
13+ pub use crate :: error:: { Error , UnexpectedServerVersionError } ;
5914
6015impl From < GetBlockHeaderError > for Error {
61- fn from ( e : GetBlockHeaderError ) -> Self { Self :: Returned ( e. to_string ( ) ) }
16+ fn from ( e : GetBlockHeaderError ) -> Self { Self :: Model ( e. to_string ( ) ) }
6217}
6318
6419impl From < GetBlockHeaderVerboseError > for Error {
65- fn from ( e : GetBlockHeaderVerboseError ) -> Self { Self :: Returned ( e. to_string ( ) ) }
20+ fn from ( e : GetBlockHeaderVerboseError ) -> Self { Self :: Model ( e. to_string ( ) ) }
6621}
6722
6823impl From < GetBlockVerboseOneError > for Error {
69- fn from ( e : GetBlockVerboseOneError ) -> Self { Self :: Returned ( e. to_string ( ) ) }
24+ fn from ( e : GetBlockVerboseOneError ) -> Self { Self :: Model ( e. to_string ( ) ) }
7025}
7126
7227impl From < GetRawTransactionVerboseError > for Error {
73- fn from ( e : GetRawTransactionVerboseError ) -> Self { Self :: Returned ( e. to_string ( ) ) }
28+ fn from ( e : GetRawTransactionVerboseError ) -> Self { Self :: Model ( e. to_string ( ) ) }
7429}
7530
7631impl From < GetBlockHeaderVerboseErrorV29 > for Error {
77- fn from ( e : GetBlockHeaderVerboseErrorV29 ) -> Self { Self :: Returned ( e. to_string ( ) ) }
32+ fn from ( e : GetBlockHeaderVerboseErrorV29 ) -> Self { Self :: Model ( e. to_string ( ) ) }
7833}
7934
8035impl From < GetBlockVerboseOneErrorV29 > for Error {
81- fn from ( e : GetBlockVerboseOneErrorV29 ) -> Self { Self :: Returned ( e. to_string ( ) ) }
36+ fn from ( e : GetBlockVerboseOneErrorV29 ) -> Self { Self :: Model ( e. to_string ( ) ) }
8237}
8338
8439impl From < GetBlockFilterError > for Error {
85- fn from ( e : GetBlockFilterError ) -> Self { Self :: Returned ( e. to_string ( ) ) }
86- }
87-
88- impl fmt:: Display for Error {
89- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
90- use Error :: * ;
91-
92- match * self {
93- JsonRpc ( ref e) => write ! ( f, "JSON-RPC error: {}" , e) ,
94- HexToArray ( ref e) => write ! ( f, "hex to array decode error: {}" , e) ,
95- HexToBytes ( ref e) => write ! ( f, "hex to bytes decode error: {}" , e) ,
96- Json ( ref e) => write ! ( f, "JSON error: {}" , e) ,
97- BitcoinSerialization ( ref e) => write ! ( f, "Bitcoin serialization error: {}" , e) ,
98- Io ( ref e) => write ! ( f, "I/O error: {}" , e) ,
99- InvalidCookieFile => write ! ( f, "invalid cookie file" ) ,
100- UnexpectedStructure => write ! ( f, "the JSON result had an unexpected structure" ) ,
101- Returned ( ref s) => write ! ( f, "the daemon returned an error string: {}" , s) ,
102- ServerVersion ( ref e) => write ! ( f, "server version: {}" , e) ,
103- MissingUserPassword => write ! ( f, "missing user and/or password" ) ,
104- }
105- }
106- }
107-
108- impl error:: Error for Error {
109- fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
110- use Error :: * ;
111-
112- match * self {
113- JsonRpc ( ref e) => Some ( e) ,
114- HexToArray ( ref e) => Some ( e) ,
115- HexToBytes ( ref e) => Some ( e) ,
116- Json ( ref e) => Some ( e) ,
117- BitcoinSerialization ( ref e) => Some ( e) ,
118- Io ( ref e) => Some ( e) ,
119- ServerVersion ( ref e) => Some ( e) ,
120- InvalidCookieFile | UnexpectedStructure | Returned ( _) | MissingUserPassword => None ,
121- }
122- }
123- }
124-
125- /// Error returned when RPC client expects a different version than bitcoind reports.
126- #[ derive( Debug , Clone , PartialEq , Eq ) ]
127- pub struct UnexpectedServerVersionError {
128- /// Version from server.
129- pub got : usize ,
130- /// Expected server version.
131- pub expected : Vec < usize > ,
132- }
133-
134- impl fmt:: Display for UnexpectedServerVersionError {
135- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
136- let mut expected = String :: new ( ) ;
137- for version in & self . expected {
138- let v = format ! ( " {} " , version) ;
139- expected. push_str ( & v) ;
140- }
141- write ! ( f, "unexpected bitcoind version, got: {} expected one of: {}" , self . got, expected)
142- }
143- }
144-
145- impl error:: Error for UnexpectedServerVersionError { }
146-
147- impl From < UnexpectedServerVersionError > for Error {
148- fn from ( e : UnexpectedServerVersionError ) -> Self { Self :: ServerVersion ( e) }
40+ fn from ( e : GetBlockFilterError ) -> Self { Self :: Model ( e. to_string ( ) ) }
14941}
0 commit comments