SonarSource/sonarqube · error · IllegalStateException

Unknown branch type '%s'

Error message

Unknown branch type '%s'

What it means

TaskFormatter.formatQueue/formatActivity build task representations and attach branch metadata via setBranchOrPullRequest. The task's branchType field must be BRANCH; any other value (e.g. PULL_REQUEST flowing through the branch path, or a corrupted/null-ish value) hits the default case and throws this IllegalStateException. It signals an internal inconsistency in how CE task branch data was stored or selected.

Source

Thrown at server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/TaskFormatter.java:162

    }
    builder.setComponentKey(componentDto.getKey());
    builder.setComponentName(componentDto.name());
    builder.setComponentQualifier(componentDto.qualifier());
    return builder;
  }

  private static Ce.Task.Builder setBranchOrPullRequest(Ce.Task.Builder builder, String taskUuid, DtoCache componentDtoCache) {
    componentDtoCache.getBranchKey(taskUuid).ifPresent(
      b -> {
        Common.BranchType branchType = componentDtoCache.getBranchType(taskUuid)
          .orElseThrow(() -> new IllegalStateException(format("Could not find branch type of task '%s'", taskUuid)));
        switch (branchType) {
          case BRANCH:
            builder.setBranchType(branchType);
            builder.setBranch(b);
            break;
          default:
            throw new IllegalStateException(String.format("Unknown branch type '%s'", branchType));
        }
      });
    componentDtoCache.getPullRequest(taskUuid).ifPresent(builder::setPullRequest);
    return builder;
  }

  private static List<String> extractWarningMessages(CeActivityDto dto) {
    return dto.getCeTaskMessageDtos().stream()
      .filter(ceTaskMessageDto -> ceTaskMessageDto.getType().isWarning())
      .map(CeTaskMessageDto::getMessage)
      .toList();
  }

  private static List<String> extractInfoMessages(CeActivityDto activityDto) {
    return activityDto.getCeTaskMessageDtos().stream()
      .filter(ceTaskMessageDto -> MessageType.INFO.equals(ceTaskMessageDto.getType()))
      .sorted(Comparator.comparing(CeTaskMessageDto::getCreatedAt))
      .map(CeTaskMessageDto::getMessage)

View on GitHub (pinned to 184c821202)

Solutions

  1. Check the ce_activity/branch rows backing the failing task UUID and correct branch_type to 'BRANCH'
  2. Verify the task is a branch task, not a pull-request task — PR tasks are resolved via componentDtoCache.getPullRequest and never enter this switch
  3. Clear stale task data via api/ce/clear or purge the task row and re-run analysis
  4. Upgrade to a SonarQube version where the formatter fix matches your DB schema

Example fix

// before
default:
  throw new IllegalStateException(String.format("Unknown branch type '%s'", branchType));
// after
case PULL_REQUEST:
  builder.setBranchType(branchType);
  break;
default:
  LOGGER.warn("Skipping unknown branch type '{}' for task {}", branchType, taskUuid);
Defensive patterns

Strategy: try-catch

Validate before calling

// Java: check before calling the formatter for a task
BranchDto branch = branchDao.selectByUuid(dbSession, task.getBranchUuid()).orElse(null);
if (branch == null || !"BRANCH".equals(branch.getBranchType())) {
    throw new IllegalArgumentException("task is not a branch task: " + task.getUuid());
}

Type guard

boolean isBranchTask(TaskDto t) {
  return "BRANCH".equals(t.getBranchType());
}

Try / catch

try {
  TaskFormatterMessage msg = formatter.format(task);
} catch (IllegalStateException e) {
  if (e.getMessage().startsWith("Unknown branch type")) {
    LOGGER.warn("Skipping task {} with unknown branch type", task.getUuid());
  } else { throw e; }
}

Prevention

When it happens

Trigger: A queued/activity CE task whose branchType is anything other than the BRANCH constant while setBranchOrPullRequest iterates it — e.g. a pull-request task wrongly surfaced in the branch lookup, or a DB row with an unexpected branch_type value.

Common situations: Plugin or DB migration wrote an unexpected branch_type; calling the api/ce/activity or api/ce/task endpoints for tasks where branch and pull request data are inconsistent; upgrading SonarQube with stale ce_activity rows.

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/875d1d3ae01af9ea. Report an issue: GitHub.