Skip to content

Commit

Permalink
use thiserror to define errors
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Oct 25, 2021
1 parent 31b76ff commit e7022cd
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 38 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ bytes = "1.1.0"
prost = "0.8.0"

futures = { version = "0.3", default-features = false, features = ["alloc"]}
thiserror = "1.0.30"

[dev-dependencies]
async-std = "1.3.0"
Expand Down
10 changes: 2 additions & 8 deletions examples/info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use libmavsdk::{info, RequestError, System};
use libmavsdk::System;
use std::io::{self, Write};

#[tokio::main]
Expand All @@ -24,12 +24,6 @@ async fn main() {

match system.info.get_version().await {
Ok(v) => println!("Version received: {:?}", v),
Err(RequestError::MavErr(info::InfoError::Unknown(s))) => {
println!("Unknown MAVLink error ({:?})", s);
}
Err(RequestError::MavErr(info::InfoError::InformationNotReceivedYet(s))) => {
println!("{}", s);
}
Err(RequestError::RpcErr(rpc_err)) => println!("RPC error: {:?}", rpc_err),
Err(e) => println!("{}", e),
};
}
7 changes: 2 additions & 5 deletions examples/mocap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_std::task;
use libmavsdk::{mocap, RequestError, System};
use libmavsdk::{mocap, System};
use std::io::{self, Write};
use std::time::Duration;

Expand Down Expand Up @@ -35,10 +35,7 @@ async fn main() {
.set_vision_position_estimate(vision_position_estimate.clone())
.await
{
match error {
RequestError::MavErr(mav_err) => println!("MAVLink error: {:?}", mav_err),
RequestError::RpcErr(rpc_err) => println!("RPC error: {:?}", rpc_err.message()),
}
println!("{}", error);
return;
};
counter -= 1;
Expand Down
18 changes: 10 additions & 8 deletions src/generated/info/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::super::FromRpcResponse;
use super::super::RequestError::{MavErr, RpcErr};
use super::super::RequestError;
use super::super::RequestResult;
use super::super::TonicResult;

Expand Down Expand Up @@ -45,9 +45,11 @@ impl From<&pb::Version> for Version {
}
}

#[derive(PartialEq, Clone, Debug)]
#[derive(PartialEq, Clone, Debug, thiserror::Error)]
pub enum InfoError {
#[error("Unknown error: {0}")]
Unknown(String),
#[error("Information not yet received: {0}")]
InformationNotReceivedYet(String),
}

