SonarSource/sonarqube · error · IllegalArgumentException
Invalid type: %s
Error message
Invalid type: %s
What it means
validateType converts the submitted type string to a NewCodePeriodType enum (upper-cased first) and rethrows any IllegalArgumentException as 'Invalid type: <value>'. Only the enum's constant names are accepted, so free-form or lowercase-only variants that do not match a constant fail.
Source
Thrown at server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java:254
}
private ProjectDto getProject(DbSession dbSession, String projectKey) {
return componentFinder.getProjectByKey(dbSession, projectKey);
}
private BranchDto getMainBranch(DbSession dbSession, ProjectDto project) {
return dbClient.branchDao().selectByProject(dbSession, project)
.stream().filter(BranchDto::isMain)
.findFirst()
.orElseThrow(() -> new NotFoundException(format("Main branch in project '%s' is not found", project.getKey())));
}
private static NewCodePeriodType validateType(String typeStr, boolean isOverall, boolean isBranch) {
NewCodePeriodType type;
try {
type = NewCodePeriodType.valueOf(typeStr.toUpperCase(Locale.US));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid type: " + typeStr);
}
if (isOverall) {
checkType("Overall setting", OVERALL_TYPES, type);
} else if (isBranch) {
checkType("Branches", BRANCH_TYPES, type);
} else {
checkType("Projects", PROJECT_TYPES, type);
}
return type;
}
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;
}View on GitHub (pinned to 184c821202)
Solutions
- Use one of the exact NewCodePeriodType names: PREVIOUS_VERSION, SPECIFIC_ANALYSIS, REFERENCE_BRANCH, or NUMBER_OF_DAYS.
- Trim whitespace and avoid quoting errors in the type parameter.
- Note that case does not matter (it is upper-cased), but spelling must match the enum constant exactly.
Example fix
// before type=days // after type=NUMBER_OF_DAYS
Defensive patterns
Strategy: validation
Validate before calling
const VALID = ['PREVIOUS_VERSION', 'SPECIFIC_ANALYSIS', 'REFERENCE_BRANCH', 'NUMBER_OF_DAYS'];
if (!VALID.includes(String(params.type).trim().toUpperCase())) throw new Error('Invalid New Code Period type: ' + params.type); Type guard
const isNCType = (t) => typeof t === 'string' && ['PREVIOUS_VERSION','SPECIFIC_ANALYSIS','REFERENCE_BRANCH','NUMBER_OF_DAYS'].includes(t.trim().toUpperCase());
Try / catch
try { ... } catch (e) { if (/^Invalid type: /.test(String(e.message))) { log('Use one of: PREVIOUS_VERSION, SPECIFIC_ANALYSIS, REFERENCE_BRANCH, NUMBER_OF_DAYS'); } throw e; } Prevention
- Use enum constant names, not UI labels or snake_case variants.
- Trim the value; case is tolerated but spelling is not.
When it happens
Trigger: Calling api/new_code_periods/set with a type parameter not matching any NewCodePeriodType constant — e.g. 'days', 'previous_version ' with trailing space, or 'since_previous_version'.
Common situations: Typos or abbreviations in CI pipelines; assuming snake_case HTTP-style keys instead of enum names; documentation drift between SonarQube versions where enum constants were renamed.
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
- Failed to set the New Code Definition. The given value is no
- Invalid type '%s'. %s can only be set with types: %s
- Transition '%s' not supported. Only %s are supported.
- If branch key is specified, project key needs to be specifie
- Failed to parse number of days: %s
AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09).
Data as JSON: /api/errors/4b13556af527f6ec.
Report an issue: GitHub.