Skip to content

Commit

Permalink
Add jade error codes to api
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardparis committed Apr 15, 2024
1 parent 2ed135b commit e5c4a68
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/jade/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ impl<T> Response<T> {
}
}

#[derive(Debug, PartialEq, Eq)]
#[repr(i32)]
pub enum ErrorCode {
InvalidRequest = -32600,
UnknownMethod = -32601,
BadParameters = -32602,
InternalError = -32603,
UserCancelled = -32000,
ProtocolError = -32001,
HwLocked = -32002,
NetworkMismatch = -32003,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Error {
pub code: i32,
Expand Down
19 changes: 9 additions & 10 deletions src/jade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,6 @@ impl std::fmt::Display for TransportError {

#[derive(Debug)]
pub enum JadeError {
DeviceNotFound,
DeviceDidNotSign,
UserCancelled,
Transport(TransportError),
Rpc(api::Error),
PinServer(pinserver::Error),
Expand All @@ -529,11 +526,8 @@ impl From<pinserver::Error> for JadeError {
impl std::fmt::Display for JadeError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::DeviceNotFound => write!(f, "Jade not found"),
Self::DeviceDidNotSign => write!(f, "Jade did not sign the psbt"),
Self::Transport(e) => write!(f, "{}", e),
Self::Rpc(e) => write!(f, "{:?}", e),
Self::UserCancelled => write!(f, "User cancelled operation"),
Self::PinServer(e) => write!(f, "{:?}", e),
Self::HandShakeRefused => write!(f, "Handshake with pinserver refused"),
}
Expand All @@ -543,12 +537,17 @@ impl std::fmt::Display for JadeError {
impl From<JadeError> for HWIError {
fn from(e: JadeError) -> HWIError {
match e {
JadeError::DeviceNotFound => HWIError::DeviceNotFound,
JadeError::DeviceDidNotSign => HWIError::DeviceDidNotSign,
JadeError::Transport(e) => HWIError::Device(e.to_string()),
JadeError::Rpc(e) => HWIError::Device(format!("{:?}", e)),
JadeError::Rpc(e) => {
if e.code == api::ErrorCode::UserCancelled as i32 {
HWIError::UserRefused
} else if e.code == api::ErrorCode::NetworkMismatch as i32 {
HWIError::NetworkMismatch
} else {
HWIError::Device(format!("{:?}", e))
}
}
JadeError::PinServer(e) => HWIError::Device(format!("{:?}", e)),
JadeError::UserCancelled => HWIError::UserRefused,
JadeError::HandShakeRefused => {
HWIError::Device("Handshake with pinserver refused".to_string())
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum Error {
Device(String),
Unexpected(&'static str),
UserRefused,
NetworkMismatch,
}

impl std::fmt::Display for Error {
Expand All @@ -50,6 +51,7 @@ impl std::fmt::Display for Error {
Error::InvalidParameter(param, e) => write!(f, "Invalid parameter {}: {}", param, e),
Error::Unexpected(e) => write!(f, "{}", e),
Error::UserRefused => write!(f, "User refused operation"),
Error::NetworkMismatch => write!(f, "Device network is different"),
}
}
}
Expand Down

0 comments on commit e5c4a68

Please sign in to comment.