SonarSource/sonarqube · error · IllegalArgumentException

Invalid type:

Error message

Invalid type: 

What it means

New Code Definition types are restricted to the NewCodePeriodType enum (e.g. PREVIOUS_VERSION, NUMBER_OF_DAYS, REFERENCE, SPECIFIC_ANALYSIS). parseNewCodeDefinitionType uppercases the input and calls valueOf; unknown strings are rethrown as this IllegalArgumentException, and even valid enum names are then checked by validateType.

Solutions

  1. Use one of the exact supported types: PREVIOUS_VERSION, NUMBER_OF_DAYS, REFERENCE, SPECIFIC_ANALYSIS (case-insensitive).
  2. Check GET api/new_code_periods/available_types or the enum definition for valid names.
  3. Fix client code that passes a legacy or custom type string.

Example fix

// before
newCodePeriodType("days")

// after
newCodePeriodType("NUMBER_OF_DAYS")
Defensive patterns

Strategy: validation

Validate before calling

// allowlist check before calling
Set<String> VALID = Set.of("PREVIOUS_VERSION", "NUMBER_OF_DAYS", "REFERENCE", "SPECIFIC_ANALYSIS");
if (typeStr == null || !VALID.contains(typeStr.toUpperCase(Locale.US))) throw new IllegalArgumentException("Unsupported type: " + typeStr);

Try / catch

try { NewCodePeriodType t = NewCodePeriodType.valueOf(typeStr.toUpperCase(Locale.US)); } catch (IllegalArgumentException e) { log.error("Unknown new code type: {}", typeStr); }

Prevention

When it happens

Trigger: Calling newCodePeriodType (e.g. via api/new_code_periods) with a type string that does not match a NewCodePeriodType constant, such as 'NUMBER_OF_MONTHS', 'days', or 'main_branch'.

Common situations: Typo'd type parameters; clients written against older SonarQube versions with different type names; copying values from other SonarQube settings that use different enums.

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/365b1169e45db383. Report an issue: GitHub.

Appendix: source

Thrown at server/sonar-webserver-common/src/main/java/org/sonar/server/common/newcodeperiod/NewCodeDefinitionResolver.java:152

  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 newCodeDefinitionValue", type);
  }

  private static NewCodePeriodType parseNewCodeDefinitionType(String typeStr) {
    NewCodePeriodType type;
    try {
      type = NewCodePeriodType.valueOf(typeStr.toUpperCase(Locale.US));
    } catch (IllegalArgumentException e) {
      throw new IllegalArgumentException("Invalid type: " + typeStr);
    }
    validateType(type);
    return type;
  }

  private static void validateType(NewCodePeriodType type) {
    Preconditions.checkArgument(projectCreationNCDTypes.contains(type), "Invalid type '%s'. `newCodeDefinitionType` can only be set with types: %s",
      type, projectCreationNCDTypes);
  }

}

View on GitHub (pinned to 184c821202)