2021-11-25 07:56:21 +00:00
#![ warn(missing_docs) ]
//! Error handling for various aspects of the PeachCloud system, including the network, OLED, stats and dyndns JSON-RPC clients, as well as the configuration manager, sbot client and password utilities.
2022-07-04 12:35:17 +00:00
use golgi ::GolgiError ;
2022-07-04 12:54:00 +00:00
use std ::{ io , str , string } ;
2021-11-25 07:56:21 +00:00
/// This type represents all possible errors that can occur when interacting with the PeachCloud library.
#[ derive(Debug) ]
2021-08-06 17:58:40 +00:00
pub enum PeachError {
2022-05-09 13:53:03 +00:00
/// Represents looking up a Config value with a non-existent key
InvalidKey {
2022-05-10 10:59:36 +00:00
/// the key value which was invalid
key : String ,
2022-05-09 13:53:03 +00:00
} ,
2022-02-01 08:03:46 +00:00
/// Represents a failure to determine the path of the user's home directory.
HomeDir ,
2021-11-25 07:56:21 +00:00
/// Represents all other cases of `std::io::Error`.
Io ( io ::Error ) ,
/// Represents a JSON-RPC core error returned from a JSON-RPC client.
JsonRpcClientCore ( jsonrpc_client_core ::Error ) ,
/// Represents a JSON-RPC core error returned from a JSON-RPC server.
JsonRpcCore ( jsonrpc_core ::Error ) ,
/// Represents a JSON-RPC HTTP error returned from a JSON-RPC client.
JsonRpcHttp ( jsonrpc_client_http ::Error ) ,
/// Represents a failure to update the nameserver.
NsUpdate {
/// A message describing the context of the attempted nameserver update.
msg : String ,
2021-08-06 17:58:40 +00:00
} ,
2021-11-25 07:56:21 +00:00
/// Represents a failure to parse a string slice to a boolean value.
ParseBool ( str ::ParseBoolError ) ,
/// Represents a failure to parse a `DateTime`. Includes the error source and the file path
/// used in the parse attempt.
ParseDateTime {
/// The underlying source of the error.
source : chrono ::ParseError ,
/// The file path for the parse attempt.
path : String ,
2021-08-06 17:58:40 +00:00
} ,
2021-11-25 07:56:21 +00:00
/// Represents the submission of an incorrect admin password.
PasswordIncorrect ,
/// Represents the submission of two passwords which do not match.
PasswordMismatch ,
/// Represents an unset admin password (empty password hash value) in the config file.
PasswordNotSet ,
/// Represents a failure to read from input. Includes the error source and the file path used
/// in the read attempt.
Read {
/// The underlying source of the error.
source : io ::Error ,
/// The file path for the read attempt.
2021-08-06 17:58:40 +00:00
path : String ,
} ,
2021-11-25 07:56:21 +00:00
/// Represents a failure to parse or compile a regular expression.
Regex ( regex ::Error ) ,
2022-03-04 08:53:49 +00:00
/// Represents a failure to successfully execute an sbot command (via golgi).
Sbot ( String ) ,
2021-11-25 07:56:21 +00:00
/// Represents a failure to serialize or deserialize JSON.
SerdeJson ( serde_json ::error ::Error ) ,
2022-02-01 08:03:46 +00:00
/// Represents a failure to deserialize TOML.
2022-02-01 14:07:19 +00:00
TomlDeser ( toml ::de ::Error ) ,
/// Represents a failure to serialize TOML.
TomlSer ( toml ::ser ::Error ) ,
2022-02-01 08:03:46 +00:00
2021-11-25 07:56:21 +00:00
/// Represents a failure to serialize or deserialize YAML.
SerdeYaml ( serde_yaml ::Error ) ,
/// Represents a failure to find the given SSB ID in the config file.
SsbAdminIdNotFound {
/// An SSB ID (public key).
id : String ,
} ,
/// Represents a failure to interpret a sequence of u8 as a string slice.
Utf8ToStr ( str ::Utf8Error ) ,
/// Represents a failure to interpret a sequence of u8 as a String.
Utf8ToString ( string ::FromUtf8Error ) ,
/// Represents a failure to write to output. Includes the error source and the file path used
/// in the write attempt.
Write {
/// The underlying source of the error.
source : io ::Error ,
2022-02-01 08:03:46 +00:00
/// The file path for the write attempt.
2021-11-25 07:56:21 +00:00
path : String ,
} ,
2022-07-04 12:35:17 +00:00
/// Represents a Golgi error
Golgi ( GolgiError ) ,
2022-07-28 12:53:45 +00:00
/// Represents a generic system error, whose details are specified in the string message
System ( String ) ,
2021-08-06 17:58:40 +00:00
}
2021-11-25 07:56:21 +00:00
impl std ::error ::Error for PeachError {
fn source ( & self ) -> Option < & ( dyn std ::error ::Error + 'static ) > {
match * self {
2022-02-01 08:03:46 +00:00
PeachError ::HomeDir = > None ,
2022-05-10 10:59:36 +00:00
PeachError ::InvalidKey { .. } = > None ,
2021-11-25 07:56:21 +00:00
PeachError ::Io ( _ ) = > None ,
PeachError ::JsonRpcClientCore ( _ ) = > None ,
PeachError ::JsonRpcCore ( _ ) = > None ,
PeachError ::JsonRpcHttp ( _ ) = > None ,
PeachError ::NsUpdate { .. } = > None ,
PeachError ::ParseBool ( _ ) = > None ,
PeachError ::ParseDateTime { ref source , .. } = > Some ( source ) ,
PeachError ::PasswordIncorrect = > None ,
PeachError ::PasswordMismatch = > None ,
PeachError ::PasswordNotSet = > None ,
PeachError ::Read { ref source , .. } = > Some ( source ) ,
PeachError ::Regex ( _ ) = > None ,
2022-03-04 08:53:49 +00:00
PeachError ::Sbot ( _ ) = > None ,
2021-11-25 07:56:21 +00:00
PeachError ::SerdeJson ( _ ) = > None ,
PeachError ::SerdeYaml ( _ ) = > None ,
PeachError ::SsbAdminIdNotFound { .. } = > None ,
2022-02-01 14:07:19 +00:00
PeachError ::TomlDeser ( _ ) = > None ,
PeachError ::TomlSer ( _ ) = > None ,
2021-11-25 07:56:21 +00:00
PeachError ::Utf8ToStr ( _ ) = > None ,
PeachError ::Utf8ToString ( _ ) = > None ,
PeachError ::Write { ref source , .. } = > Some ( source ) ,
2022-07-04 12:35:17 +00:00
PeachError ::Golgi ( _ ) = > None ,
2022-07-28 12:53:45 +00:00
PeachError ::System ( _ ) = > None ,
2021-11-25 07:56:21 +00:00
}
2021-08-06 17:58:40 +00:00
}
}
2021-11-25 07:56:21 +00:00
impl std ::fmt ::Display for PeachError {
fn fmt ( & self , f : & mut std ::fmt ::Formatter ) -> std ::fmt ::Result {
match * self {
2022-05-10 10:59:36 +00:00
PeachError ::InvalidKey { ref key } = > {
write! ( f , " Invalid key in config lookup for key: {} " , key )
2022-05-09 13:53:03 +00:00
}
2022-02-01 08:03:46 +00:00
PeachError ::HomeDir = > {
write! (
f ,
" Unable to determine the path of the user's home directory "
)
}
2021-11-25 07:56:21 +00:00
PeachError ::Io ( ref err ) = > err . fmt ( f ) ,
PeachError ::JsonRpcClientCore ( ref err ) = > err . fmt ( f ) ,
PeachError ::JsonRpcCore ( ref err ) = > {
write! ( f , " {:?} " , err )
}
PeachError ::JsonRpcHttp ( ref err ) = > err . fmt ( f ) ,
PeachError ::NsUpdate { ref msg } = > {
write! ( f , " Nameserver error: {} " , msg )
}
PeachError ::ParseBool ( ref err ) = > err . fmt ( f ) ,
PeachError ::ParseDateTime { ref path , .. } = > {
write! ( f , " Date/time parse error: {} " , path )
}
PeachError ::PasswordIncorrect = > {
2022-03-04 08:53:49 +00:00
write! ( f , " password is incorrect " )
2021-11-25 07:56:21 +00:00
}
PeachError ::PasswordMismatch = > {
2022-03-04 08:53:49 +00:00
write! ( f , " passwords do not match " )
2021-11-25 07:56:21 +00:00
}
PeachError ::PasswordNotSet = > {
2022-03-04 08:53:49 +00:00
write! ( f , " hash value in YAML configuration file is empty " )
2021-11-25 07:56:21 +00:00
}
PeachError ::Read { ref path , .. } = > {
write! ( f , " Read error: {} " , path )
}
PeachError ::Regex ( ref err ) = > err . fmt ( f ) ,
2022-03-04 08:53:49 +00:00
PeachError ::Sbot ( ref msg ) = > {
2021-11-25 07:56:21 +00:00
write! ( f , " Sbot error: {} " , msg )
}
PeachError ::SerdeJson ( ref err ) = > err . fmt ( f ) ,
PeachError ::SerdeYaml ( ref err ) = > err . fmt ( f ) ,
PeachError ::SsbAdminIdNotFound { ref id } = > {
write! ( f , " Config error: SSB admin ID `{}` not found " , id )
}
2022-02-01 14:07:19 +00:00
PeachError ::TomlDeser ( ref err ) = > err . fmt ( f ) ,
PeachError ::TomlSer ( ref err ) = > err . fmt ( f ) ,
2021-11-25 07:56:21 +00:00
PeachError ::Utf8ToStr ( ref err ) = > err . fmt ( f ) ,
PeachError ::Utf8ToString ( ref err ) = > err . fmt ( f ) ,
PeachError ::Write { ref path , .. } = > {
write! ( f , " Write error: {} " , path )
}
2022-07-04 12:35:17 +00:00
PeachError ::Golgi ( ref err ) = > err . fmt ( f ) ,
2022-07-28 12:53:45 +00:00
PeachError ::System ( ref msg ) = > {
write! ( f , " system error: {} " , msg )
}
2021-11-25 07:56:21 +00:00
}
2021-08-06 17:58:40 +00:00
}
}
2021-11-25 07:56:21 +00:00
impl From < std ::io ::Error > for PeachError {
fn from ( err : std ::io ::Error ) -> PeachError {
PeachError ::Io ( err )
2021-08-06 17:58:40 +00:00
}
}
2021-11-25 07:56:21 +00:00
impl From < jsonrpc_client_core ::Error > for PeachError {
fn from ( err : jsonrpc_client_core ::Error ) -> PeachError {
PeachError ::JsonRpcClientCore ( err )
2021-08-06 17:58:40 +00:00
}
}
2021-11-25 07:56:21 +00:00
impl From < jsonrpc_client_http ::Error > for PeachError {
fn from ( err : jsonrpc_client_http ::Error ) -> PeachError {
PeachError ::JsonRpcHttp ( err )
}
}
impl From < str ::ParseBoolError > for PeachError {
fn from ( err : str ::ParseBoolError ) -> PeachError {
PeachError ::ParseBool ( err )
2021-08-06 17:58:40 +00:00
}
}
impl From < regex ::Error > for PeachError {
fn from ( err : regex ::Error ) -> PeachError {
2021-11-25 07:56:21 +00:00
PeachError ::Regex ( err )
2021-08-06 17:58:40 +00:00
}
}
2021-11-25 07:56:21 +00:00
impl From < serde_json ::error ::Error > for PeachError {
fn from ( err : serde_json ::error ::Error ) -> PeachError {
PeachError ::SerdeJson ( err )
2021-08-06 17:58:40 +00:00
}
}
2021-11-25 07:56:21 +00:00
impl From < serde_yaml ::Error > for PeachError {
fn from ( err : serde_yaml ::Error ) -> PeachError {
PeachError ::SerdeYaml ( err )
2021-08-06 17:58:40 +00:00
}
}
2022-02-01 08:03:46 +00:00
impl From < toml ::de ::Error > for PeachError {
fn from ( err : toml ::de ::Error ) -> PeachError {
2022-02-01 14:07:19 +00:00
PeachError ::TomlDeser ( err )
}
}
impl From < toml ::ser ::Error > for PeachError {
fn from ( err : toml ::ser ::Error ) -> PeachError {
PeachError ::TomlSer ( err )
2022-02-01 08:03:46 +00:00
}
}
2021-11-25 07:56:21 +00:00
impl From < str ::Utf8Error > for PeachError {
fn from ( err : str ::Utf8Error ) -> PeachError {
PeachError ::Utf8ToStr ( err )
}
}
impl From < string ::FromUtf8Error > for PeachError {
fn from ( err : string ::FromUtf8Error ) -> PeachError {
PeachError ::Utf8ToString ( err )
2021-08-06 17:58:40 +00:00
}
}
2022-07-04 11:23:55 +00:00
impl From < GolgiError > for PeachError {
fn from ( err : GolgiError ) -> PeachError {
PeachError ::Golgi ( err )
}
2022-07-04 12:54:00 +00:00
}
2022-07-28 12:53:45 +00:00