Expand All @@ -65,31 +67,31 @@ impl FromRpcResponse<pb::GetVersionResponse> for GetVersionResult {
pb::info_result::Result::Success => {
match get_version_response.version {
Some(ref rpc_version) => Ok(Version::from(rpc_version)),
None => Err(MavErr(InfoError::Unknown(
None => Err(RequestError::Mav(InfoError::Unknown(
"Version does not received".into(),
))),
}
}
pb::info_result::Result::Unknown => Err(MavErr(
pb::info_result::Result::Unknown => Err(RequestError::Mav(
InfoError::Unknown(rpc_info_result.result_str.clone()),
)),
pb::info_result::Result::InformationNotReceivedYet => {
Err(MavErr(InfoError::InformationNotReceivedYet(
Err(RequestError::Mav(InfoError::InformationNotReceivedYet(
rpc_info_result.result_str.clone(),
)))
}
},
None => Err(MavErr(InfoError::Unknown(
None => Err(RequestError::Mav(InfoError::Unknown(
"Unsupported InfoResult.result value".into(),
))),
}
}
None => Err(MavErr(InfoError::Unknown(
None => Err(RequestError::Mav(InfoError::Unknown(
"InfoResult does not received".into(),
))),
}
}
Err(err) => Err(RpcErr(err)),
Err(err) => Err(RequestError::Rpc(err)),
}
}
}
Expand Down
22 changes: 13 additions & 9 deletions src/generated/mocap/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::super::FromRpcResponse;
use super::super::RequestError::{MavErr, RpcErr};
use super::super::RequestError;
use super::super::RequestResult;
use super::super::TonicResult;
use std::convert::Into;
Expand Down Expand Up @@ -225,15 +225,19 @@ impl Into<pb::Quaternion> for Quaternion {
}
}

#[derive(PartialEq, Clone, Debug)]
#[derive(PartialEq, Clone, Debug, thiserror::Error)]
pub enum MocapError {
/// Unknown error
#[error("Unknown error: {0}")]
Unknown(String),
/// No system is connected
#[error("No system is connected: {0}")]
NoSystem(String),
/// Connection error
#[error("Connection error: {0}")]
ConnectionError(String),
/// Invalid request data
#[error("Invalid request: {0}")]
InvalidRequestData(String),
}

Expand All @@ -253,34 +257,34 @@ impl FromRpcResponse<pb::SetVisionPositionEstimateResponse> for SetVisionPositio
match pb::mocap_result::Result::from_i32(rpc_mocap_result.result) {
Some(mocap_result) => match mocap_result {
pb::mocap_result::Result::Success => Ok(()),
pb::mocap_result::Result::Unknown => Err(MavErr(
pb::mocap_result::Result::Unknown => Err(RequestError::Mav(
MocapError::Unknown(rpc_mocap_result.result_str.clone()),
)),
pb::mocap_result::Result::NoSystem => Err(MavErr(
pb::mocap_result::Result::NoSystem => Err(RequestError::Mav(
MocapError::NoSystem(rpc_mocap_result.result_str.clone()),
)),
pb::mocap_result::Result::ConnectionError => {
Err(MavErr(MocapError::ConnectionError(
Err(RequestError::Mav(MocapError::ConnectionError(
rpc_mocap_result.result_str.clone(),
)))
}
pb::mocap_result::Result::InvalidRequestData => {
Err(MavErr(MocapError::InvalidRequestData(
Err(RequestError::Mav(MocapError::InvalidRequestData(
rpc_mocap_result.result_str.clone(),
)))
}
},
None => Err(MavErr(MocapError::Unknown(
None => Err(RequestError::Mav(MocapError::Unknown(
"Unsupported MocapResult.result value".into(),
))),
}
}
None => Err(MavErr(MocapError::Unknown(
None => Err(RequestError::Mav(MocapError::Unknown(
"MocapResult does not received".into(),
))),
}
}
Err(err) => Err(RpcErr(err)),
Err(err) => Err(RequestError::Rpc(err)),
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/generated/telemetry/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::super::RequestError::{MavErr, RpcErr};
use super::super::RequestError;
use super::super::RequestResult;
use futures::stream::{Stream, StreamExt};
use futures::task::{Context, Poll};
Expand Down Expand Up @@ -213,15 +213,19 @@ impl Into<pb::Quaternion> for Quaternion {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, thiserror::Error)]
pub enum TelemetryError {
/// Unknown error
#[error("Unknown error: {0}")]
Unknown(String),
/// No system is connected
#[error("No system is connected: {0}")]
NoSystem(String),
/// Connection error
#[error("Connection error: {0}")]
ConnectionError(String),
/// Invalid request data
#[error("Invalid request: {0}")]
InvalidRequestData(String),
}

Expand Down Expand Up @@ -258,11 +262,11 @@ impl Stream for OdometryStream {
Poll::Ready(Some(rpc_result)) => match rpc_result {
Ok(odometry_response) => match odometry_response.odometry {
Some(rpc_odometry) => Poll::Ready(Some(Ok(Odometry::from(rpc_odometry)))),
None => Poll::Ready(Some(Err(MavErr(TelemetryError::Unknown(
None => Poll::Ready(Some(Err(RequestError::Mav(TelemetryError::Unknown(
"Unexpected value".into(),
))))),
},
Err(err) => Poll::Ready(Some(Err(RpcErr(err)))),
Err(err) => Poll::Ready(Some(Err(RequestError::Rpc(err)))),
},
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ pub use generated::info;
pub use generated::mocap;
pub use generated::telemetry;

#[derive(Debug)]
pub enum RequestError<PluginMavErr> {
MavErr(PluginMavErr),
RpcErr(tonic::Status),
#[derive(Debug, thiserror::Error)]
pub enum RequestError<PluginMavErr>
where
PluginMavErr: std::error::Error,
{
#[error("MAVLink error: {0}")]
Mav(PluginMavErr),
#[error("RPC error: {0}")]
Rpc(#[from] tonic::Status),
}

pub type RequestResult<SuccessType, PluginMavErr> = Result<SuccessType, RequestError<PluginMavErr>>;
Expand Down

0 comments on commit e7022cd

Please sign in to comment.