Skip to content

Commit aaeac64

Browse files
julienldclaude
andauthored
fix: add error handling to search tools for better diagnostics (#227)
Enhanced error handling in ha_search_entities, ha_deep_search, and ha_get_state to include structured error responses with: - success: false flag for explicit error detection - error_type: exception class name for categorization - traceback: full stack trace for debugging - Contextual suggestions for resolution Closes #208 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3dcfefa commit aaeac64

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

src/ha_mcp/tools/tools_search.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,12 @@ async def ha_search_entities(
240240
return await add_timezone_metadata(client, result)
241241

242242
except Exception as e:
243+
import traceback
243244
error_data = {
245+
"success": False,
244246
"error": str(e),
247+
"error_type": type(e).__name__,
248+
"traceback": traceback.format_exc(),
245249
"query": query,
246250
"domain_filter": domain_filter,
247251
"area_filter": area_filter,
@@ -351,15 +355,19 @@ async def ha_deep_search(
351355
result = await smart_tools.deep_search(query, parsed_search_types, limit)
352356
return cast(dict[str, Any], result)
353357
except Exception as e:
358+
import traceback
354359
return {
355360
"success": False,
356361
"error": str(e),
362+
"error_type": type(e).__name__,
363+
"traceback": traceback.format_exc(),
357364
"query": query,
358365
"search_types": parsed_search_types,
366+
"limit": limit,
359367
"suggestions": [
360368
"Check Home Assistant connection",
361-
"Verify automation/script/helper configurations exist",
362-
"Try a simpler search query",
369+
"Try simpler search terms",
370+
"Check search_types are valid: 'automation', 'script', 'helper'",
363371
],
364372
}
365373

@@ -371,9 +379,13 @@ async def ha_get_state(entity_id: str) -> dict[str, Any]:
371379
result = await client.get_entity_state(entity_id)
372380
return await add_timezone_metadata(client, result)
373381
except Exception as e:
382+
import traceback
374383
error_data = {
384+
"success": False,
375385
"entity_id": entity_id,
376386
"error": str(e),
387+
"error_type": type(e).__name__,
388+
"traceback": traceback.format_exc(),
377389
"suggestions": [
378390
f"Verify entity {entity_id} exists",
379391
"Check Home Assistant connection",

0 commit comments

Comments
 (0)