SonarSource/sonarqube · error · ServerException

Error while generating token. Please try again.

Error message

Error while generating token. Please try again.

What it means

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.

Solutions

  1. Retry the generation — the generator produces a new random token each call.
  2. If it reproduces, inspect the user_tokens table for duplicate token_hash rows and remove corrupted entries.
  3. Verify the token hash generation/encoding settings and DB integrity; restore consistency from backups if needed.
  4. Report to SonarSource if reproducible — a persistent collision indicates an internal invariant violation, not normal operation.
Defensive patterns

Strategy: retry

Try / catch

for (let attempt = 1; attempt <= 3; attempt++) {
  try { return await generateToken(name, exp); }
  catch (e) {
    if (e.status === 500 && /Error while generating token/.test(e.message) && attempt < 3) continue;
    throw e;
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/36bf991aefad6e4a. Report an issue: GitHub.

Appendix: source

Thrown at server/sonar-webserver-webapi/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java:200

    String projectKey = request.mandatoryParam(PARAM_PROJECT_KEY).trim();
    ProjectDto project = componentFinder.getProjectByKey(session, projectKey);
    token.setProjectUuid(project.getUuid());
    token.setProjectKey(project.getKey());
  }

  private static TokenType getTokenTypeFromRequest(Request request) {
    String tokenTypeValue = request.mandatoryParam(PARAM_TYPE).trim();
    return TokenType.valueOf(tokenTypeValue);
  }

  private String hashToken(DbSession dbSession, String token) {
    String tokenHash = tokenGenerator.hash(token);
    UserTokenDto userToken = dbClient.userTokenDao().selectByTokenHash(dbSession, tokenHash);
    if (userToken == null) {
      return tokenHash;
    }
    throw new ServerException(HTTP_INTERNAL_ERROR, "Error while generating token. Please try again.");
  }

  private UserTokenDto insertTokenInDb(DbSession dbSession, UserDto user, UserTokenDto userTokenDto) {
    checkTokenDoesNotAlreadyExists(dbSession, user, userTokenDto.getName());
    dbClient.userTokenDao().insert(dbSession, userTokenDto, user.getLogin());
    dbSession.commit();
    return userTokenDto;
  }

  private void checkTokenDoesNotAlreadyExists(DbSession dbSession, UserDto user, String name) {
    UserTokenDto userTokenDto = dbClient.userTokenDao().selectByUserAndName(dbSession, user, name);
    checkRequest(userTokenDto == null, "A user token for login '%s' and name '%s' already exists", user.getLogin(), name);
  }

  private static GenerateWsResponse buildResponse(UserTokenDto userTokenDto, String token, UserDto user) {
    GenerateWsResponse.Builder responseBuilder = GenerateWsResponse.newBuilder()
      .setLogin(user.getLogin())
      .setName(userTokenDto.getName())

View on GitHub (pinned to 184c821202)