Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(search): handle wrapped error messages in user_facing_error

Steven Enamakelc8e233dc58ff
diff --git a/crates/openhuman-core/src/search/tools.rs b/crates/openhuman-core/src/search/tools.rs
index c6c6beb578..03643b4ba2 100644
--- a/crates/openhuman-core/src/search/tools.rs
+++ b/crates/openhuman-core/src/search/tools.rs
@@ -62,41 +62,34 @@ impl TinySearchTool {
/// classified failures with `tinysearch.<code>:`; the detail after the prefix
/// may echo the query, so it is not logged.
pub fn user_facing_error(error: &str) -> String {
- let code = error
- .split("tinysearch.")
- .nth(1)
- .and_then(|rest| rest.split(':').next())
- .unwrap_or_default();
- match code {
- c if c == errors::INSUFFICIENT_BALANCE => {
+ match error_code(error) {
+ Some(code) if code == errors::INSUFFICIENT_BALANCE => {
"Web search is unavailable: the TinyHumans balance is too low for managed search. \
Top up the balance, or add your own provider key under Connections → Search."
.to_string()
}
- c if c == errors::RATE_LIMITED => {
+ Some(code) if code == errors::RATE_LIMITED => {
"Web search is rate limited right now. Wait a moment and try again.".to_string()
}
- c if c == errors::UNAVAILABLE => {
+ Some(code) if code == errors::UNAVAILABLE => {
"Every configured search provider for this request is unavailable right now."
.to_string()
}
- c if c == errors::INVALID_ARGUMENTS => {
- format!(
- "The search request was rejected: {}",
- error.split_once(": ").map(|(_, d)| d).unwrap_or(error)
- )
+ Some(code) if code == errors::INVALID_ARGUMENTS => {
+ let marker = format!("{}{code}: ", errors::PREFIX);
+ let detail = error
+ .split_once(marker.as_str())
+ .map(|(_, detail)| detail)
+ .unwrap_or(error);
+ format!("The search request was rejected: {detail}")
}
_ => format!("Web search failed: {error}"),
}
}
/// The classified error code in a module error message, if any.
-pub fn error_code(error: &str) -> Option<&str> {
- error
- .split("tinysearch.")
- .nth(1)
- .and_then(|rest| rest.split(':').next())
- .filter(|code| !code.is_empty())
+pub fn error_code(error: &str) -> Option<&'static str> {
+ errors::code_of(error)
}
#[async_trait]
diff --git a/crates/openhuman-core/src/search/tools_tests.rs b/crates/openhuman-core/src/search/tools_tests.rs
index b8f1ce00bc..e129e70165 100644
--- a/crates/openhuman-core/src/search/tools_tests.rs
+++ b/crates/openhuman-core/src/search/tools_tests.rs
@@ -21,7 +21,9 @@ fn classified_errors_become_actionable_messages() {
assert!(user_facing_error("tinysearch.rate_limited: slow down").contains("rate limited"));
assert!(user_facing_error("tinysearch.provider_unavailable: all down").contains("unavailable"));
assert_eq!(
- user_facing_error("tinysearch.invalid_arguments: urls must not be empty"),
+ user_facing_error(
+ "search ExecuteTool failed: tinysearch.invalid_arguments: urls must not be empty"
+ ),
"The search request was rejected: urls must not be empty"
);
assert_eq!(user_facing_error("boom"), "Web search failed: boom");
diff --git a/scripts/mock-api/routes/__tests__/search.test.mjs b/scripts/mock-api/routes/__tests__/search.test.mjs
index 60dd0b7d54..02670407f1 100644
--- a/scripts/mock-api/routes/__tests__/search.test.mjs
+++ b/scripts/mock-api/routes/__tests__/search.test.mjs
@@ -72,7 +72,7 @@ test("serves Exa contents, answer and findSimilar", async () => {
assert.equal(similar.status, 200);
});
-test("answers 402 for Exa when the balance behavior is set", async () => {
+test("answers insufficient credits for Exa when the balance behavior is set", async () => {
setMockBehaviors({ exaInsufficientBalance: "1" });
const started = await startMockServer(18593, { retryIfInUse: true });
const baseUrl = `http://127.0.0.1:${started.port}`;
@@ -81,7 +81,8 @@ test("answers 402 for Exa when the balance behavior is set", async () => {
objective: "x",
searchQueries: ["x"],
});
- assert.equal(response.status, 402);
+ assert.equal(response.status, 400);
+ assert.equal(response.body.errorCode, "USER_INSUFFICIENT_CREDITS");
});
test("serves Gemini grounded generate-content with grounding metadata", async () => {
diff --git a/scripts/mock-api/routes/integrations.mjs b/scripts/mock-api/routes/integrations.mjs
index 1d1c544e4c..d61b6085d8 100644
--- a/scripts/mock-api/routes/integrations.mjs
+++ b/scripts/mock-api/routes/integrations.mjs
@@ -625,10 +625,15 @@ export function handleIntegrations(ctx) {
// ── Exa (managed) ──────────────────────────────────────────
// Mirrors backend `/agent-integrations/exa/*`: search takes
// `{objective, searchQueries}`; contents/answer/findSimilar proxy Exa's
- // own REST bodies. `mockBehavior.exaInsufficientBalance = "1"` answers 402.
+ // own REST bodies. `mockBehavior.exaInsufficientBalance = "1"` answers
+ // the backend's insufficient-credits 400.
if (method === "POST" && /^\/agent-integrations\/exa\//.test(url)) {
if (mockBehavior.exaInsufficientBalance === "1") {
- json(res, 402, { success: false, error: "Insufficient balance" });
+ json(res, 400, {
+ success: false,
+ error: "Insufficient balance",
+ errorCode: "USER_INSUFFICIENT_CREDITS",
+ });
return true;
}
if (/^\/agent-integrations\/exa\/search\/?$/.test(url)) {