Skip to content

Commit

Permalink
Simplify error returns and improve an error message
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Egger <daniel.egger@axiros.com>
  • Loading branch information
therealprof committed Mar 12, 2024
1 parent 84fb9f0 commit c6cee0f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 58 deletions.
12 changes: 6 additions & 6 deletions src/usp_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ impl<'a> Msg<'a> {
/// ```
pub fn is_request(&'a self) -> bool {
if let Some(body) = self.body.as_ref() {
return matches!(&body.msg_body, usp::mod_Body::OneOfmsg_body::request(_));
matches!(&body.msg_body, usp::mod_Body::OneOfmsg_body::request(_))
} else {
false
}

false
}

/// Checks whether the body contains a message of type notify (request)
Expand Down Expand Up @@ -279,10 +279,10 @@ impl<'a> Msg<'a> {
/// ```
pub fn is_response(&'a self) -> bool {
if let Some(body) = self.body.as_ref() {
return matches!(&body.msg_body, usp::mod_Body::OneOfmsg_body::response(_));
matches!(&body.msg_body, usp::mod_Body::OneOfmsg_body::response(_))
} else {
false
}

false
}

/// Checks whether the body contains a message of type response
Expand Down
87 changes: 35 additions & 52 deletions src/usp_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ impl Serialize for Record<'_> {
if let Ok(msg) = msg {
state.serialize_field("payload", &msg)?;
} else {
return Err(serde::ser::Error::custom(
msg.context("parsing of the protobuf USP Message failed")
.unwrap_err(),
));
Err(serde::ser::Error::custom(format!(
"{:?}",
msg.context("Interpreting USP Record payload as USP Msg")
.unwrap_err()
)))?
}
}
session_context(context) => {
Expand All @@ -44,11 +45,9 @@ impl Serialize for Record<'_> {
mqtt_connect(mqtt) => state.serialize_field("mqtt_connect", mqtt)?,
stomp_connect(stomp) => state.serialize_field("stomp_connect", stomp)?,
disconnect(disc) => state.serialize_field("disconnect", disc)?,
_ => {
return Err(serde::ser::Error::custom(
"Unknown/Unsupported record type!",
));
}
_ => Err(serde::ser::Error::custom(
"Unknown/Unsupported record type!",
))?,
}

state.end()
Expand Down Expand Up @@ -269,7 +268,7 @@ impl Serialize for Body<'_> {
request(ref m) => state.serialize_field("Request", m)?,
response(ref m) => state.serialize_field("Response", m)?,
error(ref m) => state.serialize_field("Error", m)?,
None => return Err(serde::ser::Error::custom("USP Msg without body?!?")),
None => Err(serde::ser::Error::custom("USP Msg without body?!?"))?,
}
state.end()
}
Expand All @@ -295,7 +294,7 @@ impl Serialize for Request<'_> {
get_supported_protocol(ref m) => state.serialize_field("GetSupportedProtocol", &m)?,
register(ref m) => state.serialize_field("Register", &m)?,
deregister(ref m) => state.serialize_field("Deregister", &m)?,
None => return Err(serde::ser::Error::custom("USP Request Msg without type?!?")),
None => Err(serde::ser::Error::custom("USP Request Msg without type?!?"))?,
}
state.end()
}
Expand Down Expand Up @@ -323,11 +322,9 @@ impl Serialize for Response<'_> {
}
register_resp(ref m) => state.serialize_field("RegisterResp", &m)?,
deregister_resp(ref m) => state.serialize_field("DeregisterResp", &m)?,
None => {
return Err(serde::ser::Error::custom(
"USP Response Msg without type?!?",
))
}
None => Err(serde::ser::Error::custom(
"USP Response Msg without type?!?",
))?,
}
state.end()
}
Expand Down Expand Up @@ -443,7 +440,7 @@ impl Serialize for Notify<'_> {
obj_deletion(ref m) => state.serialize_field("obj_deletion", &m)?,
oper_complete(ref m) => state.serialize_field("oper_complete", &m)?,
on_board_req(ref m) => state.serialize_field("on_board_req", &m)?,
None => return Err(serde::ser::Error::custom("Unknown USP Notify type")),
None => Err(serde::ser::Error::custom("Unknown USP Notify type"))?,
}

state.end()
Expand Down Expand Up @@ -547,11 +544,9 @@ impl Serialize for mod_Notify::OperationComplete<'_> {
match self.operation_resp {
req_output_args(ref m) => state.serialize_field("req_output_args", m)?,
cmd_failure(ref m) => state.serialize_field("cmd_failure", m)?,
None => {
return Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))
}
None => Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))?,
}
state.end()
}
Expand Down Expand Up @@ -957,11 +952,9 @@ impl Serialize for mod_SetResp::mod_UpdatedObjectResult::OperationStatus<'_> {
match &self.oper_status {
oper_success(ref m) => state.serialize_field("oper_success", m)?,
oper_failure(ref m) => state.serialize_field("oper_failure", m)?,
None => {
return Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))
}
None => Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))?,
}
state.end()
}
Expand Down Expand Up @@ -1053,11 +1046,9 @@ impl Serialize for mod_OperateResp::OperationResult<'_> {
req_obj_path(ref m) => state.serialize_field("req_obj_path", m)?,
req_output_args(ref m) => state.serialize_field("req_output_args", m)?,
cmd_failure(ref m) => state.serialize_field("cmd_failure", m)?,
None => {
return Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))
}
None => Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))?,
}
state.end()
}
Expand Down Expand Up @@ -1145,11 +1136,9 @@ impl Serialize for mod_RegisterResp::mod_RegisteredPathResult::OperationStatus<'
match &self.oper_status {
oper_success(ref m) => state.serialize_field("oper_success", m)?,
oper_failure(ref m) => state.serialize_field("oper_failure", m)?,
None => {
return Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))
}
None => Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))?,
}
state.end()
}
Expand Down Expand Up @@ -1216,11 +1205,9 @@ impl Serialize for mod_DeregisterResp::mod_DeregisteredPathResult::OperationStat
match &self.oper_status {
oper_success(ref m) => state.serialize_field("oper_success", m)?,
oper_failure(ref m) => state.serialize_field("oper_failure", m)?,
None => {
return Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))
}
None => Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))?,
}
state.end()
}
Expand Down Expand Up @@ -1276,11 +1263,9 @@ impl Serialize for mod_DeleteResp::mod_DeletedObjectResult::OperationStatus<'_>
match &self.oper_status {
oper_success(ref m) => state.serialize_field("oper_success", m)?,
oper_failure(ref m) => state.serialize_field("oper_failure", m)?,
None => {
return Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))
}
None => Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))?,
}
state.end()
}
Expand Down Expand Up @@ -1386,11 +1371,9 @@ impl Serialize for mod_AddResp::mod_CreatedObjectResult::OperationStatus<'_> {
match self.oper_status {
oper_success(ref m) => state.serialize_field("oper_success", m)?,
oper_failure(ref m) => state.serialize_field("oper_failure", m)?,
None => {
return Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))
}
None => Err(serde::ser::Error::custom(
"USP Msg OperationStatus is unknown?!?",
))?,
}
state.end()
}
Expand Down

0 comments on commit c6cee0f

Please sign in to comment.