handle patron errors better
This commit is contained in:
parent
c04e4015fa
commit
52885ec91b
17
src/app.rs
17
src/app.rs
|
@ -35,16 +35,23 @@ async fn patrons() -> Result<Option<patreon::Users>> {
|
|||
let creds: Credentials = envy::prefixed("PATREON_").from_env().unwrap();
|
||||
let cli = Client::new(creds);
|
||||
|
||||
if let Ok(camp) = cli.campaign().await {
|
||||
match cli.campaign().await {
|
||||
Ok(camp) => {
|
||||
let id = camp.data[0].id.clone();
|
||||
if let Ok(users) = cli.pledges(id).await {
|
||||
Ok(Some(users))
|
||||
} else {
|
||||
|
||||
match cli.pledges(id).await {
|
||||
Ok(users) => Ok(Some(users)),
|
||||
Err(why) => {
|
||||
log::error!("error getting pledges: {:?}", why);
|
||||
Ok(None)
|
||||
}
|
||||
} else {
|
||||
}
|
||||
}
|
||||
Err(why) => {
|
||||
log::error!("error getting patreon campaign: {:?}", why);
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const ICON: &'static str = "https://christine.website/static/img/avatar.png";
|
||||
|
|
|
@ -3,7 +3,7 @@ use crate::{
|
|||
templates::{self, Html, RenderRucte},
|
||||
};
|
||||
use lazy_static::lazy_static;
|
||||
use prometheus::{IntCounterVec, register_int_counter_vec, opts};
|
||||
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
|
||||
use std::{convert::Infallible, fmt, sync::Arc};
|
||||
use warp::{
|
||||
http::{Response, StatusCode},
|
||||
|
@ -41,8 +41,13 @@ pub async fn patrons(state: Arc<State>) -> Result<impl Reply, Rejection> {
|
|||
HIT_COUNTER.with_label_values(&["patrons"]).inc();
|
||||
let state = state.clone();
|
||||
match &state.patrons {
|
||||
None => Response::builder().html(|o| templates::error_html(o, "Could not load patrons, let me know the API token expired again".to_string())),
|
||||
Some(patrons) => Response::builder().html(|o| templates::patrons_html(o, patrons.clone()))
|
||||
None => Response::builder().status(500).html(|o| {
|
||||
templates::error_html(
|
||||
o,
|
||||
"Could not load patrons, let me know the API token expired again".to_string(),
|
||||
)
|
||||
}),
|
||||
Some(patrons) => Response::builder().html(|o| templates::patrons_html(o, patrons.clone())),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,8 +102,10 @@ impl From<SeriesNotFound> for warp::reject::Rejection {
|
|||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref REJECTION_COUNTER: IntCounterVec =
|
||||
register_int_counter_vec!(opts!("rejections", "Number of rejections by kind"), &["kind"])
|
||||
static ref REJECTION_COUNTER: IntCounterVec = register_int_counter_vec!(
|
||||
opts!("rejections", "Number of rejections by kind"),
|
||||
&["kind"]
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
|
@ -111,7 +118,9 @@ pub async fn rejection(err: Rejection) -> Result<impl Reply, Infallible> {
|
|||
path = "".into();
|
||||
code = StatusCode::NOT_FOUND;
|
||||
} else if let Some(SeriesNotFound(series)) = err.find() {
|
||||
REJECTION_COUNTER.with_label_values(&["SeriesNotFound"]).inc();
|
||||
REJECTION_COUNTER
|
||||
.with_label_values(&["SeriesNotFound"])
|
||||
.inc();
|
||||
log::error!("invalid series {}", series);
|
||||
path = format!("/blog/series/{}", series);
|
||||
code = StatusCode::NOT_FOUND;
|
||||
|
|
Loading…
Reference in New Issue