Skip to content

Commit 0b22df2

Browse files
authored
Merge pull request #13 from seun-ja/10-bug-error-handling-would-break-program
fix buggy error handling
2 parents e490add + b2b4f64 commit 0b22df2

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/network/event.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,38 @@ pub async fn event_runner(mut swarm: Swarm<PeerBehavior>) -> Result<(), Box<dyn
7272
..
7373
})
7474
)) => {
75-
tracing::info!(
76-
"Got record {:?} {:?}",
77-
std::str::from_utf8(key.as_ref())?,
78-
std::str::from_utf8(&value)?, // TODO: #10 `BUG` error handling would break program
79-
);
75+
if let Ok(value) = std::str::from_utf8(&value) {
76+
if let Ok(key) = std::str::from_utf8(key.as_ref()) {
77+
tracing::info!(
78+
"Got record {:?} with key {:?}",
79+
value,
80+
key
81+
)
82+
}
83+
};
8084
}
8185
kad::QueryResult::GetRecord(Ok(_)) => {}
8286
kad::QueryResult::GetRecord(Err(err)) => {
8387
tracing::info!("Failed to get record: {err:?}");
8488
}
8589
kad::QueryResult::PutRecord(Ok(kad::PutRecordOk { key })) => {
86-
tracing::info!(
87-
"Successfully put record {:?}",
88-
std::str::from_utf8(key.as_ref())?
89-
);
90+
if let Ok(key) = std::str::from_utf8(key.as_ref()) {
91+
tracing::info!(
92+
"Successfully put record {:?}",
93+
key
94+
)
95+
}
9096
}
9197
kad::QueryResult::PutRecord(Err(err)) => {
9298
tracing::info!("Failed to put record: {err:?}");
9399
}
94100
kad::QueryResult::StartProviding(Ok(kad::AddProviderOk { key })) => {
95-
tracing::info!(
96-
"Successfully put provider record {:?}",
97-
std::str::from_utf8(key.as_ref())?
98-
);
101+
if let Ok(key) = std::str::from_utf8(key.as_ref()) {
102+
tracing::info!(
103+
"Successfully put provider record {:?}",
104+
key
105+
)
106+
}
99107
}
100108
kad::QueryResult::StartProviding(Err(err)) => {
101109
eprintln!("Failed to put provider record: {err:?}");

0 commit comments

Comments
 (0)