Skip to content

Commit 8717695

Browse files
committed
fix(panics): replace runtime-fallible unwraps with graceful fallbacks
1 parent 36c6545 commit 8717695

3 files changed

Lines changed: 19 additions & 21 deletions

File tree

src/abstraction/command.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl CommandData {
4343
))
4444
.default_headers(headers)
4545
.build()
46-
.unwrap();
46+
.expect("failed to build reqwest client");
4747

4848
let inner = playmatch_client::Client::new_with_client(
4949
&env::var("PLAYMATCH_API_URL")
@@ -67,13 +67,13 @@ impl CommandData {
6767

6868
lazy_static! {
6969
pub static ref STAFF_ROLE_ID: u64 = env::var("DISCORD_RETROREALM_STAFF_ROLE_ID")
70-
.unwrap_or_default()
71-
.parse()
72-
.unwrap();
70+
.ok()
71+
.and_then(|v| v.parse().ok())
72+
.unwrap_or(0);
7373
pub static ref TRUSTED_ROLE_IDS: Vec<u64> = env::var("DISCORD_TRUSTED_ROLE_IDS")
7474
.unwrap_or_default()
75-
.split(",")
76-
.map(|id| id.trim().parse().unwrap())
75+
.split(',')
76+
.filter_map(|id| id.trim().parse().ok())
7777
.collect();
7878
}
7979

@@ -85,11 +85,9 @@ pub async fn is_user_trusted_or_above(ctx: CommandContext<'_>) -> CheckResult {
8585
return Ok(true);
8686
}
8787

88-
if member.is_none() {
88+
let Some(member) = member else {
8989
return Ok(false);
90-
}
91-
92-
let member = member.unwrap();
90+
};
9391

9492
if member.roles.iter().any(|role_id| {
9593
role_id == &RoleId::new(*STAFF_ROLE_ID) || TRUSTED_ROLE_IDS.contains(&role_id.get())

src/command/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ use poise::Command;
1111

1212
lazy_static! {
1313
pub static ref RETROREALM_SERVER_ID: u64 = std::env::var("DISCORD_RETROREALM_SERVER_ID")
14-
.unwrap_or_default()
15-
.parse()
16-
.unwrap();
14+
.ok()
15+
.and_then(|v| v.parse().ok())
16+
.unwrap_or(0);
1717
static ref UPDATE_ROLE_ID: String =
1818
std::env::var("DISCORD_RETROREALM_UPDATE_ROLE_ID").unwrap_or_default();
1919
pub(crate) static ref SUGGESTION_CHANNEL_ID: u64 =
2020
std::env::var("DISCORD_RETROREALM_SUGGESTION_CHANNEL_ID")
21-
.unwrap_or_default()
22-
.parse()
23-
.unwrap();
21+
.ok()
22+
.and_then(|v| v.parse().ok())
23+
.unwrap_or(0);
2424
}
2525

2626
pub fn get_all_commands() -> Vec<Command<CommandData, CommandError>> {

src/command/playmatch.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,25 +230,25 @@ pub async fn get_game_metadata(
230230
MetadataMatchType::Automatic => {
231231
let reason = metadata_mapping
232232
.automatic_match_reason
233-
.expect("Automatic match reason is missing");
233+
.ok_or_else(|| anyhow!("Automatic match is missing reason"))?;
234234
let provider_id = metadata_mapping
235235
.provider_id
236-
.expect("Provider ID is missing for automatic match");
236+
.ok_or_else(|| anyhow!("Automatic match is missing provider id"))?;
237237
format!("\nReason: `{reason}`\nProvider ID: `{provider_id}`")
238238
}
239239
MetadataMatchType::Manual => {
240240
let manual_match_type = metadata_mapping
241241
.manual_match_type
242-
.expect("Manual match type is missing");
242+
.ok_or_else(|| anyhow!("Manual match is missing match type"))?;
243243
let provider_id = metadata_mapping
244244
.provider_id
245-
.expect("Provider ID is missing for manual match");
245+
.ok_or_else(|| anyhow!("Manual match is missing provider id"))?;
246246
format!("\nMatched By: `{manual_match_type}`\nProvider ID: `{provider_id}`")
247247
}
248248
MetadataMatchType::Failed => {
249249
let failed_reason = metadata_mapping
250250
.failed_match_reason
251-
.expect("Failed match reason is missing");
251+
.ok_or_else(|| anyhow!("Failed match is missing reason"))?;
252252
format!("\nReason: `{failed_reason}`")
253253
}
254254
MetadataMatchType::None => String::new(),

0 commit comments

Comments
 (0)