SonarSource/sonarqube · error · IllegalArgumentException
Failed to parse number of days: %s
Error message
Failed to parse number of days: %s
What it means
parseDays converts a NUMBER_OF_DAYS value into an integer via NewCodePeriodParser.parseDays; any parse failure is rethrown as IllegalArgumentException with the offending raw value. This guards the New Code Definition 'days' parameter against non-numeric or malformed input.
Source
Thrown at server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java:217
case REFERENCE_BRANCH -> {
requireValue(type, value);
dto.setValue(value);
}
default -> throw new IllegalStateException("Unexpected type: " + type);
}
}
private static void checkValuesForSpecificBranch(NewCodePeriodType type, @Nullable String value, @Nullable ProjectDto project, @Nullable BranchDto branch) {
requireValue(type, value);
requireProject(type, project);
requireBranch(type, branch);
}
private static String parseDays(String value) {
try {
return Integer.toString(NewCodePeriodParser.parseDays(value));
} catch (Exception e) {
throw new IllegalArgumentException("Failed to parse number of days: " + value);
}
}
private static void requireValue(NewCodePeriodType type, @Nullable String value) {
Preconditions.checkArgument(value != null, "New code definition type '%s' requires a value", type);
}
private static void requireBranch(NewCodePeriodType type, @Nullable BranchDto branch) {
Preconditions.checkArgument(branch != null, "New code definition type '%s' requires a branch", type);
}
private static void requireProject(NewCodePeriodType type, @Nullable ProjectDto project) {
Preconditions.checkArgument(project != null, "New code definition type '%s' requires a project", type);
}
private BranchDto getBranch(DbSession dbSession, ProjectDto project, String branchKey) {
return dbClient.branchDao().selectByBranchKey(dbSession, project.getUuid(), branchKey)
.orElseThrow(() -> new NotFoundException(format("Branch '%s' in project '%s' not found", branchKey, project.getKey())));View on GitHub (pinned to 184c821202)
Solutions
- Pass a bare positive integer string, e.g. value=30.
- Strip units and whitespace from the value in the calling script before sending.
- Validate with a regex like ^[0-9]+$ client-side before invoking the endpoint.
- Check that template/CI variables actually resolved to a number.
Example fix
// before POST api/new_code_periods/set?project=my-app&type=NUMBER_OF_DAYS&value=30d // after POST api/new_code_periods/set?project=my-app&type=NUMBER_OF_DAYS&value=30
Defensive patterns
Strategy: validation
Validate before calling
if (params.type === 'NUMBER_OF_DAYS') { if (!/^\d+$/.test(String(params.value))) throw new Error('NUMBER_OF_DAYS value must be a positive integer: ' + params.value); } Type guard
const isValidDays = (v) => typeof v === 'string' && /^\d+$/.test(v.trim());
Try / catch
try { ... } catch (e) { if (String(e.message).startsWith('Failed to parse number of days')) { log('Bad NUMBER_OF_DAYS value: ' + e.message.split(': ').pop()); } throw e; } Prevention
- Strip units ('d', 'days') and whitespace before sending the value.
- Check CI/template variables actually interpolate to a plain integer.
When it happens
Trigger: Calling api/new_code_periods/set with type=NUMBER_OF_DAYS and a value that is not a plain positive integer — e.g. '30d', 'one month', '-5', or an empty string.
Common situations: Scripts appending units ('30 days'); locale-formatted numbers; null/empty values produced by template variables that failed to interpolate; negative durations assumed valid.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- If branch key is specified, project key needs to be specifie
- Failed to set the New Code Definition. The given value is no
- Invalid type: %s
- Invalid type '%s'. %s can only be set with types: %s
- If branch key is specified, project key needs to be specifie
AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09).
Data as JSON: /api/errors/3109118b4cc1b112.
Report an issue: GitHub.