Skip to content

Commit

Permalink
Make the constructor of AbstractApolloHttpException implementation cl…
Browse files Browse the repository at this point in the history
…ass to support string template

Signed-off-by: WillardHu <wei.hu@daocloud.io>
  • Loading branch information
WillardHu authored and nobodyiam committed Sep 27, 2021
1 parent 2fb3479 commit c488995
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 36 deletions.
Original file line number Diff line number Diff line change
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(String.format("releases not found for %s", releaseIds));
throw new NotFoundException("releases not found for %s", 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 @@ -197,15 +197,14 @@ public ItemDTO get(@PathVariable("itemId") long itemId) {

@GetMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{key:.+}")
public ItemDTO get(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName, @PathVariable("key") String key) {
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName, @PathVariable("key") String key) {
Item item = itemService.findOne(appId, clusterName, namespaceName, key);
if (item == null) {
throw new NotFoundException(
String.format("item not found for %s %s %s %s", appId, clusterName, namespaceName, key));
throw new NotFoundException("item not found for %s %s %s %s", appId, clusterName,
namespaceName, key);
}
return BeanUtils.transform(ItemDTO.class, item);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,19 @@ private void checkBranch(String appId, String clusterName, String namespaceName,
//2. check child namespace
Namespace childNamespace = namespaceService.findOne(appId, branchName, namespaceName);
if (childNamespace == null) {
throw new BadRequestException(String.format("Namespace's branch not exist. AppId = %s, ClusterName = %s, "
+ "NamespaceName = %s, BranchName = %s",
appId, clusterName, namespaceName, branchName));
throw new BadRequestException(
"Namespace's branch not exist. AppId = %s, ClusterName = %s, NamespaceName = %s, BranchName = %s",
appId, clusterName, namespaceName, branchName);
}

}

private void checkNamespace(String appId, String clusterName, String namespaceName) {
Namespace parentNamespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (parentNamespace == null) {
throw new BadRequestException(String.format("Namespace not exist. AppId = %s, ClusterName = %s, NamespaceName = %s", appId,
clusterName, namespaceName));
throw new BadRequestException(
"Namespace not exist. AppId = %s, ClusterName = %s, NamespaceName = %s", appId,
clusterName, namespaceName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ 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(
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
throw new NotFoundException("namespace not found for %s %s %s", appId, clusterName,
namespaceName);
}

namespaceService.deleteNamespace(entity, operator);
Expand All @@ -86,7 +86,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(String.format("namespace not found for %s", namespaceId));
throw new NotFoundException("namespace not found for %s", namespaceId);
}
return BeanUtils.transform(NamespaceDTO.class, namespace);
}
Expand All @@ -109,8 +109,8 @@ public NamespaceDTO get(@PathVariable("appId") String appId,
@PathVariable("namespaceName") String namespaceName) {
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (namespace == null) {
throw new NotFoundException(
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
throw new NotFoundException("namespace not found for %s %s %s", appId, clusterName,
namespaceName);
}
return BeanUtils.transform(NamespaceDTO.class, namespace);
}
Expand All @@ -122,7 +122,7 @@ public NamespaceDTO findPublicNamespaceForAssociatedNamespace(@PathVariable Stri
Namespace namespace = namespaceService.findPublicNamespaceForAssociatedNamespace(clusterName, namespaceName);

if (namespace == null) {
throw new NotFoundException(String.format("public namespace not found. namespace:%s", namespaceName));
throw new NotFoundException("public namespace not found. namespace:%s", namespaceName);
}

return BeanUtils.transform(NamespaceDTO.class, namespace);
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(String.format("release not found for %s", releaseId));
throw new NotFoundException("release not found for %s", releaseId);
}
return BeanUtils.transform(ReleaseDTO.class, release);
}
Expand Down Expand Up @@ -125,8 +125,8 @@ 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(String.format("Could not find namespace for %s %s %s", appId,
clusterName, namespaceName));
throw new NotFoundException("Could not find namespace for %s %s %s", appId, clusterName,
namespaceName);
}
Release release = releaseService.publish(namespace, releaseName, releaseComment, operator, isEmergencyPublish);

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

Release release = releaseService.mergeBranchChangeSetsAndRelease(namespace, branchName, releaseName,
Expand Down Expand Up @@ -214,11 +214,12 @@ 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(String.format("Could not find namespace for %s %s %s", appId,
clusterName, namespaceName));
throw new NotFoundException("Could not find namespace for %s %s %s", appId, clusterName,
namespaceName);
}

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

//send release message
Namespace parentNamespace = namespaceService.findParentNamespace(namespace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class ReleaseHistoryController {

private static final Gson GSON = new Gson();

private Type configurationTypeReference = new TypeToken<Map<String, Object>>() {
private final Type configurationTypeReference = new TypeToken<Map<String, Object>>() {
}.getType();

private final ReleaseHistoryService releaseHistoryService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.ctrip.framework.apollo.common.exception;

import com.google.common.base.Strings;
import org.springframework.http.HttpStatus;

public abstract class AbstractApolloHttpException extends RuntimeException{
Expand All @@ -24,8 +25,15 @@ public abstract class AbstractApolloHttpException extends RuntimeException{

protected HttpStatus httpStatus;

public AbstractApolloHttpException(String msg){
super(msg);
/**
* When args not empty, use {@link com.google.common.base.Strings#lenientFormat(String, Object...)}
* to replace %s in msgtpl with args to set the error message. Otherwise, use msgtpl
* to set the error message. e.g.:
* <pre>{@code new NotFoundException("... %s ... %s ... %s", "str", 0, 0.1)}</pre>
* If the number of '%s' in `msgtpl` does not match args length, the '%s' string will be printed.
*/
public AbstractApolloHttpException(String msgtpl, Object... args){
super(args == null || args.length == 0 ? msgtpl : Strings.lenientFormat(msgtpl, args));
}

public AbstractApolloHttpException(String msg, Exception e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@

public class BadRequestException extends AbstractApolloHttpException {


public BadRequestException(String str) {
super(str);
/**
* @see AbstractApolloHttpException#AbstractApolloHttpException(String, Object...)
*/
public BadRequestException(String msgtpl, Object... args) {
super(msgtpl, args);
setHttpStatus(HttpStatus.BAD_REQUEST);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

public class NotFoundException extends AbstractApolloHttpException {


public NotFoundException(String str) {
super(str);
/**
* @see AbstractApolloHttpException#AbstractApolloHttpException(String, Object...)
*/
public NotFoundException(String msgtpl, Object... args) {
super(msgtpl, args);
setHttpStatus(HttpStatus.NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@

public class ServiceException extends AbstractApolloHttpException {

public ServiceException(String str) {
super(str);
/**
* @see AbstractApolloHttpException#AbstractApolloHttpException(String, Object...)
*/
public ServiceException(String msgtpl, Object... args) {
super(msgtpl, args);
setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2021 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.common.exception;

import org.junit.Assert;
import org.junit.Test;

public class NotFoundExceptionTest {

@Test
public void testConstructor() {
String appId = "app-1001";
String clusterName = "test";
String namespaceName = "application";
String key = "test.key";
NotFoundException e1, e2;
e1 = new NotFoundException("item not found for %s %s %s %s", appId,
clusterName, namespaceName, key);
e2 = new NotFoundException(
String.format("item not found for %s %s %s %s", appId, clusterName, namespaceName, key));
Assert.assertEquals(e1.getMessage(), e2.getMessage());
}

}

0 comments on commit c488995

Please sign in to comment.