2021-03-25 21:22:45 -07:00
//! Error types for problems that may arise when reading or storing wallet data to SQLite.
2021-01-12 17:24:18 -08:00
use std ::error ;
2019-03-08 18:16:00 -08:00
use std ::fmt ;
2022-08-15 12:14:15 -07:00
use zcash_client_backend ::{
data_api ,
encoding ::{ Bech32DecodeError , TransparentCodecError } ,
} ;
2021-04-13 10:02:35 -07:00
use zcash_primitives ::consensus ::BlockHeight ;
2019-03-08 18:16:00 -08:00
2021-04-13 10:02:35 -07:00
use crate ::{ NoteId , PRUNING_HEIGHT } ;
2020-08-18 12:48:59 -07:00
2022-08-30 15:54:58 -07:00
#[ cfg(feature = " unstable " ) ]
use zcash_primitives ::transaction ::TxId ;
2021-03-25 21:22:45 -07:00
/// The primary error type for the SQLite wallet backend.
2019-03-08 18:16:00 -08:00
#[ derive(Debug) ]
2021-01-12 17:24:18 -08:00
pub enum SqliteClientError {
/// Decoding of a stored value from its serialized form has failed.
CorruptedData ( String ) ,
2021-01-19 11:41:12 -08:00
2021-01-12 17:24:18 -08:00
/// Decoding of the extended full viewing key has failed (for the specified network)
2021-03-26 22:17:54 -07:00
IncorrectHrpExtFvk ,
2021-01-19 11:41:12 -08:00
2021-01-12 17:24:18 -08:00
/// The rcm value for a note cannot be decoded to a valid JubJub point.
InvalidNote ,
2021-01-19 11:41:12 -08:00
2021-02-03 11:46:47 -08:00
/// The note id associated with a witness being stored corresponds to a
2021-01-19 11:27:48 -08:00
/// sent note, not a received note.
InvalidNoteId ,
/// Illegal attempt to reinitialize an already-initialized wallet database.
TableNotEmpty ,
2022-08-30 15:54:58 -07:00
/// A transaction that has already been mined was requested for removal.
#[ cfg(feature = " unstable " ) ]
TransactionIsMined ( TxId ) ,
2022-08-15 12:14:15 -07:00
/// A Bech32-encoded key or address decoding error
Bech32DecodeError ( Bech32DecodeError ) ,
2021-01-19 11:41:12 -08:00
2021-01-12 17:24:18 -08:00
/// Base58 decoding error
Base58 ( bs58 ::decode ::Error ) ,
2021-01-19 11:41:12 -08:00
2020-12-22 06:10:13 -08:00
/// Base58 decoding error
TransparentAddress ( TransparentCodecError ) ,
2021-01-12 17:24:18 -08:00
/// Wrapper for rusqlite errors.
DbError ( rusqlite ::Error ) ,
2021-01-19 11:41:12 -08:00
2021-01-12 17:24:18 -08:00
/// Wrapper for errors from the IO subsystem
Io ( std ::io ::Error ) ,
2021-01-19 11:41:12 -08:00
2021-01-12 17:24:18 -08:00
/// A received memo cannot be interpreted as a UTF-8 string.
2020-10-29 09:48:26 -07:00
InvalidMemo ( zcash_primitives ::memo ::Error ) ,
2021-01-19 11:41:12 -08:00
2021-04-13 10:02:35 -07:00
/// A requested rewind would violate invariants of the
/// storage layer. The payload returned with this error is
2022-01-31 15:36:09 -08:00
/// (safe rewind height, requested height).
RequestedRewindInvalid ( BlockHeight , BlockHeight ) ,
2021-04-13 10:02:35 -07:00
2021-01-13 14:20:11 -08:00
/// Wrapper for errors from zcash_client_backend
BackendError ( data_api ::error ::Error < NoteId > ) ,
2019-03-08 18:16:00 -08:00
}
2021-01-12 17:24:18 -08:00
impl error ::Error for SqliteClientError {
fn source ( & self ) -> Option < & ( dyn error ::Error + 'static ) > {
match & self {
SqliteClientError ::InvalidMemo ( e ) = > Some ( e ) ,
2022-08-15 12:14:15 -07:00
SqliteClientError ::Bech32DecodeError ( Bech32DecodeError ::Bech32Error ( e ) ) = > Some ( e ) ,
2021-01-12 17:24:18 -08:00
SqliteClientError ::DbError ( e ) = > Some ( e ) ,
SqliteClientError ::Io ( e ) = > Some ( e ) ,
_ = > None ,
}
2019-03-08 18:16:00 -08:00
}
}
2021-01-12 17:24:18 -08:00
impl fmt ::Display for SqliteClientError {
fn fmt ( & self , f : & mut fmt ::Formatter ) -> fmt ::Result {
match & self {
SqliteClientError ::CorruptedData ( reason ) = > {
write! ( f , " Data DB is corrupted: {} " , reason )
}
2021-03-26 22:17:54 -07:00
SqliteClientError ::IncorrectHrpExtFvk = > write! ( f , " Incorrect HRP for extfvk " ) ,
2021-01-12 17:24:18 -08:00
SqliteClientError ::InvalidNote = > write! ( f , " Invalid note " ) ,
2021-04-13 10:02:35 -07:00
SqliteClientError ::InvalidNoteId = >
write! ( f , " The note ID associated with an inserted witness must correspond to a received note. " ) ,
2022-01-31 15:36:09 -08:00
SqliteClientError ::RequestedRewindInvalid ( h , r ) = >
write! ( f , " A rewind must be either of less than {} blocks, or at least back to block {} for your wallet; the requested height was {}. " , PRUNING_HEIGHT , h , r ) ,
2022-08-15 12:14:15 -07:00
SqliteClientError ::Bech32DecodeError ( e ) = > write! ( f , " {} " , e ) ,
2021-01-12 17:24:18 -08:00
SqliteClientError ::Base58 ( e ) = > write! ( f , " {} " , e ) ,
2020-12-22 06:10:13 -08:00
SqliteClientError ::TransparentAddress ( e ) = > write! ( f , " {} " , e ) ,
2021-01-12 17:24:18 -08:00
SqliteClientError ::TableNotEmpty = > write! ( f , " Table is not empty " ) ,
2022-08-30 15:54:58 -07:00
#[ cfg(feature = " unstable " ) ]
SqliteClientError ::TransactionIsMined ( txid ) = > write! ( f , " Cannot remove transaction {} which has already been mined " , txid ) ,
2021-01-12 17:24:18 -08:00
SqliteClientError ::DbError ( e ) = > write! ( f , " {} " , e ) ,
SqliteClientError ::Io ( e ) = > write! ( f , " {} " , e ) ,
SqliteClientError ::InvalidMemo ( e ) = > write! ( f , " {} " , e ) ,
2021-01-13 14:20:11 -08:00
SqliteClientError ::BackendError ( e ) = > write! ( f , " {} " , e ) ,
2021-01-12 17:24:18 -08:00
}
2019-03-08 19:12:31 -08:00
}
}
2020-08-05 18:14:45 -07:00
impl From < rusqlite ::Error > for SqliteClientError {
fn from ( e : rusqlite ::Error ) -> Self {
2021-01-12 17:24:18 -08:00
SqliteClientError ::DbError ( e )
2019-03-08 18:16:00 -08:00
}
}
2020-08-05 18:14:45 -07:00
impl From < std ::io ::Error > for SqliteClientError {
2019-03-08 19:12:31 -08:00
fn from ( e : std ::io ::Error ) -> Self {
2021-01-12 17:24:18 -08:00
SqliteClientError ::Io ( e )
2019-03-08 19:12:31 -08:00
}
}
2022-08-15 12:14:15 -07:00
impl From < Bech32DecodeError > for SqliteClientError {
fn from ( e : Bech32DecodeError ) -> Self {
SqliteClientError ::Bech32DecodeError ( e )
2019-03-08 18:16:00 -08:00
}
}
2020-08-26 14:47:47 -07:00
2021-01-12 17:24:18 -08:00
impl From < bs58 ::decode ::Error > for SqliteClientError {
fn from ( e : bs58 ::decode ::Error ) -> Self {
SqliteClientError ::Base58 ( e )
2020-08-26 14:47:47 -07:00
}
}
2021-01-12 17:24:18 -08:00
2020-10-29 09:48:26 -07:00
impl From < zcash_primitives ::memo ::Error > for SqliteClientError {
fn from ( e : zcash_primitives ::memo ::Error ) -> Self {
SqliteClientError ::InvalidMemo ( e )
}
}
2021-01-13 14:20:11 -08:00
impl From < data_api ::error ::Error < NoteId > > for SqliteClientError {
fn from ( e : data_api ::error ::Error < NoteId > ) -> Self {
SqliteClientError ::BackendError ( e )
}
2021-01-12 17:24:18 -08:00
}