feat: add deprecated LibraryError

This commit is contained in:
jane400 2024-08-18 18:03:15 +02:00 committed by jane400
parent a1089b5af7
commit daa2aedf0c
2 changed files with 30 additions and 48 deletions

View file

@ -37,6 +37,8 @@ pub enum LibraryError {
DecodeError(String), DecodeError(String),
#[error("upstream api was changed. not continuing")] #[error("upstream api was changed. not continuing")]
APIChange, APIChange,
#[error("upstream api was changed. this method is deprecated")]
Deprecated,
} }
pub type LibraryResult<T> = Result<T, LibraryError>; pub type LibraryResult<T> = Result<T, LibraryError>;

View file

@ -323,62 +323,42 @@ impl Login {
libpaket::LibraryError::Unauthorized => { libpaket::LibraryError::Unauthorized => {
sender.input(LoginInput::NeedsLogin); sender.input(LoginInput::NeedsLogin);
} }
libpaket::LibraryError::Deprecated => {
sender.output(LoginOutput::Error(res)).unwrap();
sender.input(LoginInput::BreakWorld);
}
} }
} }
} }
} }
} }
fn convert_library_error_to_response_type(err: &LibraryError) -> ResponseType { macro_rules! received {
match err { ($func_name:ident, $args:tt, $calling:ident, $calling_args:tt) => {
LibraryError::NetworkFetch => ResponseType::Retry, async fn $func_name$args -> LoginCommand {
LibraryError::Unauthorized => ResponseType::Okay, let client = OpenIdClient::new();
LibraryError::InvalidArgument(_) => ResponseType::Okay, let mut err = LibraryError::NetworkFetch;
LibraryError::DecodeError(_) => ResponseType::Okay, for _ in 0..6 {
LibraryError::APIChange => ResponseType::Okay, let result: Result<TokenResponse, LibraryError> = client
} .$calling$calling_args
} .await;
async fn received_auth_code(auth_code: String, code_verifier: CodeVerfier) -> LoginCommand { err = match result {
let client = OpenIdClient::new(); Ok(result) => return LoginCommand::Token(Ok(result)),
let mut err = LibraryError::NetworkFetch; Err(err) => err,
for _ in 0..6 { };
let result: Result<TokenResponse, LibraryError> = client
.token_authorization(auth_code.clone(), &code_verifier) if err == LibraryError::NetworkFetch {
.await; continue;
} else {
if let Ok(result) = result { return LoginCommand::Token(Err(err));
return LoginCommand::Token(Ok(result)) }
}
LoginCommand::Token(Err(err))
} }
err = result.unwrap_err(); };
let response_type = convert_library_error_to_response_type(&err);
if response_type == ResponseType::Retry {
continue;
} else {
return LoginCommand::Token(Err(err));
}
}
LoginCommand::Token(Err(err))
} }
async fn use_refresh_token(refresh_token: RefreshToken) -> LoginCommand { received!(received_auth_code, (auth_code: String, code_verifier: CodeVerfier), token_authorization, (auth_code.clone(), &code_verifier));
let client = OpenIdClient::new(); received!(use_refresh_token, (refresh_token: RefreshToken), token_refresh, (&refresh_token));
let mut err = LibraryError::NetworkFetch;
for _ in 0..6 {
let result: Result<TokenResponse, LibraryError> =
client.token_refresh(&refresh_token).await;
err = match result {
Ok(result) => return LoginCommand::Token(Ok(result)),
Err(err) => err,
};
let response_type = convert_library_error_to_response_type(&err);
if response_type == ResponseType::Retry {
continue;
} else {
return LoginCommand::Token(Err(err));
}
}
LoginCommand::Token(Err(err))
}