{"record":{"id":"3109118b4cc1b112","repo":"SonarSource/sonarqube","slug":"failed-to-parse-number-of-days-s","errorCode":null,"errorMessage":"Failed to parse number of days: %s","messagePattern":"Failed to parse number of days: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":400,"severity":"error","filePath":"server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java","lineNumber":217,"sourceCode":"      case REFERENCE_BRANCH -> {\n        requireValue(type, value);\n        dto.setValue(value);\n      }\n      default -> throw new IllegalStateException(\"Unexpected type: \" + type);\n    }\n  }\n\n  private static void checkValuesForSpecificBranch(NewCodePeriodType type, @Nullable String value, @Nullable ProjectDto project, @Nullable BranchDto branch) {\n    requireValue(type, value);\n    requireProject(type, project);\n    requireBranch(type, branch);\n  }\n\n  private static String parseDays(String value) {\n    try {\n      return Integer.toString(NewCodePeriodParser.parseDays(value));\n    } catch (Exception e) {\n      throw new IllegalArgumentException(\"Failed to parse number of days: \" + value);\n    }\n  }\n\n  private static void requireValue(NewCodePeriodType type, @Nullable String value) {\n    Preconditions.checkArgument(value != null, \"New code definition type '%s' requires a value\", type);\n  }\n\n  private static void requireBranch(NewCodePeriodType type, @Nullable BranchDto branch) {\n    Preconditions.checkArgument(branch != null, \"New code definition type '%s' requires a branch\", type);\n  }\n\n  private static void requireProject(NewCodePeriodType type, @Nullable ProjectDto project) {\n    Preconditions.checkArgument(project != null, \"New code definition type '%s' requires a project\", type);\n  }\n\n  private BranchDto getBranch(DbSession dbSession, ProjectDto project, String branchKey) {\n    return dbClient.branchDao().selectByBranchKey(dbSession, project.getUuid(), branchKey)\n      .orElseThrow(() -> new NotFoundException(format(\"Branch '%s' in project '%s' not found\", branchKey, project.getKey())));","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java#L199-L235","documentation":"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.","triggerScenarios":"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.","commonSituations":"Scripts appending units ('30 days'); locale-formatted numbers; null/empty values produced by template variables that failed to interpolate; negative durations assumed valid.","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."],"exampleFix":"// before\nPOST api/new_code_periods/set?project=my-app&type=NUMBER_OF_DAYS&value=30d\n// after\nPOST api/new_code_periods/set?project=my-app&type=NUMBER_OF_DAYS&value=30","handlingStrategy":"validation","validationCode":"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); }","typeGuard":"const isValidDays = (v) => typeof v === 'string' && /^\\d+$/.test(v.trim());","tryCatchPattern":"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; }","preventionTips":["Strip units ('d', 'days') and whitespace before sending the value.","Check CI/template variables actually interpolate to a plain integer."],"tags":["webapi","new-code-period","parsing","sonarqube"],"backgroundTag":"invalid-argument-format","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}