handle patron errors better
This commit is contained in:
parent
c04e4015fa
commit
52885ec91b
21
src/app.rs
21
src/app.rs
|
@ -35,15 +35,22 @@ async fn patrons() -> Result<Option<patreon::Users>> {
|
||||||
let creds: Credentials = envy::prefixed("PATREON_").from_env().unwrap();
|
let creds: Credentials = envy::prefixed("PATREON_").from_env().unwrap();
|
||||||
let cli = Client::new(creds);
|
let cli = Client::new(creds);
|
||||||
|
|
||||||
if let Ok(camp) = cli.campaign().await {
|
match cli.campaign().await {
|
||||||
let id = camp.data[0].id.clone();
|
Ok(camp) => {
|
||||||
if let Ok(users) = cli.pledges(id).await {
|
let id = camp.data[0].id.clone();
|
||||||
Ok(Some(users))
|
|
||||||
} else {
|
match cli.pledges(id).await {
|
||||||
|
Ok(users) => Ok(Some(users)),
|
||||||
|
Err(why) => {
|
||||||
|
log::error!("error getting pledges: {:?}", why);
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(why) => {
|
||||||
|
log::error!("error getting patreon campaign: {:?}", why);
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ use crate::{
|
||||||
templates::{self, Html, RenderRucte},
|
templates::{self, Html, RenderRucte},
|
||||||
};
|
};
|
||||||
use lazy_static::lazy_static;
|
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 std::{convert::Infallible, fmt, sync::Arc};
|
||||||
use warp::{
|
use warp::{
|
||||||
http::{Response, StatusCode},
|
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();
|
HIT_COUNTER.with_label_values(&["patrons"]).inc();
|
||||||
let state = state.clone();
|
let state = state.clone();
|
||||||
match &state.patrons {
|
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())),
|
None => Response::builder().status(500).html(|o| {
|
||||||
Some(patrons) => Response::builder().html(|o| templates::patrons_html(o, patrons.clone()))
|
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,9 +102,11 @@ impl From<SeriesNotFound> for warp::reject::Rejection {
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref REJECTION_COUNTER: IntCounterVec =
|
static ref REJECTION_COUNTER: IntCounterVec = register_int_counter_vec!(
|
||||||
register_int_counter_vec!(opts!("rejections", "Number of rejections by kind"), &["kind"])
|
opts!("rejections", "Number of rejections by kind"),
|
||||||
.unwrap();
|
&["kind"]
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn rejection(err: Rejection) -> Result<impl Reply, Infallible> {
|
pub async fn rejection(err: Rejection) -> Result<impl Reply, Infallible> {
|
||||||
|
@ -111,7 +118,9 @@ pub async fn rejection(err: Rejection) -> Result<impl Reply, Infallible> {
|
||||||
path = "".into();
|
path = "".into();
|
||||||
code = StatusCode::NOT_FOUND;
|
code = StatusCode::NOT_FOUND;
|
||||||
} else if let Some(SeriesNotFound(series)) = err.find() {
|
} 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);
|
log::error!("invalid series {}", series);
|
||||||
path = format!("/blog/series/{}", series);
|
path = format!("/blog/series/{}", series);
|
||||||
code = StatusCode::NOT_FOUND;
|
code = StatusCode::NOT_FOUND;
|
||||||
|
|
Loading…
Reference in New Issue