refactor error handling to catch more things
This commit is contained in:
parent
310886f2ca
commit
2506aa1be3
|
@ -23,12 +23,6 @@ dependencies = [
|
|||
"alloc-no-stdlib",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.70"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4"
|
||||
|
||||
[[package]]
|
||||
name = "askama"
|
||||
version = "0.12.0"
|
||||
|
@ -284,7 +278,6 @@ checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
|
|||
name = "emojos-dot-in"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"askama",
|
||||
"reqwest",
|
||||
"rocket",
|
||||
|
|
|
@ -10,7 +10,6 @@ publish = false
|
|||
include = ["/build.rs", "/src", "/static", "/templates", "/LICENSE", ".gitignore"]
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
askama = { version = "0.12", default-features = false }
|
||||
reqwest = { version = "0.11", features = ["gzip", "brotli", "deflate", "json"] }
|
||||
rocket = { version = "0.5.0-rc.3", default-features = false }
|
||||
|
|
185
src/main.rs
185
src/main.rs
|
@ -7,7 +7,7 @@ mod trivial;
|
|||
|
||||
use askama::Template;
|
||||
use reqwest::{Client, StatusCode, Url};
|
||||
use rocket::http::{ContentType, Header, Status};
|
||||
use rocket::http::{Header, Status};
|
||||
use rocket::response::{self, Debug, Responder};
|
||||
use rocket::{get, routes, Request, Response, State};
|
||||
use serde::Deserialize;
|
||||
|
@ -39,7 +39,6 @@ fn rocket() -> _ {
|
|||
)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Html<T: Template>(T);
|
||||
|
||||
impl<T: Template> Responder<'_, 'static> for Html<T> {
|
||||
|
@ -52,74 +51,134 @@ impl<T: Template> Responder<'_, 'static> for Html<T> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Emojo {
|
||||
shortcode: String,
|
||||
url: String,
|
||||
static_url: String,
|
||||
visible_in_picker: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "emojo.html")]
|
||||
struct Output {
|
||||
instance: String,
|
||||
show_animated: bool,
|
||||
emojo: Vec<Emojo>,
|
||||
}
|
||||
|
||||
#[get("/<instance>?<show_all>&<show_animated>")]
|
||||
async fn instance(
|
||||
client: &State<Client>,
|
||||
instance: &str,
|
||||
instance: String,
|
||||
show_all: Option<bool>,
|
||||
show_animated: Option<bool>,
|
||||
) -> Result<(ContentType, String), Debug<anyhow::Error>> {
|
||||
#[derive(Deserialize)]
|
||||
struct Emojo {
|
||||
shortcode: String,
|
||||
url: String,
|
||||
static_url: String,
|
||||
visible_in_picker: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "emojo.html")]
|
||||
struct Output<'a> {
|
||||
instance: &'a str,
|
||||
show_animated: bool,
|
||||
emojo: Vec<Emojo>,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "oh_no.html")]
|
||||
struct OhNo<'a> {
|
||||
instance: &'a str,
|
||||
status: StatusCode,
|
||||
why: &'a str,
|
||||
}
|
||||
|
||||
) -> Result<Html<Output>, InstanceError> {
|
||||
let show_all = show_all.unwrap_or_default();
|
||||
let show_animated = show_animated.unwrap_or_default();
|
||||
|
||||
let output = async {
|
||||
let mut url = Url::from_str("https://host.invalid/api/v1/custom_emojis").unwrap();
|
||||
url.set_host(Some(instance))?;
|
||||
|
||||
let response = client.get(url).send().await?;
|
||||
if response.status().is_client_error() || response.status().is_server_error() {
|
||||
return Ok(OhNo {
|
||||
instance,
|
||||
status: response.status(),
|
||||
why: match response.status() {
|
||||
StatusCode::FORBIDDEN => "This instance's emoji list is private.",
|
||||
StatusCode::NOT_FOUND => {
|
||||
"This instance doesn't support the Mastodon custom emoji API."
|
||||
}
|
||||
_ => "That's all we know.",
|
||||
},
|
||||
}
|
||||
.render()?);
|
||||
}
|
||||
|
||||
let mut emojo: Vec<Emojo> = response.json().await?;
|
||||
if !show_all {
|
||||
emojo.retain(|x| x.visible_in_picker.unwrap_or(true));
|
||||
}
|
||||
|
||||
anyhow::Ok(
|
||||
Output {
|
||||
instance,
|
||||
show_animated,
|
||||
emojo,
|
||||
}
|
||||
.render()?,
|
||||
)
|
||||
let mut url = Url::from_str("https://host.invalid/api/v1/custom_emojis").unwrap();
|
||||
if url.set_host(Some(&instance)).is_err() {
|
||||
return Err(InstanceError::from_kind(Kind::NotFound, instance));
|
||||
}
|
||||
|
||||
let mut emojo = match client
|
||||
.get(url)
|
||||
.send()
|
||||
.await
|
||||
.and_then(reqwest::Response::error_for_status)
|
||||
{
|
||||
Ok(response) => match response.json::<Vec<Emojo>>().await {
|
||||
Ok(emojo) => emojo,
|
||||
Err(err) => return Err(InstanceError::new(err, instance)),
|
||||
},
|
||||
Err(err) => return Err(InstanceError::new(err, instance)),
|
||||
};
|
||||
if !show_all {
|
||||
emojo.retain(|x| x.visible_in_picker.unwrap_or(true));
|
||||
}
|
||||
|
||||
Ok(Html(Output {
|
||||
instance,
|
||||
show_animated,
|
||||
emojo,
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "oh_no.html")]
|
||||
struct ErrorDisplay {
|
||||
status: Status,
|
||||
instance: String,
|
||||
kind: Kind,
|
||||
}
|
||||
|
||||
#[derive(Responder)]
|
||||
enum InstanceError {
|
||||
Display((Status, Html<ErrorDisplay>)),
|
||||
Debug(Debug<reqwest::Error>),
|
||||
}
|
||||
|
||||
impl InstanceError {
|
||||
fn new(err: reqwest::Error, instance: String) -> InstanceError {
|
||||
let kind = match err.status() {
|
||||
Some(StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN) => Kind::Private,
|
||||
Some(StatusCode::NOT_FOUND | StatusCode::METHOD_NOT_ALLOWED | StatusCode::GONE) => {
|
||||
Kind::NotFound
|
||||
}
|
||||
Some(_) => return InstanceError::Debug(Debug(err)),
|
||||
None => {
|
||||
if err.is_connect() {
|
||||
Kind::NotFound
|
||||
} else if err.is_decode() || err.is_redirect() {
|
||||
Kind::Malformed
|
||||
} else if err.is_timeout() {
|
||||
Kind::TimedOut
|
||||
} else {
|
||||
return InstanceError::Debug(Debug(err));
|
||||
}
|
||||
}
|
||||
};
|
||||
InstanceError::from_kind(kind, instance)
|
||||
}
|
||||
|
||||
fn from_kind(kind: Kind, instance: String) -> InstanceError {
|
||||
InstanceError::from(ErrorDisplay {
|
||||
status: match kind {
|
||||
Kind::Malformed => Status::BadGateway,
|
||||
Kind::NotFound => Status::NotFound,
|
||||
Kind::Private => Status::Forbidden,
|
||||
Kind::TimedOut => Status::GatewayTimeout,
|
||||
},
|
||||
instance,
|
||||
kind,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ErrorDisplay> for InstanceError {
|
||||
fn from(display: ErrorDisplay) -> InstanceError {
|
||||
InstanceError::Display((display.status, Html(display)))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum Kind {
|
||||
Malformed,
|
||||
NotFound,
|
||||
Private,
|
||||
TimedOut,
|
||||
}
|
||||
|
||||
impl Kind {
|
||||
fn message(self) -> &'static str {
|
||||
match self {
|
||||
Kind::Malformed => "Response from the instance was malformed",
|
||||
Kind::NotFound => {
|
||||
"Not a fediverse instance, or does not support the Mastodon custom emoji API"
|
||||
}
|
||||
Kind::Private => "Instance emoji list is private",
|
||||
Kind::TimedOut => "Timed out waiting for response",
|
||||
}
|
||||
}
|
||||
.await?;
|
||||
Ok((ContentType::HTML, output))
|
||||
}
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
{% extends "base.html" %}
|
||||
{% block title %}Custom emoji list for {{ instance }}{% endblock title %}
|
||||
{% block title %}Error{% endblock title %}
|
||||
{% block body %}
|
||||
<p>
|
||||
<b>{{ instance }}</b>'s custom emoji API (/api/v1/custom_emojis) returned {{ status }}.
|
||||
{{ why }} :(
|
||||
<b>Error</b>: {{ instance }}: {{ kind.message() }}. :(
|
||||
</p>
|
||||
{% endblock body %}
|
||||
|
|
Loading…
Reference in New Issue