Skip to content

Commit

Permalink
On unsupported entity scheme for auto registration, Dont return the e…
Browse files Browse the repository at this point in the history
…rror message, but log it (#2441)

Signed-off-by: Pradeep Kumar K J <[email protected]>
  • Loading branch information
PradeepKiruvale authored Nov 17, 2023
1 parent a9a4480 commit 80399a7
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
10 changes: 10 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 @@ -60,6 +60,7 @@ certificate = { path = "crates/common/certificate" }
clap = { version = "4.4", features = ["cargo", "derive"] }
clock = { path = "crates/common/clock" }
collectd_ext = { path = "crates/extensions/collectd_ext" }
capture-logger = "0.1"
csv = "1.1"
darling = "0.20"
doku = "0.21"
Expand Down
2 changes: 1 addition & 1 deletion crates/core/tedge_api/src/entity_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ pub enum Error {
#[error("The specified entity {0} does not exist in the store")]
UnknownEntity(String),

#[error("The specified topic id {0} does not match the default topic scheme: 'device/<device-id>/service/<service-id>'")]
#[error("Auto registration of the entity with topic id {0} failed as it does not match the default topic scheme: 'device/<device-id>/service/<service-id>'. Try explicit registration instead.")]
NonDefaultTopicScheme(EntityTopicId),

#[error(transparent)]
Expand Down
2 changes: 2 additions & 0 deletions crates/extensions/c8y_mapper_ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ tracing = { workspace = true }
[dev-dependencies]
assert-json-diff = { workspace = true }
assert_matches = { workspace = true }
capture-logger = { workspace = true }
proptest = { workspace = true }
rand = { workspace = true }
tedge_actors = { workspace = true, features = ["test-helpers"] }
tedge_mqtt_ext = { workspace = true, features = ["test-helpers"] }
tedge_test_utils = { workspace = true }
test-case = { workspace = true }

37 changes: 36 additions & 1 deletion crates/extensions/c8y_mapper_ext/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,20 @@ impl CumulocityConverter {
// if device is unregistered register using auto-registration
if self.entity_store.get(&source).is_none() {
let auto_registration_messages =
self.entity_store.auto_register_entity(&source)?;
match self.entity_store.auto_register_entity(&source) {
Ok(auto_registration_messages) => auto_registration_messages,
Err(e) => match e {
Error::NonDefaultTopicScheme(eid) => {
debug!("{}", Error::NonDefaultTopicScheme(eid.clone()));
return Ok(vec![self.new_error_message(
ConversionError::FromEntityStoreError(
entity_store::Error::NonDefaultTopicScheme(eid),
),
)]);
}
e => return Err(e.into()),
},
};
for auto_registration_message in &auto_registration_messages {
registration_messages.append(
&mut self.register_and_convert_entity(auto_registration_message)?,
Expand Down Expand Up @@ -3020,6 +3033,28 @@ pub(crate) mod tests {
}
}

#[tokio::test]
async fn try_auto_registration_on_custom_topic() -> Result<(), anyhow::Error> {
use capture_logger::begin_capture;
use capture_logger::pop_captured;
let tmp_dir = TempTedgeDir::new();
let (mut converter, _http_proxy) = create_c8y_converter(&tmp_dir).await;

let alarm_topic = "te/custom/child2///m/";
let alarm_payload = json!({ "text": "" }).to_string();
let alarm_message = Message::new(&Topic::new_unchecked(alarm_topic), alarm_payload);
begin_capture();
let res = converter.try_convert(&alarm_message).await.unwrap();
let expected_err_msg = Message::new(&Topic::new_unchecked("te/errors"), "Auto registration of the entity with topic id custom/child2// failed as it does not match the default topic scheme: 'device/<device-id>/service/<service-id>'. Try explicit registration instead.");
assert_eq!(res[0], expected_err_msg);
let expected_log = "Auto registration of the entity with topic id custom/child2// failed as it does not match the default topic scheme: 'device/<device-id>/service/<service-id>'. Try explicit registration instead.";
// skip other log messages
pop_captured().unwrap().message();
pop_captured().unwrap().message();
assert_eq!(pop_captured().unwrap().message(), expected_log);
Ok(())
}

pub(crate) async fn create_c8y_converter(
tmp_dir: &TempTedgeDir,
) -> (
Expand Down

1 comment on commit 80399a7

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
376 0 3 376 100 58m18.825s

Please sign in to comment.