SonarSource/sonarqube · error · IllegalArgumentException

Invalid type '%s'. %s can only be set with types: %s

Error message

Invalid type '%s'. %s can only be set with types: %s

What it means

checkType enforces scope-specific whitelists: the overall setting accepts only OVERALL_TYPES and branch settings only BRANCH_TYPES. When the parsed type is valid as an enum but outside the whitelist for the request's scope, it throws with the scope name and the allowed types. This prevents e.g. setting a reference branch as the global New Code Definition.

Source

Thrown at server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java:285

  private SnapshotDto getAnalysis(DbSession dbSession, String analysisUuid, ProjectDto project, BranchDto branch) {
    SnapshotDto snapshotDto = dbClient.snapshotDao().selectByUuid(dbSession, analysisUuid)
      .orElseThrow(() -> new NotFoundException(format("Analysis '%s' is not found", analysisUuid)));
    checkAnalysis(dbSession, project, branch, snapshotDto);
    return snapshotDto;
  }

  private void checkAnalysis(DbSession dbSession, ProjectDto project, BranchDto branch, SnapshotDto analysis) {
    BranchDto analysisBranch = dbClient.branchDao().selectByUuid(dbSession, analysis.getRootComponentUuid()).orElse(null);
    boolean analysisMatchesProjectBranch = analysisBranch != null && analysisBranch.getUuid().equals(branch.getUuid());

    checkArgument(analysisMatchesProjectBranch,
      "Analysis '%s' does not belong to branch '%s' of project '%s'",
      analysis.getUuid(), branch.getKey(), project.getKey());
  }

  private static void checkType(String name, Set<NewCodePeriodType> validTypes, NewCodePeriodType type) {
    if (!validTypes.contains(type)) {
      throw new IllegalArgumentException(String.format("Invalid type '%s'. %s can only be set with types: %s", type, name, validTypes));
    }
  }
}

View on GitHub (pinned to 184c821202)

Solutions

  1. Use only the types listed in the error message for the target scope (e.g. PREVIOUS_VERSION/NUMBER_OF_DAYS for overall).
  2. If you need REFERENCE_BRANCH, set it per-project or per-branch, not as the overall definition.
  3. Read the error's allowed-types list and adjust type accordingly.

Example fix

// before (overall scope)
POST api/new_code_periods/set?type=REFERENCE_BRANCH&value=master
// after
POST api/new_code_periods/set?type=PREVIOUS_VERSION
Defensive patterns

Strategy: validation

Validate before calling

const OVERALL = ['PREVIOUS_VERSION', 'SPECIFIC_ANALYSIS', 'NUMBER_OF_DAYS'];
const scope = params.project ? (params.branch ? 'branch' : 'project') : 'overall';
if (scope === 'overall' && !OVERALL.includes(params.type)) throw new Error('Type not allowed for overall scope: ' + params.type);

Try / catch

try { ... } catch (e) { const m = String(e.message).match(/can only be set with types: (.*)/); if (m) { params.type = m[1].split(', ')[0]; return retry(params); } throw e; }

Prevention

When it happens

Trigger: Calling api/new_code_periods/set without a project (overall scope) with type=REFERENCE_BRANCH, or with a branch parameter using a type not in BRANCH_TYPES.

Common situations: Copy-pasting a project-level set command to set the overall default; scripts configuring REFERENCE_BRANCH globally; misunderstanding which types apply to overall vs branch scope.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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