Skip to content

Commit 010d82e

Browse files
committed
Improve error message formatting in sync client
1 parent 550fb44 commit 010d82e

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/humanloop/sync/sync_client.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .metadata_handler import MetadataHandler
77
import time
88
from humanloop.error import HumanloopRuntimeError
9+
import json
910

1011
if TYPE_CHECKING:
1112
from humanloop.base_client import BaseHumanloop
@@ -22,6 +23,28 @@
2223
# Default cache size for file content caching
2324
DEFAULT_CACHE_SIZE = 100
2425

26+
def format_api_error(error: Exception) -> str:
27+
"""Format API error messages to be more user-friendly."""
28+
error_msg = str(error)
29+
if "status_code" not in error_msg or "body" not in error_msg:
30+
return error_msg
31+
32+
try:
33+
# Extract the body part and parse as JSON
34+
body_str = error_msg.split("body: ")[1]
35+
# Convert Python dict string to valid JSON by replacing single quotes with double quotes
36+
body_str = body_str.replace("'", '"')
37+
body = json.loads(body_str)
38+
39+
# Get the detail from the body
40+
detail = body.get("detail", {})
41+
42+
# Prefer description, fall back to msg
43+
return detail.get("description") or detail.get("msg") or error_msg
44+
except Exception as e:
45+
logger.debug(f"Failed to parse error message: {str(e)}")
46+
return error_msg
47+
2548
class SyncClient:
2649
"""Client for managing synchronization between local filesystem and Humanloop.
2750
@@ -260,7 +283,8 @@ def _pull_directory(self,
260283

261284
page += 1
262285
except Exception as e:
263-
raise HumanloopRuntimeError(f"Failed to fetch page {page}: {str(e)}")
286+
formatted_error = format_api_error(e)
287+
raise HumanloopRuntimeError(f"Failed to pull files: {formatted_error}")
264288

265289
# Log summary only if we have results
266290
if successful_files or failed_files:

0 commit comments

Comments
 (0)