From 463acedb11b9f61bb7e97bbe81147cd1ac8a4f43 Mon Sep 17 00:00:00 2001 From: hobostay <110hqc@gmail.com> Date: Thu, 12 Feb 2026 18:20:24 +0800 Subject: [PATCH] fix: replace panic with proper error handling in get_tokenizer The get_tokenizer() function returns Result, String> but was using panic!() when tokenizer initialization failed, which would crash the program instead of returning an error. Changed: - Updated TOKENIZER static to store Result, String> - Modified get_tokenizer() to return Err() instead of panic!() on failure - This allows proper error propagation instead of causing a crash Co-Authored-By: Claude Sonnet 4.5 Signed-off-by: hobostay <110hqc@gmail.com> --- crates/goose/src/token_counter.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/goose/src/token_counter.rs b/crates/goose/src/token_counter.rs index c689d7324515..64efa48ac091 100644 --- a/crates/goose/src/token_counter.rs +++ b/crates/goose/src/token_counter.rs @@ -8,7 +8,7 @@ use tokio::sync::OnceCell; use crate::conversation::message::Message; -static TOKENIZER: OnceCell> = OnceCell::const_new(); +static TOKENIZER: OnceCell, String>> = OnceCell::const_new(); const MAX_TOKEN_CACHE_SIZE: usize = 10_000; @@ -182,15 +182,15 @@ impl TokenCounter { } async fn get_tokenizer() -> Result, String> { - let tokenizer = TOKENIZER + TOKENIZER .get_or_init(|| async { match tiktoken_rs::o200k_base() { - Ok(bpe) => Arc::new(bpe), - Err(e) => panic!("Failed to initialize o200k_base tokenizer: {}", e), + Ok(bpe) => Ok(Arc::new(bpe)), + Err(e) => Err(format!("Failed to initialize o200k_base tokenizer: {}", e)), } }) - .await; - Ok(tokenizer.clone()) + .await + .clone() } pub async fn create_token_counter() -> Result {