apolloconfig/apollo · error · BadRequestException

Namespace's role does not exist. Please check whether namesp

Error message

Namespace's role does not exist. Please check whether namespace has created.

What it means

Thrown as BadRequestException (HTTP 400) by ConsumerService.assignNamespaceRoleToConsumer when the namespace's Modify or Release Role does not exist (rolePermissionService.findRoleByRoleName returns null for either). Roles are created when a namespace is created, so a missing role means the namespace was not fully/properly created.

Source

Thrown at apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/service/ConsumerService.java:178

    return assignNamespaceRoleToConsumer(token, appId, namespaceName, null, operator);
  }

  @Transactional
  public List<ConsumerRole> assignNamespaceRoleToConsumer(String token, String appId,
      String namespaceName, String env, String operator) {
    validateOperator(operator);
    Long consumerId = getConsumerIdByToken(token);
    if (consumerId == null) {
      throw new BadRequestException("Token is Illegal");
    }

    Role namespaceModifyRole = rolePermissionService
        .findRoleByRoleName(RoleUtils.buildModifyNamespaceRoleName(appId, namespaceName, env));
    Role namespaceReleaseRole = rolePermissionService
        .findRoleByRoleName(RoleUtils.buildReleaseNamespaceRoleName(appId, namespaceName, env));

    if (namespaceModifyRole == null || namespaceReleaseRole == null) {
      throw new BadRequestException(
          "Namespace's role does not exist. Please check whether namespace has created.");
    }

    long namespaceModifyRoleId = namespaceModifyRole.getId();
    long namespaceReleaseRoleId = namespaceReleaseRole.getId();

    ConsumerRole managedModifyRole =
        consumerRoleRepository.findByConsumerIdAndRoleId(consumerId, namespaceModifyRoleId);
    ConsumerRole managedReleaseRole =
        consumerRoleRepository.findByConsumerIdAndRoleId(consumerId, namespaceReleaseRoleId);
    if (managedModifyRole != null && managedReleaseRole != null) {
      return Arrays.asList(managedModifyRole, managedReleaseRole);
    }

    ConsumerRole namespaceModifyConsumerRole =
        createConsumerRole(consumerId, namespaceModifyRoleId, operator);
    ConsumerRole namespaceReleaseConsumerRole =
        createConsumerRole(consumerId, namespaceReleaseRoleId, operator);

View on GitHub (pinned to d95fc18d11)

Solutions

  1. Ensure the namespace exists (and its roles are seeded) in the target env before assigning the consumer role.
  2. Verify the appId, namespaceName, and env exactly match the role-name builder expectations.
  3. Re-trigger namespace creation if roles are missing, then retry the assignment.
Defensive patterns

Strategy: validation

Validate before calling

Role modify = rolePermissionService.findRoleByRoleName(
    RoleUtils.buildModifyNamespaceRoleName(appId, namespaceName, env));
Role release = rolePermissionService.findRoleByRoleName(
    RoleUtils.buildReleaseNamespaceRoleName(appId, namespaceName, env));
if (modify == null || release == null) {
  // ensure the namespace (and its roles) exist in this env first
}

Try / catch

try {
  consumerService.assignNamespaceRoleToConsumer(token, appId, namespaceName, env, operator);
} catch (BadRequestException e) {
  if (e.getMessage().contains("role does not exist")) {
    // (re)create namespace to seed roles, then retry
  } else throw e;
}

Prevention

When it happens

Trigger: Assigning a consumer to a namespace whose modify/release roles are absent — typically because the namespace was never created, creation was incomplete, or env-specific roles (RoleUtils.buildModifyNamespaceRoleName / buildReleaseNamespaceRoleName with env) don't exist for that env.

Common situations: Calling role assignment before namespace creation finishes; assigning an env-scoped role for an env where the namespace roles weren't seeded; namespace created in some envs but not the target env.

Related errors


AI-assisted analysis of apolloconfig/apollo@d95fc18d11 (2026-08-14). Data as JSON: /api/errors/5827726651e48e65. Report an issue: GitHub.