Skip to content

Commit 14fefb6

Browse files
authored
Merge pull request #2001 from Urgau/zulip-errors
Consistently handle Zulip API errors
2 parents 5ea17fa + 3b8d10d commit 14fefb6

File tree

3 files changed

+63
-40
lines changed

3 files changed

+63
-40
lines changed

src/handlers/major_change.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ pub(super) async fn handle_input(
130130
.await
131131
.context("zulip post failed")?;
132132

133-
let zulip_send_res: crate::zulip::MessageApiResponse = zulip_send_res.json().await?;
134-
135133
let zulip_update_req = crate::zulip::UpdateMessageApiRequest {
136134
message_id: zulip_send_res.message_id,
137135
topic: Some(&new_topic),

src/handlers/notify_zulip.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,15 @@ pub(super) async fn handle_input<'a>(
218218
let msg = msg.replace("{title}", &event.issue.title);
219219
let msg = replace_team_to_be_nominated(&event.issue.labels, msg);
220220

221-
let resp = crate::zulip::MessageApiRequest {
221+
let req = crate::zulip::MessageApiRequest {
222222
recipient,
223223
content: &msg,
224224
}
225225
.send(&ctx.github.raw())
226-
.await?;
226+
.await;
227227

228-
let status = resp.status();
229-
let body = resp.text().await?;
230-
if !status.is_success() {
231-
log::error!("Failed to send notification to Zulip {}", body);
228+
if let Err(err) = req {
229+
log::error!("Failed to send notification to Zulip {}", err);
232230
}
233231
}
234232
}

src/zulip.rs

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -475,18 +475,8 @@ async fn execute_for_other_user(
475475
.send(ctx.github.raw())
476476
.await;
477477

478-
match res {
479-
Ok(resp) => {
480-
if !resp.status().is_success() {
481-
log::error!(
482-
"Failed to notify real user about command: response: {:?}",
483-
resp
484-
);
485-
}
486-
}
487-
Err(err) => {
488-
log::error!("Failed to notify real user about command: {:?}", err);
489-
}
478+
if let Err(err) = res {
479+
log::error!("Failed to notify real user about command: {:?}", err);
490480
}
491481

492482
Ok(Some(output))
@@ -591,7 +581,7 @@ impl<'a> MessageApiRequest<'a> {
591581
self.recipient.url()
592582
}
593583

594-
pub async fn send(&self, client: &reqwest::Client) -> anyhow::Result<reqwest::Response> {
584+
pub async fn send(&self, client: &reqwest::Client) -> anyhow::Result<MessageApiResponse> {
595585
let bot_api_token = env::var("ZULIP_API_TOKEN").expect("ZULIP_API_TOKEN");
596586

597587
#[derive(serde::Serialize)]
@@ -604,7 +594,7 @@ impl<'a> MessageApiRequest<'a> {
604594
content: &'a str,
605595
}
606596

607-
Ok(client
597+
let resp = client
608598
.post(format!("{}/api/v1/messages", *ZULIP_URL))
609599
.basic_auth(&*ZULIP_BOT_EMAIL, Some(&bot_api_token))
610600
.form(&SerializedApi {
@@ -623,11 +613,30 @@ impl<'a> MessageApiRequest<'a> {
623613
content: self.content,
624614
})
625615
.send()
626-
.await?)
616+
.await
617+
.context("fail sending Zulip message")?;
618+
619+
let status = resp.status();
620+
621+
if !status.is_success() {
622+
let body = resp
623+
.text()
624+
.await
625+
.context("fail receiving Zulip API response (when sending a message)")?;
626+
627+
anyhow::bail!(body)
628+
}
629+
630+
let resp: MessageApiResponse = resp
631+
.json()
632+
.await
633+
.context("fail receiving the JSON Zulip Api reponse (when sending a message)")?;
634+
635+
Ok(resp)
627636
}
628637
}
629638

630-
#[derive(serde::Deserialize)]
639+
#[derive(Debug, serde::Deserialize)]
631640
pub struct MessageApiResponse {
632641
#[serde(rename = "id")]
633642
pub message_id: u64,
@@ -642,7 +651,7 @@ pub struct UpdateMessageApiRequest<'a> {
642651
}
643652

644653
impl<'a> UpdateMessageApiRequest<'a> {
645-
pub async fn send(&self, client: &reqwest::Client) -> anyhow::Result<reqwest::Response> {
654+
pub async fn send(&self, client: &reqwest::Client) -> anyhow::Result<()> {
646655
let bot_api_token = env::var("ZULIP_API_TOKEN").expect("ZULIP_API_TOKEN");
647656

648657
#[derive(serde::Serialize)]
@@ -655,7 +664,7 @@ impl<'a> UpdateMessageApiRequest<'a> {
655664
pub content: Option<&'a str>,
656665
}
657666

658-
Ok(client
667+
let resp = client
659668
.patch(&format!(
660669
"{}/api/v1/messages/{}",
661670
*ZULIP_URL, self.message_id
@@ -667,7 +676,21 @@ impl<'a> UpdateMessageApiRequest<'a> {
667676
content: self.content,
668677
})
669678
.send()
670-
.await?)
679+
.await
680+
.context("failed to send Zulip API Update Message")?;
681+
682+
let status = resp.status();
683+
684+
if !status.is_success() {
685+
let body = resp
686+
.text()
687+
.await
688+
.context("fail receiving Zulip API response (when updating the message)")?;
689+
690+
anyhow::bail!(body)
691+
}
692+
693+
Ok(())
671694
}
672695
}
673696

@@ -833,30 +856,38 @@ struct ResponseNotRequired {
833856
response_not_required: bool,
834857
}
835858

836-
#[derive(serde::Deserialize, Debug)]
837-
struct SentMessage {
838-
id: u64,
839-
}
840-
841859
#[derive(serde::Serialize, Debug, Copy, Clone)]
842860
struct AddReaction<'a> {
843861
message_id: u64,
844862
emoji_name: &'a str,
845863
}
846864

847865
impl<'a> AddReaction<'a> {
848-
pub async fn send(self, client: &reqwest::Client) -> anyhow::Result<reqwest::Response> {
866+
pub async fn send(self, client: &reqwest::Client) -> anyhow::Result<()> {
849867
let bot_api_token = env::var("ZULIP_API_TOKEN").expect("ZULIP_API_TOKEN");
850868

851-
Ok(client
869+
let resp = client
852870
.post(&format!(
853871
"{}/api/v1/messages/{}/reactions",
854872
*ZULIP_URL, self.message_id
855873
))
856874
.basic_auth(&*ZULIP_BOT_EMAIL, Some(&bot_api_token))
857875
.form(&self)
858876
.send()
859-
.await?)
877+
.await?;
878+
879+
let status = resp.status();
880+
881+
if !status.is_success() {
882+
let body = resp
883+
.text()
884+
.await
885+
.context("fail receiving Zulip API response (when adding a reaction)")?;
886+
887+
anyhow::bail!(body)
888+
}
889+
890+
Ok(())
860891
}
861892
}
862893

@@ -911,14 +942,10 @@ async fn post_waiter(
911942
}
912943
.send(ctx.github.raw())
913944
.await?;
914-
let body = posted.text().await?;
915-
let message_id = serde_json::from_str::<SentMessage>(&body)
916-
.with_context(|| format!("{:?} did not deserialize as SentMessage", body))?
917-
.id;
918945

919946
for reaction in waiting.emoji {
920947
AddReaction {
921-
message_id,
948+
message_id: posted.message_id,
922949
emoji_name: reaction,
923950
}
924951
.send(&ctx.github.raw())

0 commit comments

Comments
 (0)