eyre work

This commit is contained in:
John Smith
2022-07-06 23:15:51 -04:00
parent 2f05611170
commit cd0cd78e30
21 changed files with 345 additions and 229 deletions

View File

@@ -135,18 +135,14 @@ impl VeilidAPI {
let config = self.config()?;
let args = args.trim_start();
if args.is_empty() {
return config
.get_key_json("")
.map_err(|e| VeilidAPIError::Internal { message: e });
return config.get_key_json("");
}
let (arg, rest) = args.split_once(' ').unwrap_or((args, ""));
let rest = rest.trim_start().to_owned();
// One argument is 'config get'
if rest.is_empty() {
return config
.get_key_json(arg)
.map_err(|e| VeilidAPIError::Internal { message: e });
return config.get_key_json(arg);
}
// More than one argument is 'config set'
@@ -156,15 +152,11 @@ impl VeilidAPI {
self.get_state().await?.attachment.state,
AttachmentState::Detached
) {
return Err(VeilidAPIError::Internal {
message: "Must be detached to change config".to_owned(),
});
apibail_internal!("Must be detached to change config");
}
// Change the config key
config
.set_key_json(arg, &rest)
.map_err(|e| VeilidAPIError::Internal { message: e })?;
config.set_key_json(arg, &rest)?;
Ok("Config value set".to_owned())
}
@@ -177,9 +169,7 @@ impl VeilidAPI {
self.get_state().await?.attachment.state,
AttachmentState::Detached | AttachmentState::Detaching
) {
return Err(VeilidAPIError::Internal {
message: "Must be detached to purge".to_owned(),
});
apibail_internal!("Must be detached to purge");
}
self.network_manager()?.routing_table().purge();
Ok("Buckets purged".to_owned())
@@ -203,10 +193,8 @@ impl VeilidAPI {
self.get_state().await?.attachment.state,
AttachmentState::Detached
) {
return Err(VeilidAPIError::Internal {
message: "Not detached".to_owned(),
});
};
apibail_internal!("Not detached");
}
self.attach().await?;
@@ -218,9 +206,7 @@ impl VeilidAPI {
self.get_state().await?.attachment.state,
AttachmentState::Detaching
) {
return Err(VeilidAPIError::Internal {
message: "Not attached".to_owned(),
});
apibail_internal!("Not attached");
};
self.detach().await?;

View File

@@ -31,6 +31,62 @@ use xx::*;
/////////////////////////////////////////////////////////////////////////////////////////////////////
#[allow(unused_macros)]
#[macro_export]
macro_rules! apierr_generic {
($x:expr) => {
Err(VeilidAPIError::generic($x))
};
}
#[allow(unused_macros)]
#[macro_export]
macro_rules! apierr_internal {
($x:expr) => {
Err(VeilidAPIError::internal($x))
};
}
#[allow(unused_macros)]
#[macro_export]
macro_rules! apierr_parse {
($x:expr, $y:expr) => {
Err(VeilidAPIError::parse_error($x, $y))
};
}
#[allow(unused_macros)]
#[macro_export]
macro_rules! mapapierr_parse {
($x:expr) => {
|e| VeilidAPIError::parse_error($x, e)
};
}
#[allow(unused_macros)]
#[macro_export]
macro_rules! apibail_generic {
($x:expr) => {
return Err(VeilidAPIError::generic($x));
};
}
#[allow(unused_macros)]
#[macro_export]
macro_rules! apibail_internal {
($x:expr) => {
return Err(VeilidAPIError::internal($x));
};
}
#[allow(unused_macros)]
#[macro_export]
macro_rules! apibail_parse {
($x:expr, $y:expr) => {
return Err(VeilidAPIError::parse_error($x, $y));
};
}
#[derive(Clone, Debug, PartialOrd, PartialEq, Eq, Ord, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum VeilidAPIError {
@@ -66,8 +122,63 @@ pub enum VeilidAPIError {
context: String,
argument: String,
},
Generic {
message: String,
},
}
impl VeilidAPIError {
pub fn node_not_found(node_id: NodeId) -> Self {
Self::NodeNotFound { node_id }
}
pub fn no_dial_info(node_id: NodeId) -> Self {
Self::NoDialInfo { node_id }
}
pub fn no_peer_info(node_id: NodeId) -> Self {
Self::NoPeerInfo { node_id }
}
pub fn internal<T: ToString>(msg: T) -> Self {
Self::Internal {
message: msg.to_string(),
}
}
pub fn unimplemented<T: ToString>(msg: T) -> Self {
Self::Unimplemented {
message: msg.to_string(),
}
}
pub fn parse_error<T: ToString, S: ToString>(msg: T, value: S) -> Self {
Self::ParseError {
message: msg.to_string(),
value: value.to_string(),
}
}
pub fn invalid_argument<T: ToString, S: ToString, R: ToString>(
context: T,
argument: S,
value: R,
) -> Self {
Self::InvalidArgument {
context: context.to_string(),
argument: argument.to_string(),
value: value.to_string(),
}
}
pub fn missing_argument<T: ToString, S: ToString>(context: T, argument: S) -> Self {
Self::MissingArgument {
context: context.to_string(),
argument: argument.to_string(),
}
}
pub fn generic<T: ToString>(msg: T) -> Self {
Self::Generic {
message: msg.to_string(),
}
}
}
impl std::error::Error for VeilidAPIError {}
impl fmt::Display for VeilidAPIError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match self {
@@ -111,6 +222,9 @@ impl fmt::Display for VeilidAPIError {
context, argument
)
}
VeilidAPIError::Generic { message } => {
write!(f, "VeilidAPIError::Generic({})", message)
}
}
}
}
@@ -1321,9 +1435,11 @@ impl SignedNodeInfo {
node_id: NodeId,
signature: DHTSignature,
timestamp: u64,
) -> Result<Self, String> {
let mut node_info_bytes = serde_cbor::to_vec(&node_info).map_err(map_to_string)?;
let mut timestamp_bytes = serde_cbor::to_vec(&timestamp).map_err(map_to_string)?;
) -> Result<Self, VeilidAPIError> {
let mut node_info_bytes = serde_cbor::to_vec(&node_info)
.map_err(mapapierr_parse!("failed to encode node info as cbor"))?;
let mut timestamp_bytes = serde_cbor::to_vec(&timestamp)
.map_err(mapapierr_parse!("failed to encode timestamp as cbor"))?;
node_info_bytes.append(&mut timestamp_bytes);
@@ -1339,11 +1455,13 @@ impl SignedNodeInfo {
node_info: NodeInfo,
node_id: NodeId,
secret: &DHTKeySecret,
) -> Result<Self, String> {
) -> Result<Self, VeilidAPIError> {
let timestamp = intf::get_timestamp();
let mut node_info_bytes = serde_cbor::to_vec(&node_info).map_err(map_to_string)?;
let mut timestamp_bytes = serde_cbor::to_vec(&timestamp).map_err(map_to_string)?;
let mut node_info_bytes = serde_cbor::to_vec(&node_info)
.map_err(mapapierr_parse!("failed to encode node info as cbor"))?;
let mut timestamp_bytes = serde_cbor::to_vec(&timestamp)
.map_err(mapapierr_parse!("failed to encode timestamp as cbor"))?;
node_info_bytes.append(&mut timestamp_bytes);