{"record":{"id":"9ecb7f9edf780401","repo":"alibaba/spring-ai-alibaba","slug":"accountnameexists","errorCode":"AccountNameExists","errorMessage":"Account name already exists.","messagePattern":"Account name already exists\\.","errorType":"error_code","errorClass":"BizException","httpStatus":400,"severity":"error","filePath":"spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-core/src/main/java/com/alibaba/cloud/ai/studio/core/base/service/impl/AccountServiceImpl.java","lineNumber":195,"sourceCode":"\n\t\treturn createTokenResponse(accountId);\n\t}\n\n\t/**\n\t * Invalidates access token on logout\n\t * @param accessToken Token to invalidate\n\t */\n\t@Override\n\tpublic void logout(String accessToken) {\n\t\ttokenManager.deleteAccessToken(accessToken);\n\t}\n\n\t@Override\n\tpublic String registerAccount(Account account) {\n\t\t// check if account name exists\n\t\tAccountEntity accountEntity = getAccountByName(account.getUsername());\n\t\tif (accountEntity != null) {\n\t\t\tthrow new BizException(ErrorCode.ACCOUNT_NAME_EXISTS.toError());\n\t\t}\n\n\t\tString accountId = IdGenerator.idStr();\n\n\t\tAccountEntity entity = BeanCopierUtils.copy(account, AccountEntity.class);\n\t\tentity.setAccountId(accountId);\n\t\tentity.setStatus(AccountStatus.NORMAL);\n\t\tentity.setType(AccountType.USER);\n\t\tentity.setPassword(PasswordCryptUtils.encode(account.getPassword()));\n\t\tentity.setEmail(account.getEmail());\n\t\tentity.setMobile(account.getMobile());\n\t\tentity.setGmtCreate(new Date());\n\t\tentity.setGmtModified(new Date());\n\t\tentity.setCreator(accountId);\n\t\tentity.setModifier(accountId);\n\n\t\tthis.save(entity);\n","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-core/src/main/java/com/alibaba/cloud/ai/studio/core/base/service/impl/AccountServiceImpl.java#L177-L213","documentation":"BizException thrown by registerAccount when an account with the same username already exists in the store. The service checks getAccountByName(account.getUsername()) before inserting and aborts to keep usernames unique. It maps to ErrorCode.ACCOUNT_NAME_EXISTS.","triggerScenarios":"Calling registerAccount (directly or via the login flow's auto-registration) with a username that already has an AccountEntity in the account store.","commonSituations":"Re-running an app whose bootstrap auto-registers a default admin account; a user retrying signup with the same name; shared database across environments where the account was already created; no idempotency around startup registration.","solutions":["Check existence first with getAccountByName(username) and skip registration if non-null, or catch BizException with code AccountNameExists and treat it as success","Use a different username for the new account","If the existing account is stale/unwanted, delete it from the account store before re-registering","Wrap bootstrap registration in an idempotent 'ensureAccount' helper instead of calling registerAccount unconditionally"],"exampleFix":"// before\naccountService.registerAccount(newAccount); // throws BizException if name exists\n// after\nif (accountService.getAccountByName(newAccount.getUsername()) == null) {\n    accountService.registerAccount(newAccount);\n} else {\n    log.info(\"Account {} already exists, skipping registration\", newAccount.getUsername());\n}","handlingStrategy":"validation","validationCode":"if (accountService.getAccountByName(username) != null) {\n    throw new IllegalArgumentException(\"Username already taken: \" + username);\n}\naccountService.registerAccount(account);","typeGuard":null,"tryCatchPattern":"try {\n    accountService.registerAccount(account);\n} catch (BizException e) {\n    if (\"AccountNameExists\".equals(e.getCode())) {\n        log.warn(\"Account {} already registered, treating as success\", account.getUsername());\n        return;\n    }\n    throw e;\n}","preventionTips":["Always call getAccountByName before registerAccount","Make bootstrap/seed account registration idempotent","Keep usernames unique at the database level with a unique index"],"tags":["account","duplicate-username","registration","business-exception"],"backgroundTag":"file-already-exists","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}