{"record":{"id":"daf8ba5f816847d3","repo":"moeru-ai/airi","slug":"failed-to-create-token-json-stringify-response","errorCode":null,"errorMessage":"Failed to create token: ${JSON.stringify(response) || 'Unknown error'}","messagePattern":"Failed to create token: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/stage-ui/src/libs/providers/providers/aliyun-nls/token.ts","lineNumber":134,"sourceCode":"\nexport async function createToken(accessKeyId: string, accessKeySecret: string, options?: CreateTokenOptions): Promise<{ token: string, expiresAt: number }> {\n  const request = await buildCreateTokenRequest(accessKeyId, accessKeySecret, options)\n  const response = await ofetch<{\n    NlsRequestId: string\n    RequestId: string\n    ErrMsg: string\n    Token: { ExpireTime: number, Id: string, UserId: string }\n  } | {\n    RequestId: string\n    Message: string\n    Code: string\n  }>(request.url, { method: 'POST' })\n\n  if ('Token' in response && typeof response.Token === 'object' && 'Id' in response.Token) {\n    return { token: response.Token.Id, expiresAt: response.Token.ExpireTime * 1000 }\n  }\n\n  throw new Error(`Failed to create token: ${JSON.stringify(response) || 'Unknown error'}`)\n}\n","sourceCodeStart":116,"sourceCodeEnd":136,"githubUrl":"https://github.com/moeru-ai/airi/blob/27111382b4a79a7e983289d6e983a06af185ed0f/packages/stage-ui/src/libs/providers/providers/aliyun-nls/token.ts#L116-L136","documentation":"Thrown by createToken() when the Aliyun NLS CreateToken API response does not contain a Token object with an Id. The function posts a signed request to the Aliyun REST endpoint and expects { Token: { Id, ExpireTime, UserId } }; any other shape (typically an error body with Code/Message) is serialized into the message for diagnosis. This is the server rejecting the token request.","triggerScenarios":"ofetch returns a response where 'Token' in response is false, or response.Token is not an object containing Id. The Aliyun API returned an error envelope { RequestId, Message, Code } instead. Causes: invalid accessKeyId/accessKeySecret producing a SignatureDoesNotMatch or InvalidAccessKeyId, no NLS service activated on the account, or quota/permission errors.","commonSituations":"Wrong AccessKey pair (the signature check fails). Using an account where NLS is not activated. STS/temporary credentials lacking permission. Region mismatch. Clock skew affecting the signature timestamp.","solutions":["Read the serialized response in the message — it contains the Aliyun Code (e.g. InvalidAccessKeyId, SignatureDoesNotMatch) and Message.","Verify accessKeyId/accessKeySecret are correct and have NLS permissions; re-paste from the Aliyun console.","Confirm the NLS service is activated for the account/region (default region cn-shanghai).","Ensure system clock is accurate; signature timestamps are sensitive to skew."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// before createToken, ensure credentials are present and well-formed\nif (!accessKeyId || !accessKeySecret) throw new Error('Aliyun credentials required for token creation')\n// the server is authoritative; the response shape determines success vs error","typeGuard":"function isAliyunTokenResponse(r: unknown): r is { Token: { Id: string, ExpireTime: number, UserId: string } } {\n  return !!r && typeof r === 'object'\n    && 'Token' in (r as object)\n    && typeof (r as { Token: unknown }).Token === 'object'\n    && 'Id' in (r as { Token: object }).Token\n}","tryCatchPattern":"try {\n  const { token, expiresAt } = await createToken(accessKeyId, accessKeySecret, options)\n}\ncatch (err) {\n  if (err instanceof Error && err.message.startsWith('Failed to create token')) {\n    // parse the JSON in the message for Aliyun Code; fix credentials/region/permissions\n  }\n  else throw err\n}","preventionTips":["Verify the AccessKey pair has NLS permissions and the service is activated.","Keep system clock accurate (signature timestamp sensitivity).","Use the correct region (default cn-shanghai) for your NLS instance."],"tags":["network","aliyun","nls","credentials","token","api-error"],"backgroundTag":null,"analyzedSha":"27111382b4a79a7e983289d6e983a06af185ed0f","analyzedAt":"2026-08-12T18:33:34.132Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}