Skip to content

Commit

Permalink
rory: handle errors from api
Browse files Browse the repository at this point in the history
  • Loading branch information
getchoo committed Dec 8, 2023
1 parent 7e96bce commit 026d4cb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/api/dadjoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ pub async fn get_joke() -> Result<String> {
if let StatusCode::OK = status {
Ok(resp.text().await?)
} else {
Err(eyre!("Failed to fetch joke from {DADJOKE} with {status}"))
Err(eyre!("Couldn't get a joke!"))
}
}
10 changes: 7 additions & 3 deletions src/api/prism_meta.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::api::REQWEST_CLIENT;

use color_eyre::eyre::{eyre, Result};
use color_eyre::eyre::{eyre, Context, Result};
use log::*;
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
Expand All @@ -27,11 +27,15 @@ pub async fn get_latest_minecraft_version() -> Result<String> {
let status = resp.status();

if let StatusCode::OK = status {
let data = resp.json::<MinecraftPackageJson>().await?;
let data = resp
.json::<MinecraftPackageJson>()
.await
.wrap_err_with(|| "Couldn't parse Minecraft versions!")?;

let version = data
.recommended
.first()
.ok_or_else(|| eyre!("Couldn't find first recommended version!"))?;
.ok_or_else(|| eyre!("Couldn't find latest version of Minecraft!"))?;

Ok(version.clone())
} else {
Expand Down
17 changes: 13 additions & 4 deletions src/api/rory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::api::REQWEST_CLIENT;

use color_eyre::eyre::{eyre, Result};
use color_eyre::eyre::{eyre, Context, Result};
use log::*;
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
Expand All @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
pub struct RoryResponse {
pub id: u64,
pub url: String,
pub error: Option<String>,
}

const RORY: &str = "https://rory.cat";
Expand All @@ -25,14 +26,22 @@ pub async fn get_rory(id: Option<u64>) -> Result<RoryResponse> {

let req = REQWEST_CLIENT
.get(format!("{RORY}{ENDPOINT}/{target}"))
.build()?;
.build()
.wrap_err_with(|| "Couldn't build reqwest client!")?;

info!("Making request to {}", req.url());
let resp = REQWEST_CLIENT.execute(req).await?;
let resp = REQWEST_CLIENT
.execute(req)
.await
.wrap_err_with(|| "Couldn't make request for shiggy!")?;
let status = resp.status();

if let StatusCode::OK = status {
let data = resp.json::<RoryResponse>().await?;
let data = resp
.json::<RoryResponse>()
.await
.wrap_err_with(|| "Couldn't parse the shiggy response!")?;

Ok(data)
} else {
Err(eyre!(
Expand Down
14 changes: 9 additions & 5 deletions src/commands/general/rory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ pub async fn rory(
ctx: Context<'_>,
#[description = "specify a Rory ID"] id: Option<u64>,
) -> Result<()> {
let resp = get_rory(id).await?;
let rory = get_rory(id).await?;

ctx.send(|m| {
m.embed(|e| {
e.title("Rory :3")
.url(&resp.url)
.image(resp.url)
.footer(|f| f.text(format!("ID {}", resp.id)))
if let Some(error) = rory.error {
e.title("Error!").description(error)
} else {
e.title("Rory :3")
.url(&rory.url)
.image(rory.url)
.footer(|f| f.text(format!("ID {}", rory.id)))
}
})
})
.await?;
Expand Down
1 change: 1 addition & 0 deletions src/commands/general/say.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub async fn say(ctx: Context<'_>, #[description = "Just content?"] content: Str
.await
.ok_or_else(|| eyre!("Couldn't get channel!"))?;

ctx.defer_ephemeral().await?;
channel.say(ctx, &content).await?;
ctx.say("I said what you said!").await?;

Expand Down

0 comments on commit 026d4cb

Please sign in to comment.