{"record":{"id":"2f63924f8719e778","repo":"decolua/9router","slug":"trae-create-session-json-stringify-json","errorCode":null,"errorMessage":"Trae create_session: ${JSON.stringify(json)}","messagePattern":"Trae create_session: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"open-sse/executors/trae.js","lineNumber":130,"sourceCode":"        model_name: modelName,\n        agent_type: \"solo_agent_remote\",\n        model_selection_strategy: strategy,\n        common_params: this.commonParams(psd, mode),\n      },\n      env: \"remote\",\n      auto_create_project: false,\n      origin: \"web\",\n    };\n    const res = await proxyAwareFetch(`${this.base()}/chat_sessions`, {\n      method: \"POST\",\n      headers,\n      body: JSON.stringify(body),\n      signal,\n    }, null);\n    const text = await res.text();\n    if (!res.ok) throw new Error(`[${res.status}] ${text}`);\n    const json = JSON.parse(text);\n    if (json?.code !== 0) throw new Error(`Trae create_session: ${JSON.stringify(json)}`);\n    return { sessionId: json.data.chat_session_id, messageId: json.data.message_id };\n  }\n\n  // GET /events SSE → invoke onEvent(eventType, dataObj) per frame.\n  // Resolves when `done`/`error` arrives, the stream ends, or timeout fires.\n  async streamEvents(headers, sessionId, replyTo, onEvent, signal) {\n    const url = `${this.base()}/chat_sessions/${sessionId}/events?reply_to_message_id=${encodeURIComponent(replyTo)}`;\n    const ctrl = new AbortController();\n    if (signal?.aborted) ctrl.abort();\n    const timer = setTimeout(() => ctrl.abort(new Error(\"trae stream timeout\")), STREAM_TIMEOUT_MS);\n    const onAbort = () => ctrl.abort();\n    if (signal) signal.addEventListener(\"abort\", onAbort, { once: true });\n    try {\n      const res = await proxyAwareFetch(url, { method: \"GET\", headers, signal: ctrl.signal }, null);\n      if (!res.ok || !res.body) throw new Error(`[${res.status}] events stream failed`);\n      const reader = res.body.getReader();\n      const decoder = new TextDecoder();\n      let buf = \"\";","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/decolua/9router/blob/90b52e06ffd666b7929554211474d01588f6b1f8/open-sse/executors/trae.js#L112-L148","documentation":"After a successful HTTP create-session POST, Trae wraps its real status in the JSON body with a code field where 0 means success. createSession() throws this error when the response parses as JSON but code !== 0, serializing the whole body so the developer can see Trae's error code and message.","triggerScenarios":"The create-session endpoint returns 200 with an application-level error body: {code: <nonzero>, msg: ...} — quota exhausted, auth rejected at the application layer, session limit reached, or feature disabled for the account.","commonSituations":"Trae free-tier daily quota consumed; application-level token invalid despite HTTP 200; account restrictions/region enforcement; Trae API version drift introducing new nonzero codes.","solutions":["Inspect the JSON in the message — the msg field names the app-level cause; map the code to quota vs auth and act accordingly","Re-authenticate or refresh the Trae credential if the code indicates auth rejection","Wait for quota reset or switch accounts if the code indicates rate/quota limits (enable account fallback)","Retry; transient app errors can clear on a new session","Update 9router if Trae added new error codes requiring handling"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"function isTraeSuccess(json) {\n  return json != null && typeof json === 'object' && json.code === 0 && json.data != null;\n}","tryCatchPattern":"try {\n  session = await executor.createSession(...);\n} catch (e) {\n  if (String(e.message).includes('Trae create_session:')) {\n    const body = e.message.slice(e.message.indexOf('{'));\n    const { code, msg } = JSON.parse(body);\n    log.warn('trae app error', { code, msg });\n    return failoverAccount(req);\n  }\n  throw e;\n}","preventionTips":["Parse and classify Trae's code/msg values for automated quota-vs-auth handling","Enable account fallback — app-level rejections are per-account","Refresh credentials proactively; app-layer auth failures follow token expiry","Track new Trae error codes across versions"],"tags":["trae","application-error","session","error-code"],"backgroundTag":"upstream-application-error","analyzedSha":"90b52e06ffd666b7929554211474d01588f6b1f8","analyzedAt":"2026-08-30T21:05:45.952Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}