Skip to content

Commit c5fa106

Browse files
committed
Change inform! to user_error! and document them
1 parent e214442 commit c5fa106

File tree

9 files changed

+27
-21
lines changed

9 files changed

+27
-21
lines changed

src/handlers.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use std::fmt;
1010
use std::sync::Arc;
1111
use tracing as log;
1212

13-
macro_rules! inform {
13+
/// Creates a `anyhow::Result` of [`UserError`] with a error expression.
14+
macro_rules! user_error {
1415
($err:expr $(,)?) => {
15-
anyhow::bail!(crate::handlers::UserError($err.into()))
16+
anyhow::Result::Err(anyhow::anyhow!(crate::handlers::UserError($err.into())))
1617
};
1718
}
1819

@@ -439,6 +440,9 @@ impl fmt::Display for HandlerError {
439440
}
440441
}
441442

443+
/// Represent a user error.
444+
///
445+
/// The message will be shown to the user via comment posted by this bot.
442446
#[derive(Debug)]
443447
pub struct UserError(String);
444448

src/handlers/assign.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ pub(super) async fn handle_command(
525525
let issue = event.issue().unwrap();
526526
if issue.is_pr() {
527527
if !issue.is_open() {
528-
inform!("Assignment is not allowed on a closed PR.");
528+
return user_error!("Assignment is not allowed on a closed PR.");
529529
}
530530
if matches!(
531531
event,
@@ -609,7 +609,7 @@ pub(super) async fn handle_command(
609609
AssignCommand::Claim => event.user().login.clone(),
610610
AssignCommand::AssignUser { username } => {
611611
if !is_team_member && username != event.user().login {
612-
inform!("Only Rust team members can assign other users");
612+
return user_error!("Only Rust team members can assign other users");
613613
}
614614
username.clone()
615615
}
@@ -624,7 +624,7 @@ pub(super) async fn handle_command(
624624
e.apply(&ctx.github, String::new()).await?;
625625
return Ok(());
626626
} else {
627-
inform!("Cannot release another user's assignment");
627+
return user_error!("Cannot release another user's assignment");
628628
}
629629
} else {
630630
let current = &event.user().login;
@@ -636,11 +636,13 @@ pub(super) async fn handle_command(
636636
e.apply(&ctx.github, String::new()).await?;
637637
return Ok(());
638638
} else {
639-
inform!("Cannot release unassigned issue");
639+
return user_error!("Cannot release unassigned issue");
640640
}
641641
};
642642
}
643-
AssignCommand::RequestReview { .. } => inform!("r? is only allowed on PRs."),
643+
AssignCommand::RequestReview { .. } => {
644+
return user_error!("r? is only allowed on PRs.");
645+
}
644646
};
645647
// Don't re-assign if aleady assigned, e.g. on comment edit
646648
if issue.contain_assignee(&to_assign) {

src/handlers/close.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(super) async fn handle_command(
1616
.await
1717
.unwrap_or(false);
1818
if !is_team_member {
19-
inform!("Only team members can close issues.");
19+
return user_error!("Only team members can close issues.");
2020
}
2121
issue.close(&ctx.github).await?;
2222
Ok(())

src/handlers/concern.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub(super) async fn handle_command(
3737
cmd: ConcernCommand,
3838
) -> anyhow::Result<()> {
3939
let Event::IssueComment(issue_comment) = event else {
40-
inform!("Concerns can only be issued on an issue");
40+
return user_error!("Concerns can only be issued on an issue");
4141
};
4242
let Some(comment_url) = event.html_url() else {
4343
bail!("unable to retrieve the comment url")
@@ -81,7 +81,7 @@ pub(super) async fn handle_command(
8181
issue.number,
8282
issue_comment.comment.user,
8383
);
84-
inform!(
84+
return user_error!(
8585
"Only team members in the [team repo](https://github.com/rust-lang/team) can add or resolve concerns."
8686
);
8787
}

src/handlers/major_change.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub(super) async fn handle_input(
112112
.iter()
113113
.any(|l| l.name == config.enabling_label)
114114
{
115-
inform!(format!(
115+
return user_error!(format!(
116116
"This issue is not ready for proposals; it lacks the `{}` label.",
117117
config.enabling_label
118118
));
@@ -247,7 +247,7 @@ pub(super) async fn handle_command(
247247
.iter()
248248
.any(|l| l.name == config.enabling_label)
249249
{
250-
inform!(format!(
250+
return user_error!(format!(
251251
"This issue cannot be seconded; it lacks the `{}` label.",
252252
config.enabling_label
253253
));
@@ -261,7 +261,7 @@ pub(super) async fn handle_command(
261261
.unwrap_or(false);
262262

263263
if !is_team_member {
264-
inform!("Only team members can second issues.");
264+
return user_error!("Only team members can second issues.");
265265
}
266266

267267
let has_concerns = if let Some(concerns_label) = &config.concerns_label {

src/handlers/nominate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub(super) async fn handle_command(
2121
};
2222

2323
if !is_team_member {
24-
inform!(
24+
return user_error!(
2525
"Nominating and approving issues and pull requests is restricted to members of the Rust teams."
2626
);
2727
}

src/handlers/ping.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ pub(super) async fn handle_command(
2424
};
2525

2626
if !is_team_member {
27-
inform!("Only Rust team members can ping teams.");
27+
return user_error!("Only Rust team members can ping teams.");
2828
}
2929

3030
let (gh_team, config) = match config.get_by_name(&team_name.team) {
3131
Some(v) => v,
3232
None => {
33-
inform!(format!(
33+
return user_error!(format!(
3434
"This team (`{}`) cannot be pinged via this command; \
3535
it may need to be added to `triagebot.toml` on the default branch.",
3636
team_name.team,
@@ -41,7 +41,7 @@ pub(super) async fn handle_command(
4141
let team = match team {
4242
Some(team) => team,
4343
None => {
44-
inform!(format!(
44+
return user_error!(format!(
4545
"This team (`{}`) does not exist in the team repository.",
4646
team_name.team,
4747
));
@@ -60,7 +60,7 @@ pub(super) async fn handle_command(
6060
)
6161
.await
6262
{
63-
inform!(format!("Error adding team label (`{}`): {:?}.", label, err));
63+
return user_error!(format!("Error adding team label (`{}`): {:?}.", label, err));
6464
}
6565
}
6666

src/handlers/relabel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub(super) async fn handle_command(
2727
input: RelabelCommand,
2828
) -> anyhow::Result<()> {
2929
let Some(issue) = event.issue() else {
30-
inform!("Can only add and remove labels on an issue");
30+
return user_error!("Can only add and remove labels on an issue");
3131
};
3232

3333
// Check label authorization for the current user
@@ -48,7 +48,7 @@ pub(super) async fn handle_command(
4848
};
4949
if let Some(err) = err {
5050
// bail-out and inform the user why
51-
inform!(err);
51+
return user_error!(err);
5252
}
5353
}
5454

src/handlers/shortcut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(super) async fn handle_command(
3030
let issue = event.issue().unwrap();
3131
// NOTE: if shortcuts available to issues are created, they need to be allowed here
3232
if !issue.is_pr() {
33-
inform!(format!(
33+
return user_error!(format!(
3434
"The \"{:?}\" shortcut only works on pull requests.",
3535
input
3636
));

0 commit comments

Comments
 (0)