remove json-rpc error code and improve docs

This commit is contained in:
glyph 2021-12-13 10:54:51 +02:00
parent 4f5eb3aa04
commit 9f40378fce
1 changed files with 146 additions and 277 deletions

View File

@ -1,81 +1,166 @@
//! Custom error type for `peach-network`.
use std::io;
use std::num::ParseIntError;
use io::Error as IoError;
use jsonrpc_core::{types::error::Error as JsonRpcError, ErrorCode};
use probes::ProbeError;
use regex::Error as RegexError;
use serde_json::Error as SerdeError;
use wpactrl::WpaError;
/// Custom error type encapsulating all possible errors when querying
/// network interfaces and modifying their state.
#[derive(Debug)]
pub enum NetworkError {
Add { ssid: String },
NoState { iface: String, source: IoError },
Disable { id: String, iface: String },
Disconnect { iface: String },
GenWpaPassphrase { ssid: String, source: IoError },
GenWpaPassphraseWarning { ssid: String, err_msg: String },
Id { ssid: String, iface: String },
NoIp { iface: String, source: IoError },
Rssi { iface: String },
RssiPercent { iface: String },
Ssid { iface: String },
State { iface: String },
Status { iface: String },
Traffic { iface: String },
/// Failed to add network.
Add {
/// SSID.
ssid: String,
},
/// Failed to retrieve network state.
NoState {
/// Interface.
iface: String,
/// Underlying error source.
source: IoError,
},
/// Failed to disable network.
Disable {
/// ID.
id: String,
/// Interface.
iface: String,
},
/// Failed to disconnect interface.
Disconnect {
/// Interface.
iface: String,
},
/// Failed to execute wpa_passphrase command.
GenWpaPassphrase {
/// SSID.
ssid: String,
/// Underlying error source.
source: IoError,
},
/// Failed to successfully generate wpa passphrase.
GenWpaPassphraseWarning {
/// SSID.
ssid: String,
/// Error message describing context.
err_msg: String,
},
/// Failed to retrieve ID for the given SSID and interface.
Id {
/// SSID.
ssid: String,
/// Interface.
iface: String,
},
/// Failed to retrieve IP address.
NoIp {
/// Inteface.
iface: String,
/// Underlying error source.
source: IoError,
},
/// Failed to retrieve RSSI.
Rssi {
/// Interface.
iface: String,
},
/// Failed to retrieve signal quality (%).
RssiPercent {
/// Interface.
iface: String,
},
/// Failed to retrieve SSID.
Ssid {
/// Interface.
iface: String,
},
/// Failed to retrieve state.
State {
/// Interface.
iface: String,
},
/// Failed to retrieve status.
Status {
/// Interface.
iface: String,
},
/// Failed to retieve network traffic.
Traffic {
/// Interface.
iface: String,
},
/// No saved network found for the default interface.
SavedNetworks,
AvailableNetworks { iface: String },
MissingParams(JsonRpcError),
Modify { id: String, iface: String },
Ip { iface: String },
/// No networks found in range.
AvailableNetworks {
/// Interface.
iface: String,
},
/// Failed to set new password.
Modify {
/// ID.
id: String,
/// Interface.
iface: String,
},
/// Failed to retrieve IP address.
Ip {
/// Interface.
iface: String,
},
/// Failed to parse integer from string.
ParseInt(ParseIntError),
NoTraffic { iface: String, source: ProbeError },
Reassociate { iface: String },
/// Failed to retrieve network traffic measurement.
NoTraffic {
/// Interface.
iface: String,
/// Underlying error source.
source: ProbeError,
},
/// Failed to reassociate with WiFi network.
Reassociate {
/// Interface.
iface: String,
},
/// Failed to force reread of wpa_supplicant configuration file.
Reconfigure,
Reconnect { iface: String },
/// Failed to reconnect with WiFi network.
Reconnect {
/// Interface.
iface: String,
},
/// Failed to execute Regex command.
Regex(RegexError),
Delete { id: String, iface: String },
/// Failed to delete network.
Delete {
/// ID.
id: String,
/// Interface.
iface: String,
},
/// Failed to retrieve state of wlan0 service.
WlanState(IoError),
/// Failed to retrieve connection state of wlan0 interface.
WlanOperstate(IoError),
/// Failed to save wpa_supplicant configuration changes to file.
Save,
Connect { id: String, iface: String },
/// Failed to connect to network.
Connect {
/// ID.
id: String,
/// Interface.
iface: String,
},
/// Failed to start ap0 service.
StartAp0(IoError),
/// Failed to start wlan0 service.
StartWlan0(IoError),
SerdeSerialize(SerdeError),
/// Failed to execute wpa-ctrl command.
WpaCtrl(WpaError),
}
@ -98,7 +183,6 @@ impl std::error::Error for NetworkError {
NetworkError::Traffic { .. } => None,
NetworkError::SavedNetworks => None,
NetworkError::AvailableNetworks { .. } => None,
NetworkError::MissingParams(ref source) => Some(source),
NetworkError::Modify { .. } => None,
NetworkError::Ip { .. } => None,
NetworkError::ParseInt(ref source) => Some(source),
@ -114,7 +198,6 @@ impl std::error::Error for NetworkError {
NetworkError::Connect { .. } => None,
NetworkError::StartWlan0(ref source) => Some(source),
NetworkError::StartAp0(ref source) => Some(source),
NetworkError::SerdeSerialize(ref source) => Some(source),
NetworkError::WpaCtrl(ref source) => Some(source),
}
}
@ -181,20 +264,13 @@ impl std::fmt::Display for NetworkError {
write!(f, "No status found for interface: {}", iface)
}
NetworkError::Traffic { ref iface } => {
write!(
f,
"Could not find network traffice for interface: {}",
iface
)
write!(f, "Could not find network traffic for interface: {}", iface)
}
NetworkError::SavedNetworks => {
write!(f, "No saved networks found for default interface")
}
NetworkError::AvailableNetworks { ref iface } => {
write!(f, "No networks found in range of interface: {}", iface)
}
NetworkError::MissingParams(ref source) => {
write!(f, "Missing expected parameters: {}", source)
write!(f, "No networks found in range of interface: {}", iface)
}
NetworkError::Modify { ref id, ref iface } => {
write!(
@ -258,18 +334,11 @@ impl std::fmt::Display for NetworkError {
}
NetworkError::StartWlan0(_) => write!(f, "Failed to start ap0 service"),
NetworkError::StartAp0(_) => write!(f, "Failed to start wlan0 service"),
NetworkError::SerdeSerialize(_) => write!(f, "JSON serialization failed"),
NetworkError::WpaCtrl(_) => write!(f, "WpaCtrl command failed"),
}
}
}
impl From<SerdeError> for NetworkError {
fn from(err: SerdeError) -> Self {
NetworkError::SerdeSerialize(err)
}
}
impl From<WpaError> for NetworkError {
fn from(err: WpaError) -> Self {
NetworkError::WpaCtrl(err)
@ -287,203 +356,3 @@ impl From<RegexError> for NetworkError {
NetworkError::Regex(err)
}
}
impl From<NetworkError> for JsonRpcError {
fn from(err: NetworkError) -> Self {
match &err {
NetworkError::Add { ssid } => JsonRpcError {
code: ErrorCode::ServerError(-32000),
message: format!("Failed to add network for {}", ssid),
data: None,
},
NetworkError::NoState { iface, source } => JsonRpcError {
code: ErrorCode::ServerError(-32022),
message: format!(
"Failed to retrieve interface state for {}: {}",
iface, source
),
data: None,
},
NetworkError::Disable { id, iface } => JsonRpcError {
code: ErrorCode::ServerError(-32029),
message: format!("Failed to disable network {} for {}", id, iface),
data: None,
},
NetworkError::Disconnect { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32032),
message: format!("Failed to disconnect {}", iface),
data: None,
},
NetworkError::GenWpaPassphrase { ssid, source } => JsonRpcError {
code: ErrorCode::ServerError(-32025),
message: format!("Failed to generate wpa passphrase for {}: {}", ssid, source),
data: None,
},
NetworkError::GenWpaPassphraseWarning { ssid, err_msg } => JsonRpcError {
code: ErrorCode::ServerError(-32036),
message: format!(
"Failed to generate wpa passphrase for {}: {}",
ssid, err_msg
),
data: None,
},
NetworkError::Id { iface, ssid } => JsonRpcError {
code: ErrorCode::ServerError(-32026),
message: format!("No ID found for {} on interface {}", ssid, iface),
data: None,
},
NetworkError::NoIp { iface, source } => JsonRpcError {
code: ErrorCode::ServerError(-32001),
message: format!("Failed to retrieve IP address for {}: {}", iface, source),
data: None,
},
NetworkError::Rssi { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32002),
message: format!(
"Failed to retrieve RSSI for {}. Interface may not be connected",
iface
),
data: None,
},
NetworkError::RssiPercent { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32034),
message: format!(
"Failed to retrieve signal quality (%) for {}. Interface may not be connected",
iface
),
data: None,
},
NetworkError::Ssid { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32003),
message: format!(
"Failed to retrieve SSID for {}. Interface may not be connected",
iface
),
data: None,
},
NetworkError::State { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32023),
message: format!("No state found for {}. Interface may not exist", iface),
data: None,
},
NetworkError::Status { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32024),
message: format!("No status found for {}. Interface may not exist", iface),
data: None,
},
NetworkError::Traffic { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32004),
message: format!(
"No network traffic statistics found for {}. Interface may not exist",
iface
),
data: None,
},
NetworkError::SavedNetworks => JsonRpcError {
code: ErrorCode::ServerError(-32005),
message: "No saved networks found".to_string(),
data: None,
},
NetworkError::AvailableNetworks { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32006),
message: format!("No networks found in range of {}", iface),
data: None,
},
NetworkError::MissingParams(e) => e.clone(),
NetworkError::Modify { id, iface } => JsonRpcError {
code: ErrorCode::ServerError(-32033),
message: format!("Failed to set new password for network {} on {}", id, iface),
data: None,
},
NetworkError::Ip { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32007),
message: format!("No IP address found for {}", iface),
data: None,
},
NetworkError::ParseInt(source) => JsonRpcError {
code: ErrorCode::ServerError(-32035),
message: format!(
"Failed to parse integer from string for RSSI value: {}",
source
),
data: None,
},
NetworkError::NoTraffic { iface, source } => JsonRpcError {
code: ErrorCode::ServerError(-32015),
message: format!(
"Failed to retrieve network traffic statistics for {}: {}",
iface, source
),
data: None,
},
NetworkError::Reassociate { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32008),
message: format!("Failed to reassociate with WiFi network for {}", iface),
data: None,
},
NetworkError::Reconfigure => JsonRpcError {
code: ErrorCode::ServerError(-32030),
message: "Failed to force reread of wpa_supplicant configuration file".to_string(),
data: None,
},
NetworkError::Reconnect { iface } => JsonRpcError {
code: ErrorCode::ServerError(-32009),
message: format!("Failed to reconnect with WiFi network for {}", iface),
data: None,
},
NetworkError::Regex(source) => JsonRpcError {
code: ErrorCode::ServerError(-32010),
message: format!("Regex command error: {}", source),
data: None,
},
NetworkError::Delete { id, iface } => JsonRpcError {
code: ErrorCode::ServerError(-32028),
message: format!("Failed to delete network {} for {}", id, iface),
data: None,
},
NetworkError::WlanState(source) => JsonRpcError {
code: ErrorCode::ServerError(-32011),
message: format!("Failed to retrieve state of wlan0 service: {}", source),
data: None,
},
NetworkError::WlanOperstate(source) => JsonRpcError {
code: ErrorCode::ServerError(-32021),
message: format!(
"Failed to retrieve connection state of wlan0 interface: {}",
source
),
data: None,
},
NetworkError::Save => JsonRpcError {
code: ErrorCode::ServerError(-32031),
message: "Failed to save configuration changes to file".to_string(),
data: None,
},
NetworkError::Connect { id, iface } => JsonRpcError {
code: ErrorCode::ServerError(-32027),
message: format!("Failed to connect to network {} for {}", id, iface),
data: None,
},
NetworkError::StartAp0(source) => JsonRpcError {
code: ErrorCode::ServerError(-32016),
message: format!("Failed to start ap0 service: {}", source),
data: None,
},
NetworkError::StartWlan0(source) => JsonRpcError {
code: ErrorCode::ServerError(-32018),
message: format!("Failed to start wlan0 service: {}", source),
data: None,
},
NetworkError::SerdeSerialize(source) => JsonRpcError {
code: ErrorCode::ServerError(-32012),
message: format!("JSON serialization failed: {}", source),
data: None,
},
NetworkError::WpaCtrl(source) => JsonRpcError {
code: ErrorCode::ServerError(-32013),
message: format!("WPA control interface failure: {}", source),
data: None,
},
}
}
}