mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-20 16:01:55 +00:00
fix(openai): preserve credential errors during model catalog refresh
This commit is contained in:
@@ -2075,10 +2075,24 @@ fn bounded_upstream_body(body: &str) -> String {
|
||||
}
|
||||
|
||||
fn map_catalog_error(error: CodexCredentialCatalogError) -> ProviderAdminError {
|
||||
use crate::credential::CredentialRepositoryError;
|
||||
use CodexCredentialCatalogError as Error;
|
||||
use ProviderAdminErrorKind as Kind;
|
||||
let (kind, message) = match error {
|
||||
Error::InvalidCredentialData => (Kind::Invalid, "查询模型的凭据无效,请检查账号授权"),
|
||||
Error::Credential(source) => {
|
||||
let (kind, message) = match &source {
|
||||
CredentialRepositoryError::InvalidCredentialData(_) => {
|
||||
(Kind::Invalid, "查询模型的凭据无效,请检查账号授权")
|
||||
}
|
||||
CredentialRepositoryError::RevisionConflict(_) => {
|
||||
(Kind::Conflict, "账号已更新,请重新查询模型")
|
||||
}
|
||||
CredentialRepositoryError::Store(_) => (Kind::Unavailable, "账号存储暂不可用"),
|
||||
};
|
||||
return provider_admin_error(kind)
|
||||
.with_public_message(message)
|
||||
.with_source(source);
|
||||
}
|
||||
Error::InvalidEtag => (Kind::Invalid, "模型目录版本无效,请重新查询"),
|
||||
Error::NoEligibleCredential => (Kind::NotFound, "没有可查询模型的账号"),
|
||||
Error::ConcurrentUpdate => (Kind::Conflict, "模型目录正在更新,请稍后重试"),
|
||||
|
||||
@@ -264,8 +264,8 @@ impl fmt::Debug for CodexCredentialCatalogSnapshot {
|
||||
pub enum CodexCredentialCatalogError {
|
||||
#[error("Codex model catalog has no eligible account")]
|
||||
NoEligibleCredential,
|
||||
#[error("Codex model catalog account data is invalid")]
|
||||
InvalidCredentialData,
|
||||
#[error(transparent)]
|
||||
Credential(#[from] CredentialRepositoryError),
|
||||
#[error("Codex model catalog upstream query failed: {detail}")]
|
||||
Upstream { detail: String },
|
||||
#[error("Codex model catalog cache is unavailable")]
|
||||
@@ -293,12 +293,6 @@ struct FetchedAccountModels {
|
||||
etag: Option<String>,
|
||||
}
|
||||
|
||||
impl From<CredentialRepositoryError> for CodexCredentialCatalogError {
|
||||
fn from(_: CredentialRepositoryError) -> Self {
|
||||
Self::InvalidCredentialData
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CodexCredentialCatalogService {
|
||||
repository: CodexCredentialRepository,
|
||||
@@ -559,7 +553,7 @@ impl CodexCredentialCatalogService {
|
||||
.store()
|
||||
.get_account(account_id)
|
||||
.await
|
||||
.map_err(|_| CodexCredentialCatalogError::InvalidCredentialData)?
|
||||
.map_err(CredentialRepositoryError::from)?
|
||||
.filter(|account| account.provider().as_str() == "openai")
|
||||
.ok_or(CodexCredentialCatalogError::NoEligibleCredential)?;
|
||||
let scope = CodexCatalogScope::for_account(&account);
|
||||
@@ -701,16 +695,12 @@ impl CodexCredentialCatalogService {
|
||||
account: &ProviderAccount,
|
||||
) -> Result<FetchedAccountModels, CodexCredentialCatalogError> {
|
||||
tokio::time::timeout(MODEL_CATALOG_TIMEOUT, async {
|
||||
let credential = self
|
||||
.repository
|
||||
.load_runtime_credential(account)
|
||||
.await
|
||||
.map_err(|_| CodexCredentialCatalogError::InvalidCredentialData)?;
|
||||
let credential = self.repository.load_runtime_credential(account).await?;
|
||||
let request_id = format!("catalog_{}", Uuid::now_v7().simple());
|
||||
let authorization = credential
|
||||
.authentication
|
||||
.authorization_header()
|
||||
.map_err(|_| CodexCredentialCatalogError::InvalidCredentialData)?;
|
||||
.map_err(CredentialRepositoryError::from)?;
|
||||
let mut client = client
|
||||
.for_account(account)
|
||||
.map_err(|error| CodexCredentialCatalogError::Upstream {
|
||||
|
||||
@@ -326,7 +326,7 @@ fn cas_revision(
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
#[derive(Debug, Clone, Error)]
|
||||
pub enum CredentialRepositoryError {
|
||||
#[error("Codex credential data is invalid")]
|
||||
InvalidCredentialData(#[source] Option<ErrorSource>),
|
||||
|
||||
@@ -12,7 +12,8 @@ use gateway_core::routing::{
|
||||
};
|
||||
use provider_openai::OFFICIAL_CODEX_BASE_URL;
|
||||
use provider_openai::credential::{
|
||||
CodexCredentialCatalogError, CodexCredentialCatalogService, ImportCodexOAuthCredential,
|
||||
CodexCredentialCatalogError, CodexCredentialCatalogService, CredentialRepositoryError,
|
||||
ImportCodexOAuthCredential,
|
||||
};
|
||||
use provider_openai::transport::profile::{CodexWireProfile, CodexWireProfileState};
|
||||
use wiremock::matchers::{header, method, path, query_param};
|
||||
@@ -328,12 +329,63 @@ async fn client_catalog_cache_does_not_survive_credential_rotation() {
|
||||
.await
|
||||
.expect("rotate");
|
||||
assert!(service.client_model_catalog(&scope).await.is_err());
|
||||
assert!(matches!(
|
||||
service.account_catalog_documents(&account).await,
|
||||
Err(CodexCredentialCatalogError::Credential(
|
||||
CredentialRepositoryError::RevisionConflict(_)
|
||||
))
|
||||
));
|
||||
service.refresh_catalogs().await.unwrap();
|
||||
let second = service.client_model_catalog(&scope).await.expect("new");
|
||||
assert_ne!(first, second);
|
||||
server.verify().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn catalog_refresh_preserves_the_store_cause_for_diagnostics() {
|
||||
use gateway_core::error::{ErrorDetails, ErrorSource};
|
||||
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
store.fail_provider_listing();
|
||||
let error = service(&store).refresh_catalogs().await.unwrap_err();
|
||||
assert!(matches!(
|
||||
error,
|
||||
CodexCredentialCatalogError::Credential(CredentialRepositoryError::Store(_))
|
||||
));
|
||||
assert!(!error.to_string().contains("PRIVATE_DATABASE_CAUSE"));
|
||||
let details = ErrorDetails::capture(Some(&ErrorSource::new(error)), None, false).unwrap();
|
||||
assert!(details.as_str().contains("PRIVATE_DATABASE_CAUSE"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_model_catalog_reports_conflicts_and_store_outages_before_querying_upstream() {
|
||||
use gateway_admin::ports::provider::ProviderAdminErrorKind;
|
||||
use gateway_core::error::{StoreError, StoreErrorKind};
|
||||
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
let account = seed_account(&store, "acct_catalog_errors").await;
|
||||
let server = MockServer::start().await;
|
||||
let bundle = crate::admin::initialized_test_bundle(Arc::clone(&store), server.uri()).await;
|
||||
for (store_kind, admin_kind) in [
|
||||
(StoreErrorKind::Conflict, ProviderAdminErrorKind::Conflict),
|
||||
(
|
||||
StoreErrorKind::Unavailable,
|
||||
ProviderAdminErrorKind::Unavailable,
|
||||
),
|
||||
] {
|
||||
store.on_credential_load(Arc::new(move |_, _, _| {
|
||||
Box::pin(async move { Err(StoreError::new(store_kind)) })
|
||||
}));
|
||||
let error = bundle
|
||||
.admin_provider()
|
||||
.models(account.id(), true)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.kind(), admin_kind);
|
||||
}
|
||||
assert!(server.received_requests().await.unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn failed_client_catalog_never_falls_back_to_an_out_of_scope_account_or_cached_profile() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
|
||||
Reference in New Issue
Block a user