rory: handle errors from api
This commit is contained in:
parent
7e96bced41
commit
026d4cb607
5 changed files with 31 additions and 13 deletions
|
@ -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!"))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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};
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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};
|
||||
|
@ -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";
|
||||
|
@ -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!(
|
||||
|
|
|
@ -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?;
|
||||
|
|
|
@ -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?;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue