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),
#[error("upstream api was changed. not continuing")]
APIChange,
#[error("upstream api was changed. this method is deprecated")]
Deprecated,
}
pub type LibraryResult<T> = Result<T, LibraryError>;

View file

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