Skip to content

Commit ea45e20

Browse files
committed
Handle error and deserialization directly in Zulip MessageApiRequest
1 parent 5595695 commit ea45e20

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
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: 26 additions & 26 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,
@@ -833,11 +842,6 @@ struct ResponseNotRequired {
833842
response_not_required: bool,
834843
}
835844

836-
#[derive(serde::Deserialize, Debug)]
837-
struct SentMessage {
838-
id: u64,
839-
}
840-
841845
#[derive(serde::Serialize, Debug, Copy, Clone)]
842846
struct AddReaction<'a> {
843847
message_id: u64,
@@ -911,14 +915,10 @@ async fn post_waiter(
911915
}
912916
.send(ctx.github.raw())
913917
.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;
918918

919919
for reaction in waiting.emoji {
920920
AddReaction {
921-
message_id,
921+
message_id: posted.message_id,
922922
emoji_name: reaction,
923923
}
924924
.send(&ctx.github.raw())

0 commit comments

Comments
 (0)