{"record":{"id":"b8202d0cf1c7716c","repo":"iflytek/astron-agent","slug":"converttexterrorcodetoresponseenum-listener-geterrorcode","errorCode":null,"errorMessage":"convertTextErrorCodeToResponseEnum(listener.getErrorCode())","messagePattern":"convertTextErrorCodeToResponseEnum\\(listener\\.getErrorCode\\(\\)\\)","errorType":"error_code","errorClass":"BusinessException","httpStatus":null,"severity":"error","filePath":"console/backend/hub/src/main/java/com/iflytek/astron/console/hub/util/BotAIServiceClient.java","lineNumber":219,"sourceCode":"            listener = new TextGenerationWebSocketListener(\n                    config.getPlatformAppId(), question, domain, latch, totalAnswer);\n            httpClient.newWebSocket(request, listener);\n\n            if (!latch.await(seconds, TimeUnit.SECONDS)) {\n                log.error(\"AI text generation request timeout, timeout: {} seconds\", seconds);\n                throw new BusinessException(ResponseEnum.SYSTEM_ERROR);\n            }\n\n            // Check if AI service returned an error\n            if (listener.getErrorCode() != null) {\n                String errorMsg = listener.getErrorMessage() != null\n                        ? listener.getErrorMessage()\n                        : \"AI service error, code: \" + listener.getErrorCode();\n                log.error(\"AI service error: {}\", errorMsg);\n\n                // Convert error code to corresponding ResponseEnum and throw\n                ResponseEnum responseEnum = convertTextErrorCodeToResponseEnum(listener.getErrorCode());\n                throw new BusinessException(responseEnum);\n            }\n\n            String result = totalAnswer.toString().trim();\n            if (result.isEmpty()) {\n                throw new BusinessException(ResponseEnum.SYSTEM_ERROR);\n            }\n\n            return result;\n        } catch (Exception e) {\n            log.error(\"AI text generation service call exception\", e);\n            if (e instanceof BusinessException) {\n                throw e;\n            }\n            throw new BusinessException(ResponseEnum.SYSTEM_ERROR);\n        }\n    }\n\n    /**","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/backend/hub/src/main/java/com/iflytek/astron/console/hub/util/BotAIServiceClient.java#L201-L237","documentation":"After the WebSocket exchange finishes, generateText inspects listener.getErrorCode(); if the Spark AI service reported an error frame, the raw numeric code is converted via convertTextErrorCodeToResponseEnum into a specific ResponseEnum and thrown as a BusinessException. The message shown is the converter call site — the actual thrown enum depends on the mapped Spark error code (401, 10000-11203 etc.), or SYSTEM_ERROR for unmapped codes.","triggerScenarios":"The Spark chat API sends a WebSocket message with header.code != 0 (e.g. 401 auth failure captured in onFailure, 10005 param error, 11202 QPS limit, 10013/10014 content audit failures) before the exchange completes.","commonSituations":"Wrong/expired API key or secret (401/11200), exhausted free tier or daily quota (11201), exceeding QPS/concurrency limits (11202/11203/10006), invalid 'domain' value for the API version (10005/10163), or content-policy rejections (10013/10014/10019/10021).","solutions":["Read the logged 'AI service error: ...' message to get the numeric Spark code and map it to its documented cause","For 401/11200/11203-style codes: fix the platform API key/secret/appId configuration in the platform account settings","For 11201/11202/11203/10006: apply for higher quota or add rate limiting/backoff on the caller side","For 10005/10163: verify the 'domain' argument matches the TEXT_HOST_URL API version (v4.0)"],"exampleFix":"// before\nResponseEnum responseEnum = convertTextErrorCodeToResponseEnum(listener.getErrorCode());\nthrow new BusinessException(responseEnum);\n// after\nResponseEnum responseEnum = convertTextErrorCodeToResponseEnum(listener.getErrorCode());\nlog.error(\"Spark text API error code {} mapped to {}\", listener.getErrorCode(), responseEnum);\nthrow new BusinessException(responseEnum);","handlingStrategy":"try-catch","validationCode":"// validate config before call\nif (StrUtil.isBlank(config.getPlatformApiKey()) || StrUtil.isBlank(config.getPlatformApiSecret()) || StrUtil.isBlank(domain)) { throw new BusinessException(ResponseEnum.CONFIG_ERROR); }","typeGuard":null,"tryCatchPattern":"try { return client.generateText(q, domain, 60); } catch (BusinessException e) { switch (e.getCode()) { case RATE_LIMIT_CODES: sleepAndRetry(); break; case AUTH_CODES: alertConfigIssue(); break; default: throw e; } }","preventionTips":["Map the numeric Spark code from logs to its documented cause","Keep API keys/quotas monitored (401, 11200-11203)","Apply client-side rate limiting to avoid QPS/concurrency codes","Verify domain matches the endpoint API version"],"tags":["websocket","spark-api","error-code-mapping","java"],"backgroundTag":"api-error-response","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}