Add additional handling for ping errors

This commit is contained in:
Edward Shen 2021-05-19 20:33:39 -04:00
parent fe28f19f07
commit 8772df2231
Signed by: edward
GPG key ID: 19182661E818369F
2 changed files with 40 additions and 3 deletions

View file

@ -8,6 +8,7 @@ use rustls::sign::{RSASigningKey, SigningKey};
use rustls::Certificate; use rustls::Certificate;
use serde::de::{MapAccess, Visitor}; use serde::de::{MapAccess, Visitor};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_repr::Deserialize_repr;
use sodiumoxide::crypto::box_::PrecomputedKey; use sodiumoxide::crypto::box_::PrecomputedKey;
use url::Url; use url::Url;
@ -64,7 +65,14 @@ impl<'a> From<(&'a str, &CliArgs)> for Request<'a> {
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct Response { #[serde(untagged)]
pub enum Response {
Ok(OkResponse),
Error(ErrorResponse),
}
#[derive(Deserialize, Debug)]
pub struct OkResponse {
pub image_server: Url, pub image_server: Url,
pub latest_build: usize, pub latest_build: usize,
pub url: Url, pub url: Url,
@ -76,6 +84,20 @@ pub struct Response {
pub tls: Option<Tls>, pub tls: Option<Tls>,
} }
#[derive(Deserialize, Debug)]
pub struct ErrorResponse {
pub error: String,
pub status: ErrorCode,
}
#[derive(Deserialize_repr, Debug, Copy, Clone)]
#[repr(u16)]
pub enum ErrorCode {
MalformedJson = 400,
InvalidSecret = 401,
InvalidContentType = 415,
}
pub struct Tls { pub struct Tls {
pub created_at: String, pub created_at: String,
pub priv_key: Arc<Box<dyn SigningKey>>, pub priv_key: Arc<Box<dyn SigningKey>>,
@ -150,7 +172,7 @@ pub async fn update_server_state(secret: &str, cli: &CliArgs, data: &mut Arc<RwL
let resp = client.post(CONTROL_CENTER_PING_URL).json(&req).send().await; let resp = client.post(CONTROL_CENTER_PING_URL).json(&req).send().await;
match resp { match resp {
Ok(resp) => match resp.json::<Response>().await { Ok(resp) => match resp.json::<Response>().await {
Ok(resp) => { Ok(Response::Ok(resp)) => {
debug!("got write guard for server state"); debug!("got write guard for server state");
let mut write_guard = data.0.write(); let mut write_guard = data.0.write();
@ -219,6 +241,12 @@ pub async fn update_server_state(secret: &str, cli: &CliArgs, data: &mut Arc<RwL
debug!("dropping write guard for server state"); debug!("dropping write guard for server state");
} }
Ok(Response::Error(resp)) => {
error!(
"Got an {} error from upstream: {}",
resp.status as u16, resp.error
);
}
Err(e) => warn!("Got malformed response: {}", e), Err(e) => warn!("Got malformed response: {}", e),
}, },
Err(e) => match e { Err(e) => match e {

View file

@ -39,6 +39,8 @@ pub enum ServerInitError {
KeyParseError(String), KeyParseError(String),
#[error("Token key was not provided in initial request")] #[error("Token key was not provided in initial request")]
MissingTokenKey, MissingTokenKey,
#[error("Got error response from control center")]
ErrorResponse,
} }
impl ServerState { impl ServerState {
@ -56,7 +58,7 @@ impl ServerState {
match resp { match resp {
Ok(resp) => match resp.json::<Response>().await { Ok(resp) => match resp.json::<Response>().await {
Ok(mut resp) => { Ok(Response::Ok(mut resp)) => {
let key = resp let key = resp
.token_key .token_key
.ok_or(ServerInitError::MissingTokenKey) .ok_or(ServerInitError::MissingTokenKey)
@ -118,6 +120,13 @@ impl ServerState {
url_overridden: config.override_upstream.is_some(), url_overridden: config.override_upstream.is_some(),
}) })
} }
Ok(Response::Error(resp)) => {
error!(
"Got an {} error from upstream: {}",
resp.status as u16, resp.error
);
Err(ServerInitError::ErrorResponse)
}
Err(e) => { Err(e) => {
error!("Got malformed response: {}. Is MangaDex@Home down?", e); error!("Got malformed response: {}. Is MangaDex@Home down?", e);
Err(ServerInitError::MalformedResponse(e)) Err(ServerInitError::MalformedResponse(e))