{"record":{"id":"a14e01f24b9c412d","repo":"apolloconfig/apollo","slug":"consumer-already-exist","errorCode":null,"errorMessage":"Consumer already exist","messagePattern":"Consumer already exist","errorType":"validation","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/service/ConsumerService.java","lineNumber":104,"sourceCode":"      final RoleRepository roleRepository) {\n    this.consumerTokenRepository = consumerTokenRepository;\n    this.consumerRepository = consumerRepository;\n    this.consumerAuditRepository = consumerAuditRepository;\n    this.consumerRoleRepository = consumerRoleRepository;\n    this.portalConfig = portalConfig;\n    this.rolePermissionService = rolePermissionService;\n    this.userService = userService;\n    this.roleRepository = roleRepository;\n  }\n\n\n  public Consumer createConsumer(Consumer consumer, String operator) {\n    validateOperator(operator);\n    String appId = consumer.getAppId();\n\n    Consumer managedConsumer = consumerRepository.findByAppId(appId);\n    if (managedConsumer != null) {\n      throw new BadRequestException(\"Consumer already exist\");\n    }\n\n    String ownerName = consumer.getOwnerName();\n    UserInfo owner = userService.findByUserId(ownerName);\n    if (owner == null) {\n      throw BadRequestException.userNotExists(ownerName);\n    }\n    consumer.setOwnerEmail(owner.getEmail());\n\n    consumer.setDataChangeCreatedBy(operator);\n    consumer.setDataChangeLastModifiedBy(operator);\n\n    return consumerRepository.save(consumer);\n  }\n\n  public ConsumerToken generateAndSaveConsumerToken(Consumer consumer, Integer rateLimit,\n      Date expires, String operator) {\n    validateOperator(operator);","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/apolloconfig/apollo/blob/d95fc18d112589efc09ddcbe1507047584d55251/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/service/ConsumerService.java#L86-L122","documentation":"Thrown as BadRequestException (HTTP 400) by ConsumerService.createConsumer when a Consumer already exists for the given appId (consumerRepository.findByAppId returns non-null). Each Apollo appId can have at most one OpenAPI Consumer (token holder).","triggerScenarios":"Creating a Consumer for an appId that already has a Consumer row; re-running a consumer-creation script against the same appId.","commonSituations":"See trigger scenarios.","solutions":["Reuse the existing Consumer (look it up by appId) instead of creating a new one.","Delete the existing Consumer first if you truly need to recreate it.","Make provisioning scripts idempotent: findByAppId first, create only if absent."],"exampleFix":"// before\nconsumerService.createConsumer(consumer, operator); // 400 if appId exists\n// after\nConsumer existing = consumerRepository.findByAppId(appId);\nif (existing != null) {\n  return existing;\n}\nreturn consumerService.createConsumer(consumer, operator);","handlingStrategy":"validation","validationCode":"Consumer existing = consumerRepository.findByAppId(appId);\nif (existing == null) {\n  consumerService.createConsumer(consumer, operator);\n} else {\n  return existing;\n}","typeGuard":null,"tryCatchPattern":"try {\n  return consumerService.createConsumer(consumer, operator);\n} catch (BadRequestException e) {\n  if (\"Consumer already exist\".equals(e.getMessage())) {\n    return consumerRepository.findByAppId(appId);\n  }\n  throw e;\n}","preventionTips":["Make consumer-provisioning scripts idempotent (findByAppId first).","Treat duplicate-consumer as recoverable, not fatal."],"tags":["consumer","openapi","duplicate","bad-request","apollo-portal"],"backgroundTag":null,"analyzedSha":"d95fc18d112589efc09ddcbe1507047584d55251","analyzedAt":"2026-08-14T04:00:05.477Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}