{"record":{"id":"ddc1cd882e52ce7a","repo":"yikart/AiToEarn","slug":"responsecode-accountcreatefailed","errorCode":"ResponseCode.AccountCreateFailed","errorMessage":"AccountCreateFailed","messagePattern":"AccountCreateFailed","errorType":"error_code","errorClass":"AppException","httpStatus":null,"severity":"error","filePath":"project/aitoearn-backend/apps/aitoearn-server/src/core/channels/accounts/account.service.ts","lineNumber":440,"sourceCode":"      nickname: relayAccountRef,\n      status: AccountStatus.ABNORMAL,\n    }\n  }\n\n  private async saveWritableAccount(\n    identity: AccountIdentity,\n    accountData: Partial<Account>,\n    userId: string,\n  ): Promise<Account> {\n    let account = await this.accountRepository.getByIdentity(identity)\n    let created = false\n    if (!account) {\n      account = await this.accountRepository.createByIdentity(identity, accountData)\n      created = true\n    }\n\n    if (!account) {\n      throw new AppException(ResponseCode.AccountCreateFailed)\n    }\n    if (!created && (account.userId === userId || !account.userId)) {\n      account = await this.accountRepository.updateByIdentity(identity, accountData) ?? account\n    }\n    if (account.userId !== userId) {\n      throw new AppException(ResponseCode.ChannelAccountAlreadyConnectedToAnotherUser)\n    }\n\n    return account\n  }\n\n  private async emitAccountConnected(userId: string, account: Account): Promise<void> {\n    await this.eventStream.emit(\n      EventStream.Channels,\n      EventTopic.ChannelsAccountConnected,\n      { userId, accountId: account.id, platform: account.type },\n      { source: 'account-service' },\n    )","sourceCodeStart":422,"sourceCodeEnd":458,"githubUrl":"https://github.com/yikart/AiToEarn/blob/d3aa8bea5b146a8675607cf0144d891aad3e9683/project/aitoearn-backend/apps/aitoearn-server/src/core/channels/accounts/account.service.ts#L422-L458","documentation":"saveWritableAccount looks up an existing channel account by identity, or creates it via accountRepository.createByIdentity. If the repository returns no account after the create attempt, it throws AppException(ResponseCode.AccountCreateFailed). This indicates the persistence layer failed to create or return the account row.","triggerScenarios":"accountRepository.createByIdentity returns null/undefined — DB write failure, unique constraint race with a concurrent create, transaction rollback, or the repository returning no row despite no thrown error. Reached from createPluginAccount and createRelayAccountRecord.","commonSituations":"Duplicate identity inserted concurrently by two plugin callbacks; DB connectivity/constraint issues; identity fields (uid/platform) colliding with an existing row in a way createByIdentity cannot upsert.","solutions":["Check server logs and the repository/DB for a constraint violation or error during createByIdentity.","Retry the account creation — a concurrent-create race may have since committed a row the lookup can find.","Verify identity fields (platform, uid) are correct and not colliding with an existing row in an unexpected state.","Inspect/fix accountRepository.createByIdentity to upsert or surface the underlying DB error instead of returning null."],"exampleFix":"// before\naccount = await this.accountRepository.createByIdentity(identity, accountData)\n// after\naccount = await this.accountRepository.createByIdentity(identity, accountData) ?? await this.accountRepository.findByIdentity(identity) // tolerate insert-race by re-fetching","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"function isAccountCreateFailed(e: unknown): boolean {\n  return e instanceof AppException && e.code === ResponseCode.AccountCreateFailed\n}","tryCatchPattern":"try {\n  account = await accountService.createPluginAccount(userId, identity, data)\n} catch (e) {\n  if (e instanceof AppException && e.code === ResponseCode.AccountCreateFailed) {\n    // inspect DB/logs for constraint violation, then retry once\n    account = await accountService.createPluginAccount(userId, identity, data)\n  } else throw e\n}","preventionTips":["Make createByIdentity an upsert or re-fetch on insert races.","Monitor DB errors/constraint violations on the account table.","Log the underlying repository error, not just the null result.","Serialize account-creation per identity to avoid races."],"tags":["database","account-creation","channel"],"backgroundTag":"account-create-failed","analyzedSha":"d3aa8bea5b146a8675607cf0144d891aad3e9683","analyzedAt":"2026-08-31T14:19:24.185Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}