Skip to content

Commit

Permalink
Refactor NotFoundException、BadRequestException (#4812)
Browse files Browse the repository at this point in the history
* add tech-support-qq-4.png

* Update README.md

* Enhance the user experience in the scenario of submitting duplicate keys

* Modify the key-value conflict exception prompt, adjust the code style

* refactor(apollo-common): Refactor NotFoundException

* refactor(apollo-common): Add license

* refactor(apollo-common): Refactor BadRequestException

* refactor(apollo-common): Refactor NotFoundException

---------

Co-authored-by: Jason Song <[email protected]>
  • Loading branch information
klboke and nobodyiam authored Mar 26, 2023
1 parent 5903691 commit a556681
Show file tree
Hide file tree
Showing 46 changed files with 444 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void requireLockAdvice(String appId, String clusterName, String namespace
public void requireLockAdvice(long itemId, String operator) {
Item item = itemService.findOne(itemId);
if (item == null){
throw new BadRequestException("item not exist.");
throw BadRequestException.itemNotExists(itemId);
}
acquireLock(item.getNamespaceId(), operator);
}
Expand Down Expand Up @@ -117,7 +117,7 @@ void acquireLock(long namespaceId, String currentUser) {

private void acquireLock(Namespace namespace, String currentUser) {
if (namespace == null) {
throw new BadRequestException("namespace not exist.");
throw BadRequestException.namespaceNotExists();
}

long namespaceId = namespace.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void requireLockAdvice(String appId, String clusterName, String namespace
public void requireLockAdvice(long itemId, String operator) {
Item item = itemService.findOne(itemId);
if (item == null) {
throw new BadRequestException("item not exist.");
throw BadRequestException.itemNotExists(itemId);
}
tryUnlock(namespaceService.findOne(item.getNamespaceId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public AppDTO create(@Valid @RequestBody AppDTO dto) {
App entity = BeanUtils.transform(App.class, dto);
App managedEntity = appService.findOne(entity.getAppId());
if (managedEntity != null) {
throw new BadRequestException("app already exist.");
throw BadRequestException.appAlreadyExists(entity.getAppId());
}

entity = adminService.createNewApp(entity);
Expand All @@ -66,7 +66,7 @@ public AppDTO create(@Valid @RequestBody AppDTO dto) {
public void delete(@PathVariable("appId") String appId, @RequestParam String operator) {
App entity = appService.findOne(appId);
if (entity == null) {
throw new NotFoundException("app not found for appId " + appId);
throw NotFoundException.appNotFound(appId);
}
adminService.deleteApp(entity, operator);
}
Expand Down Expand Up @@ -96,7 +96,7 @@ public List<AppDTO> find(@RequestParam(value = "name", required = false) String
public AppDTO get(@PathVariable("appId") String appId) {
App app = appService.findOne(appId);
if (app == null) {
throw new NotFoundException("app not found for appId " + appId);
throw NotFoundException.appNotFound(appId);
}
return BeanUtils.transform(AppDTO.class, app);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public AppNamespaceDTO create(@RequestBody AppNamespaceDTO appNamespace,

entity = managedEntity;
} else {
throw new BadRequestException("app namespaces already exist.");
throw BadRequestException.appNamespaceAlreadyExists(entity.getAppId(), entity.getName());
}

return BeanUtils.transform(AppNamespaceDTO.class, entity);
Expand All @@ -80,7 +80,7 @@ public void delete(@PathVariable("appId") String appId, @PathVariable("namespace
@RequestParam String operator) {
AppNamespace entity = appNamespaceService.findOne(appId, namespaceName);
if (entity == null) {
throw new BadRequestException("app namespace not found for appId: " + appId + " namespace: " + namespaceName);
throw BadRequestException.appNamespaceNotExists(appId, namespaceName);
}
appNamespaceService.deleteAppNamespace(entity, operator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ClusterDTO create(@PathVariable("appId") String appId,
Cluster entity = BeanUtils.transform(Cluster.class, dto);
Cluster managedEntity = clusterService.findOne(appId, entity.getName());
if (managedEntity != null) {
throw new BadRequestException("cluster already exist.");
throw BadRequestException.clusterAlreadyExists(entity.getName());
}

if (autoCreatePrivateNamespace) {
Expand All @@ -69,7 +69,7 @@ public void delete(@PathVariable("appId") String appId,
Cluster entity = clusterService.findOne(appId, clusterName);

if (entity == null) {
throw new NotFoundException("cluster not found for clusterName " + clusterName);
throw NotFoundException.clusterNotFound(appId, clusterName);
}

if(ConfigConsts.CLUSTER_NAME_DEFAULT.equals(entity.getName())){
Expand All @@ -90,7 +90,7 @@ public ClusterDTO get(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName) {
Cluster cluster = clusterService.findOne(appId, clusterName);
if (cluster == null) {
throw new NotFoundException("cluster not found for name " + clusterName);
throw NotFoundException.clusterNotFound(appId, clusterName);
}
return BeanUtils.transform(ClusterDTO.class, cluster);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public PageDTO<InstanceDTO> getByRelease(@RequestParam("releaseId") long release
Pageable pageable) {
Release release = releaseService.findOne(releaseId);
if (release == null) {
throw new NotFoundException("release not found for %s", releaseId);
throw NotFoundException.releaseNotFound(releaseId);
}
Page<InstanceConfig> instanceConfigsPage = instanceService.findActiveInstanceConfigsByReleaseKey
(release.getReleaseKey(), pageable);
Expand Down Expand Up @@ -123,7 +123,7 @@ public List<InstanceDTO> getByReleasesNotIn(@RequestParam("appId") String appId,
List<Release> releases = releaseService.findByReleaseIds(releaseIdSet);

if (CollectionUtils.isEmpty(releases)) {
throw new NotFoundException("releases not found for %s", releaseIds);
throw NotFoundException.releaseNotFound(releaseIds);
}

Set<String> releaseKeys = releases.stream().map(Release::getReleaseKey).collect(Collectors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public ItemDTO create(@PathVariable("appId") String appId,

Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey());
if (managedEntity != null) {
throw new BadRequestException("item already exists");
throw BadRequestException.itemAlreadyExists(entity.getKey());
}
entity = itemService.save(entity);
dto = BeanUtils.transform(ItemDTO.class, entity);
Expand Down Expand Up @@ -122,13 +122,13 @@ public ItemDTO update(@PathVariable("appId") String appId,
@RequestBody ItemDTO itemDTO) {
Item managedEntity = itemService.findOne(itemId);
if (managedEntity == null) {
throw new NotFoundException("item not found for itemId " + itemId);
throw NotFoundException.itemNotFound(appId, clusterName, namespaceName, itemId);
}

Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
// In case someone constructs an attack scenario
if (namespace == null || namespace.getId() != managedEntity.getNamespaceId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
throw BadRequestException.namespaceNotMatch();
}

Item entity = BeanUtils.transform(Item.class, itemDTO);
Expand Down Expand Up @@ -159,7 +159,7 @@ public ItemDTO update(@PathVariable("appId") String appId,
public void delete(@PathVariable("itemId") long itemId, @RequestParam String operator) {
Item entity = itemService.findOne(itemId);
if (entity == null) {
throw new NotFoundException("item not found for itemId " + itemId);
throw NotFoundException.itemNotFound(itemId);
}
itemService.delete(entity.getId(), operator);

Expand Down Expand Up @@ -205,7 +205,7 @@ public List<ItemDTO> findDeletedItems(@PathVariable("appId") String appId,
public ItemDTO get(@PathVariable("itemId") long itemId) {
Item item = itemService.findOne(itemId);
if (item == null) {
throw new NotFoundException("item not found for itemId " + itemId);
throw NotFoundException.itemNotFound(itemId);
}
return BeanUtils.transform(ItemDTO.class, item);
}
Expand All @@ -216,8 +216,7 @@ public ItemDTO get(@PathVariable("appId") String appId,
@PathVariable("namespaceName") String namespaceName, @PathVariable("key") String key) {
Item item = itemService.findOne(appId, clusterName, namespaceName, key);
if (item == null) {
throw new NotFoundException("item not found for %s %s %s %s", appId, clusterName,
namespaceName, key);
throw NotFoundException.itemNotFound(appId, clusterName, namespaceName, key);
}
return BeanUtils.transform(ItemDTO.class, item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ private void checkBranch(String appId, String clusterName, String namespaceName,
private void checkNamespace(String appId, String clusterName, String namespaceName) {
Namespace parentNamespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (parentNamespace == null) {
throw new BadRequestException(
"Namespace not exist. AppId = %s, ClusterName = %s, NamespaceName = %s", appId,
clusterName, namespaceName);
throw BadRequestException.namespaceNotExists(appId, clusterName, namespaceName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public NamespaceDTO create(@PathVariable("appId") String appId,
Namespace entity = BeanUtils.transform(Namespace.class, dto);
Namespace managedEntity = namespaceService.findOne(appId, clusterName, entity.getNamespaceName());
if (managedEntity != null) {
throw new BadRequestException("namespace already exist.");
throw BadRequestException.namespaceAlreadyExists(entity.getNamespaceName());
}

entity = namespaceService.save(entity);
Expand All @@ -68,8 +68,7 @@ public void delete(@PathVariable("appId") String appId,
@PathVariable("namespaceName") String namespaceName, @RequestParam String operator) {
Namespace entity = namespaceService.findOne(appId, clusterName, namespaceName);
if (entity == null) {
throw new NotFoundException("namespace not found for %s %s %s", appId, clusterName,
namespaceName);
throw NotFoundException.namespaceNotFound(appId, clusterName, namespaceName);
}

namespaceService.deleteNamespace(entity, operator);
Expand All @@ -86,7 +85,7 @@ public List<NamespaceDTO> find(@PathVariable("appId") String appId,
public NamespaceDTO get(@PathVariable("namespaceId") Long namespaceId) {
Namespace namespace = namespaceService.findOne(namespaceId);
if (namespace == null) {
throw new NotFoundException("namespace not found for %s", namespaceId);
throw NotFoundException.itemNotFound(namespaceId);
}
return BeanUtils.transform(NamespaceDTO.class, namespace);
}
Expand All @@ -109,8 +108,7 @@ public NamespaceDTO get(@PathVariable("appId") String appId,
@PathVariable("namespaceName") String namespaceName) {
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (namespace == null) {
throw new NotFoundException("namespace not found for %s %s %s", appId, clusterName,
namespaceName);
throw NotFoundException.namespaceNotFound(appId, clusterName, namespaceName);
}
return BeanUtils.transform(NamespaceDTO.class, namespace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public NamespaceLockDTO getNamespaceLockOwner(@PathVariable String appId, @PathV
@PathVariable String namespaceName) {
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (namespace == null) {
throw new BadRequestException("namespace not exist.");
throw BadRequestException.namespaceNotExists(appId, clusterName, namespaceName);
}

if (bizConfig.isNamespaceLockSwitchOff()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ReleaseController(
public ReleaseDTO get(@PathVariable("releaseId") long releaseId) {
Release release = releaseService.findOne(releaseId);
if (release == null) {
throw new NotFoundException("release not found for %s", releaseId);
throw NotFoundException.releaseNotFound(releaseId);
}
return BeanUtils.transform(ReleaseDTO.class, release);
}
Expand Down Expand Up @@ -125,8 +125,7 @@ public ReleaseDTO publish(@PathVariable("appId") String appId,
@RequestParam(name = "isEmergencyPublish", defaultValue = "false") boolean isEmergencyPublish) {
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (namespace == null) {
throw new NotFoundException("Could not find namespace for %s %s %s", appId, clusterName,
namespaceName);
throw NotFoundException.namespaceNotFound(appId, clusterName, namespaceName);
}
Release release = releaseService.publish(namespace, releaseName, releaseComment, operator, isEmergencyPublish);

Expand Down Expand Up @@ -162,8 +161,7 @@ public ReleaseDTO updateAndPublish(@PathVariable("appId") String appId,
@RequestBody ItemChangeSets changeSets) {
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (namespace == null) {
throw new NotFoundException("Could not find namespace for %s %s %s", appId, clusterName,
namespaceName);
throw NotFoundException.namespaceNotFound(appId, clusterName, namespaceName);
}

Release release = releaseService.mergeBranchChangeSetsAndRelease(namespace, branchName, releaseName,
Expand Down Expand Up @@ -214,11 +212,10 @@ public ReleaseDTO publish(@PathVariable("appId") String appId,
@RequestParam(name = "grayDelKeys") Set<String> grayDelKeys){
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (namespace == null) {
throw new NotFoundException("Could not find namespace for %s %s %s", appId, clusterName,
namespaceName);
throw NotFoundException.namespaceNotFound(appId, clusterName, namespaceName);
}

Release release = releaseService.grayDeletionPublish(namespace, releaseName, releaseComment,
Release release = releaseService.grayDeletionPublish(namespace, releaseName, releaseComment,
operator, isEmergencyPublish, grayDelKeys);

//send release message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public AccessKey update(String appId, AccessKey entity) {

AccessKey accessKey = accessKeyRepository.findOneByAppIdAndId(appId, id);
if (accessKey == null) {
throw new BadRequestException("AccessKey not exist");
throw BadRequestException.accessKeyNotExists();
}

accessKey.setEnabled(entity.isEnabled());
Expand All @@ -86,7 +86,7 @@ public AccessKey update(String appId, AccessKey entity) {
public void delete(String appId, long id, String operator) {
AccessKey accessKey = accessKeyRepository.findOneByAppIdAndId(appId, id);
if (accessKey == null) {
throw new BadRequestException("AccessKey not exist");
throw BadRequestException.accessKeyNotExists();
}

if (accessKey.isEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void update(App app) {

App managedApp = appRepository.findByAppId(appId);
if (managedApp == null) {
throw new BadRequestException("App not exists. AppId = %s", appId);
throw BadRequestException.appNotExists(appId);
}

managedApp.setName(app.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public Cluster saveWithoutInstanceOfAppNamespaces(Cluster entity) {
public void delete(long id, String operator) {
Cluster cluster = clusterRepository.findById(id).orElse(null);
if (cluster == null) {
throw new BadRequestException("cluster not exist");
throw BadRequestException.clusterNotExists("");
}

//delete linked namespaces
Expand Down Expand Up @@ -151,7 +151,7 @@ public void createDefaultCluster(String appId, String createBy) {
public List<Cluster> findChildClusters(String appId, String parentClusterName) {
Cluster parentCluster = findOne(appId, parentClusterName);
if (parentCluster == null) {
throw new BadRequestException("parent cluster not exist");
throw BadRequestException.clusterNotExists(parentClusterName);
}

return clusterRepository.findByParentClusterId(parentCluster.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ private Namespace findNamespaceByAppIdAndClusterNameAndNamespaceName(String appI
String namespaceName) {
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (namespace == null) {
throw new NotFoundException("namespace not found for appId:%s clusterName:%s namespaceName:%s",
appId, clusterName, namespaceName);
throw NotFoundException.namespaceNotFound(appId, clusterName, namespaceName);
}
return namespace;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ItemChangeSets updateSet(String appId, String clusterName,
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);

if (namespace == null) {
throw new NotFoundException("Namespace %s not found", namespaceName);
throw NotFoundException.namespaceNotFound(appId, clusterName, namespaceName);
}

String operator = changeSet.getDataChangeLastModifiedBy();
Expand Down Expand Up @@ -96,7 +96,7 @@ private void doDeleteItems(List<ItemDTO> toDeleteItems, Namespace namespace, Str
for (ItemDTO item : toDeleteItems) {
Item deletedItem = itemService.delete(item.getId(), operator);
if (deletedItem.getNamespaceId() != namespace.getId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
throw BadRequestException.namespaceNotMatch();
}

configChangeContentBuilder.deleteItem(deletedItem);
Expand All @@ -111,10 +111,10 @@ private void doUpdateItems(List<ItemDTO> toUpdateItems, Namespace namespace, Str

Item managedItem = itemService.findOne(entity.getId());
if (managedItem == null) {
throw new NotFoundException("item not found.(key=%s)", entity.getKey());
throw NotFoundException.itemNotFound(entity.getKey());
}
if (managedItem.getNamespaceId() != namespace.getId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
throw BadRequestException.namespaceNotMatch();
}
Item beforeUpdateItem = BeanUtils.transform(Item.class, managedItem);

Expand All @@ -135,7 +135,7 @@ private void doCreateItems(List<ItemDTO> toCreateItems, Namespace namespace, Str

for (ItemDTO item : toCreateItems) {
if (item.getNamespaceId() != namespace.getId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
throw BadRequestException.namespaceNotMatch();
}

Item entity = BeanUtils.transform(Item.class, item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public NamespaceBranchService(
public Namespace createBranch(String appId, String parentClusterName, String namespaceName, String operator){
Namespace childNamespace = findBranch(appId, parentClusterName, namespaceName);
if (childNamespace != null){
throw new BadRequestException("namespace already has branch");
throw BadRequestException.namespaceNotExists(appId, parentClusterName, namespaceName);
}

Cluster parentCluster = clusterService.findOne(appId, parentClusterName);
if (parentCluster == null || parentCluster.getParentClusterId() != 0) {
throw new BadRequestException("cluster not exist or illegal cluster");
throw BadRequestException.clusterNotExists(parentClusterName);
}

//create child cluster
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public Page<Namespace> findByItem(String itemKey, Pageable pageable) {
public Namespace findPublicNamespaceForAssociatedNamespace(String clusterName, String namespaceName) {
AppNamespace appNamespace = appNamespaceService.findPublicNamespaceByName(namespaceName);
if (appNamespace == null) {
throw new BadRequestException("namespace not exist");
throw BadRequestException.namespaceNotExists("", clusterName, namespaceName);
}

String appId = appNamespace.getAppId();
Expand Down Expand Up @@ -392,7 +392,7 @@ public void instanceOfAppNamespaces(String appId, String clusterName, String cre
public Map<String, Boolean> namespacePublishInfo(String appId) {
List<Cluster> clusters = clusterService.findParentClusters(appId);
if (CollectionUtils.isEmpty(clusters)) {
throw new BadRequestException("app not exist");
throw BadRequestException.appNotExists(appId);
}

Map<String, Boolean> clusterHasNotPublishedItems = Maps.newHashMap();
Expand Down
Loading

0 comments on commit a556681

Please sign in to comment.