Show errors on failure (#5643)

Co-authored-by: Douwe Osinga <douwe@squareup.com>
This commit is contained in:
Douwe Osinga
2025-11-10 13:39:23 -05:00
committed by GitHub
parent a971a64007
commit 8cd379ca33
17 changed files with 126 additions and 312 deletions
+4 -17
View File
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
@@ -166,7 +166,6 @@ impl PricingCache {
self.save_to_disk(&cached_data).await?;
// Update memory cache
{
let mut cache = self.memory_cache.write().await;
*cache = Some(cached_data);
@@ -179,15 +178,6 @@ impl PricingCache {
pub async fn initialize(&self) -> Result<()> {
// Try loading from disk first
if let Ok(Some(cached)) = self.load_from_disk().await {
// Log how many models we have cached
let total_models: usize = cached.pricing.values().map(|models| models.len()).sum();
tracing::debug!(
"Loaded {} providers with {} total models from disk cache",
cached.pricing.len(),
total_models
);
// Update memory cache
{
let mut cache = self.memory_cache.write().await;
*cache = Some(cached);
@@ -196,8 +186,6 @@ impl PricingCache {
return Ok(());
}
// If no disk cache, fetch from OpenRouter
tracing::info!("Fetching pricing data from OpenRouter API");
self.refresh().await
}
}
@@ -213,14 +201,13 @@ lazy_static::lazy_static! {
static ref PRICING_CACHE: PricingCache = PricingCache::new();
}
/// Create a properly configured HTTP client for the current runtime
fn create_http_client() -> Client {
fn create_http_client() -> Result<Client> {
Client::builder()
.timeout(Duration::from_secs(30))
.pool_idle_timeout(Duration::from_secs(90))
.pool_max_idle_per_host(10)
.build()
.expect("Failed to create HTTP client")
.map_err(|e| anyhow!(e))
}
/// OpenRouter model pricing information
@@ -254,7 +241,7 @@ pub struct OpenRouterModelsResponse {
/// Internal function to fetch pricing data
async fn fetch_openrouter_pricing_internal() -> Result<HashMap<String, OpenRouterModel>> {
let client = create_http_client();
let client = create_http_client()?;
let response = client
.get("https://openrouter.ai/api/v1/models")
.send()