{"record":{"id":"36bf991aefad6e4a","repo":"SonarSource/sonarqube","slug":"error-while-generating-token-please-try-again","errorCode":null,"errorMessage":"Error while generating token. Please try again.","messagePattern":"Error while generating token\\. Please try again\\.","errorType":"http","errorClass":"ServerException","httpStatus":500,"severity":"error","filePath":"server/sonar-webserver-webapi/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java","lineNumber":200,"sourceCode":"\n    String projectKey = request.mandatoryParam(PARAM_PROJECT_KEY).trim();\n    ProjectDto project = componentFinder.getProjectByKey(session, projectKey);\n    token.setProjectUuid(project.getUuid());\n    token.setProjectKey(project.getKey());\n  }\n\n  private static TokenType getTokenTypeFromRequest(Request request) {\n    String tokenTypeValue = request.mandatoryParam(PARAM_TYPE).trim();\n    return TokenType.valueOf(tokenTypeValue);\n  }\n\n  private String hashToken(DbSession dbSession, String token) {\n    String tokenHash = tokenGenerator.hash(token);\n    UserTokenDto userToken = dbClient.userTokenDao().selectByTokenHash(dbSession, tokenHash);\n    if (userToken == null) {\n      return tokenHash;\n    }\n    throw new ServerException(HTTP_INTERNAL_ERROR, \"Error while generating token. Please try again.\");\n  }\n\n  private UserTokenDto insertTokenInDb(DbSession dbSession, UserDto user, UserTokenDto userTokenDto) {\n    checkTokenDoesNotAlreadyExists(dbSession, user, userTokenDto.getName());\n    dbClient.userTokenDao().insert(dbSession, userTokenDto, user.getLogin());\n    dbSession.commit();\n    return userTokenDto;\n  }\n\n  private void checkTokenDoesNotAlreadyExists(DbSession dbSession, UserDto user, String name) {\n    UserTokenDto userTokenDto = dbClient.userTokenDao().selectByUserAndName(dbSession, user, name);\n    checkRequest(userTokenDto == null, \"A user token for login '%s' and name '%s' already exists\", user.getLogin(), name);\n  }\n\n  private static GenerateWsResponse buildResponse(UserTokenDto userTokenDto, String token, UserDto user) {\n    GenerateWsResponse.Builder responseBuilder = GenerateWsResponse.newBuilder()\n      .setLogin(user.getLogin())\n      .setName(userTokenDto.getName())","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java#L182-L218","documentation":"After hashing a new token, GenerateAction verifies the SHA hash does not already exist in user_tokens; if it does, an astronomically improbable collision, it aborts with HTTP 500 and 'Error while generating token. Please try again.' The check is an internal safety net against token-hash collisions rather than a user-input problem.","triggerScenarios":"POST api/user_tokens/generate where the freshly generated token's hash matches an existing row — essentially only due to hash collision or a corrupted/duplicated hash store.","commonSituations":"Database restore/copy scenarios that duplicated token hashes; hash truncation or encoding bugs; test fixtures seeded with identical token hashes; extremely large token tables amplifying collision odds.","solutions":["Retry the generation — the generator produces a new random token each call.","If it reproduces, inspect the user_tokens table for duplicate token_hash rows and remove corrupted entries.","Verify the token hash generation/encoding settings and DB integrity; restore consistency from backups if needed.","Report to SonarSource if reproducible — a persistent collision indicates an internal invariant violation, not normal operation."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for (let attempt = 1; attempt <= 3; attempt++) {\n  try { return await generateToken(name, exp); }\n  catch (e) {\n    if (e.status === 500 && /Error while generating token/.test(e.message) && attempt < 3) continue;\n    throw e;\n  }\n}","preventionTips":["Retry once or twice — generation is randomized and transient by design","If persistent, audit the user_tokens table for duplicate token_hash rows","Check DB integrity after restores or migrations","Escalate to SonarSource if reproducible; it indicates an internal invariant failure"],"tags":["sonarqube","rest-api","token","hash-collision"],"backgroundTag":"internal-invariant-violation","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}