Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the constructor of AbstractApolloHttpException implementation class to support string template #3999

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
@@ -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);
}
}

Original file line number Diff line number Diff line change
@@ -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);
@@ -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);
}
@@ -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);
}
@@ -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);
Original file line number Diff line number Diff line change
@@ -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);
}
@@ -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);

@@ -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,
@@ -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);
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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{
@@ -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));
nobodyiam marked this conversation as resolved.
Show resolved Hide resolved
}

public AbstractApolloHttpException(String msg, Exception e){
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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);
}

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());
}

}