use systemprompt_oauth::OauthError;
use systemprompt_traits::BoxedSource;
use super::{OAuthErrorCode, OAuthHttpError};
pub fn server_error(context: &'static str, source: impl Into<BoxedSource>) -> OAuthHttpError {
OAuthHttpError::server_error(context).with_source(source)
}
pub fn rejected(error: OAuthHttpError, source: impl Into<BoxedSource>) -> OAuthHttpError {
error.with_source(source)
}
pub fn classify_validation(
error: OauthError,
as_client_error: fn(String) -> OAuthHttpError,
) -> OAuthHttpError {
match error {
OauthError::Validation(message) | OauthError::InvalidClientMetadata(message) => {
as_client_error(message)
},
other => OAuthHttpError::from(other),
}
}
pub fn reclassify(
error: OauthError,
as_client_error: fn(String) -> OAuthHttpError,
) -> OAuthHttpError {
let http = OAuthHttpError::from(error);
if http.code() == OAuthErrorCode::ServerError {
return http;
}
as_client_error(http.description().to_owned())
}
pub fn client_metadata_error(error: OauthError) -> OAuthHttpError {
classify_validation(error, OAuthHttpError::invalid_client_metadata)
}