@@ -1249,8 +1249,10 @@ ${prompt}
12491249 // Check for error messages from SDK (error can be embedded in message payload!)
12501250 const msgAny = msg as any
12511251 if ( msgAny . type === "error" || msgAny . error ) {
1252- const sdkError =
1253- msgAny . error || msgAny . message || "Unknown SDK error"
1252+ // Extract detailed error text from message content if available
1253+ // This is where the actual error description lives (e.g., "API Error: Claude Code is unable to respond...")
1254+ const messageText = msgAny . message ?. content ?. [ 0 ] ?. text
1255+ const sdkError = messageText || msgAny . error || msgAny . message || "Unknown SDK error"
12541256 lastError = new Error ( sdkError )
12551257
12561258 // Detailed SDK error logging in main process
@@ -1271,11 +1273,14 @@ ${prompt}
12711273 console . error ( `[CLAUDE SDK ERROR] ========================================` )
12721274
12731275 // Categorize SDK-level errors
1276+ // Use the raw error code (e.g., "invalid_request") for category matching
1277+ const rawErrorCode = msgAny . error || ""
12741278 let errorCategory = "SDK_ERROR"
1275- let errorContext = "Claude SDK error"
1279+ // Default errorContext to the full error text (which may include detailed message)
1280+ let errorContext = sdkError
12761281
12771282 if (
1278- sdkError === "authentication_failed" ||
1283+ rawErrorCode === "authentication_failed" ||
12791284 sdkError . includes ( "authentication" )
12801285 ) {
12811286 errorCategory = "AUTH_FAILED_SDK"
@@ -1288,23 +1293,31 @@ ${prompt}
12881293 errorCategory = "MCP_INVALID_TOKEN"
12891294 errorContext = "Invalid access token. Update MCP settings"
12901295 } else if (
1291- sdkError === "invalid_api_key" ||
1296+ rawErrorCode === "invalid_api_key" ||
12921297 sdkError . includes ( "api_key" )
12931298 ) {
12941299 errorCategory = "INVALID_API_KEY_SDK"
12951300 errorContext = "Invalid API key in Claude Code CLI"
12961301 } else if (
1297- sdkError === "rate_limit_exceeded" ||
1302+ rawErrorCode === "rate_limit_exceeded" ||
12981303 sdkError . includes ( "rate" )
12991304 ) {
13001305 errorCategory = "RATE_LIMIT_SDK"
13011306 errorContext = "Session limit reached"
13021307 } else if (
1303- sdkError === "overloaded" ||
1308+ rawErrorCode === "overloaded" ||
13041309 sdkError . includes ( "overload" )
13051310 ) {
13061311 errorCategory = "OVERLOADED_SDK"
13071312 errorContext = "Claude is overloaded, try again later"
1313+ } else if (
1314+ rawErrorCode === "invalid_request" ||
1315+ sdkError . includes ( "Usage Policy" ) ||
1316+ sdkError . includes ( "violate" )
1317+ ) {
1318+ // Usage Policy violation - keep the full detailed error text
1319+ errorCategory = "USAGE_POLICY_VIOLATION"
1320+ // errorContext already contains the full message from sdkError
13081321 }
13091322
13101323 // Emit auth-error for authentication failures, regular error otherwise
@@ -1319,7 +1332,7 @@ ${prompt}
13191332 errorText : errorContext ,
13201333 debugInfo : {
13211334 category : errorCategory ,
1322- sdkError : sdkError ,
1335+ rawErrorCode ,
13231336 sessionId : msgAny . session_id ,
13241337 messageId : msgAny . message ?. id ,
13251338 } ,
@@ -1329,8 +1342,8 @@ ${prompt}
13291342 console . log ( `[SD] M:END sub=${ subId } reason=sdk_error cat=${ errorCategory } n=${ chunkCount } ` )
13301343 console . error ( `[SD] SDK Error details:` , {
13311344 errorCategory,
1332- errorContext,
1333- sdkError ,
1345+ errorContext : errorContext . slice ( 0 , 200 ) , // Truncate for log readability
1346+ rawErrorCode ,
13341347 sessionId : msgAny . session_id ,
13351348 messageId : msgAny . message ?. id ,
13361349 fullMessage : JSON . stringify ( msgAny , null , 2 ) ,
0 commit comments