chenzhiguo closed pull request #146: Add exception uniform catch and response status specification URL: https://github.com/apache/incubator-dubbo-ops/pull/146
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/CommonResponse.java b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/CommonResponse.java new file mode 100644 index 0000000..b1f5f11 --- /dev/null +++ b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/CommonResponse.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.admin.common; + +import com.google.common.collect.Lists; + +import java.util.Collection; +import java.util.HashMap; + +/** + * Common Response + */ +public class CommonResponse extends HashMap<String, Object> { + + private static final String MESSAGE = "message"; + + private static final String SUCCESS = "success"; + + private static final String DATA = "data"; + + public boolean isSuccess() { + return get(SUCCESS) != null && (Boolean) get(SUCCESS); + } + + public String getMessage() { + if (get(MESSAGE) != null) { + return (String) get(MESSAGE); + } + return ""; + } + + private CommonResponse() { + super(); + this.put(SUCCESS, false); + } + + public CommonResponse success() { + this.put(SUCCESS, true); + return this; + } + + public CommonResponse success(String message) { + this.put(SUCCESS, true); + this.put(MESSAGE, message); + return this; + } + + public CommonResponse fail(String message) { + this.put(SUCCESS, false); + this.put(MESSAGE, message); + return this; + } + + public CommonResponse redirect(String url) { + this.put("redirect", url); + return this; + } + + public CommonResponse setData(Object data) { + Collection collection; + if (!containsKey(DATA) || get(DATA) == null) { + collection = Lists.newArrayList(); + put(DATA, collection); + } else { + collection = (Collection) get(DATA); + } + collection.add(data); + return this; + } + + public CommonResponse setData(String key, Object data) { + this.put(key, data); + return this; + } + + public CommonResponse setData(Collection collection) { + this.put(DATA, collection); + return this; + } + + public static CommonResponse createCommonResponse() { + CommonResponse commonResponse = new CommonResponse(); + commonResponse.success(); + return commonResponse; + } + + public static CommonResponse createCommonResponse(Object data) { + CommonResponse commonResponse = new CommonResponse(); + commonResponse.success(); + commonResponse.setData(data); + return commonResponse; + } +} \ No newline at end of file diff --git a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/ParamValidationException.java b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/ParamValidationException.java new file mode 100644 index 0000000..1b598ad --- /dev/null +++ b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/ParamValidationException.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.admin.common.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * Parameter validation failure exception + */ +@ResponseStatus(value = HttpStatus.BAD_REQUEST) +public class ParamValidationException extends SystemException { + + public ParamValidationException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/PermissionDeniedException.java b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/PermissionDeniedException.java new file mode 100644 index 0000000..a5ecc79 --- /dev/null +++ b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/PermissionDeniedException.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.admin.common.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * Permission denied exception + */ +@ResponseStatus(value = HttpStatus.UNAUTHORIZED) +public class PermissionDeniedException extends RuntimeException { + + public PermissionDeniedException(String message) { + super(message); + } + + public PermissionDeniedException(String message, Throwable cause) { + super(message, cause); + } + + protected PermissionDeniedException() { + super(); + } +} \ No newline at end of file diff --git a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/ResourceNotFoundException.java b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/ResourceNotFoundException.java new file mode 100644 index 0000000..e1251f9 --- /dev/null +++ b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/ResourceNotFoundException.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.admin.common.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * Resource not found exception + */ +@ResponseStatus(value = HttpStatus.NOT_FOUND) +public class ResourceNotFoundException extends SystemException { + + public ResourceNotFoundException() { + } + + public ResourceNotFoundException(String message) { + super(message); + } + + public ResourceNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public ResourceNotFoundException(Throwable cause) { + super(cause); + } + +} \ No newline at end of file diff --git a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/ServiceException.java b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/ServiceException.java new file mode 100644 index 0000000..1e31331 --- /dev/null +++ b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/ServiceException.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.admin.common.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * Service Logic Exception + */ +@ResponseStatus(value = HttpStatus.SERVICE_UNAVAILABLE) +public class ServiceException extends RuntimeException { + + public ServiceException() { + } + + public ServiceException(String message) { + super(message); + } + + public ServiceException(String message, Throwable cause) { + super(message, cause); + } + + public ServiceException(Throwable cause) { + super(cause); + } + + public ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} \ No newline at end of file diff --git a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/SystemException.java b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/SystemException.java new file mode 100644 index 0000000..924bfc2 --- /dev/null +++ b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/common/exception/SystemException.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.admin.common.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * System Exception + */ +@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) +public class SystemException extends RuntimeException { + + public SystemException() { + super(); + } + + public SystemException(String message) { + super(message); + } + + public SystemException(String message, Throwable cause) { + super(message, cause); + } + + public SystemException(Throwable cause) { + super(cause); + } +} \ No newline at end of file diff --git a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/AccessesController.java b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/AccessesController.java index 967296b..e6d3bb1 100644 --- a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/AccessesController.java +++ b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/AccessesController.java @@ -19,10 +19,14 @@ import com.alibaba.dubbo.common.logger.Logger; import com.alibaba.dubbo.common.logger.LoggerFactory; import org.apache.commons.lang3.StringUtils; + +import org.apache.dubbo.admin.common.exception.ParamValidationException; +import org.apache.dubbo.admin.common.exception.ResourceNotFoundException; import org.apache.dubbo.admin.dto.AccessDTO; import org.apache.dubbo.admin.governance.service.RouteService; import org.apache.dubbo.admin.registry.common.domain.Route; import org.apache.dubbo.admin.registry.common.route.RouteRule; +import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @@ -64,8 +68,8 @@ result.add(accessDTO); } } - - return result; +throw new ParseException("222",3); +// return result; } @RequestMapping(value = "/{id}", method = RequestMethod.GET) @@ -88,25 +92,23 @@ public AccessDTO detailAccess(@PathVariable String id, @PathVariable String env) @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) public void deleteAccess(@PathVariable String id, @PathVariable String env) { - if (id == null) { - throw new IllegalArgumentException("Argument of id is null!"); - } routeService.deleteRoute(id); } @RequestMapping(method = RequestMethod.POST) + @ResponseStatus(HttpStatus.CREATED) public void createAccess(@RequestBody AccessDTO accessDTO, @PathVariable String env) { if (StringUtils.isBlank(accessDTO.getService())) { - throw new IllegalArgumentException("Service is required."); + throw new ParamValidationException("Service is required."); } if (accessDTO.getBlacklist() == null && accessDTO.getWhitelist() == null) { - throw new IllegalArgumentException("One of Blacklist/Whitelist is required."); + throw new ParamValidationException("One of Blacklist/Whitelist is required."); } Route route = routeService.getBlackwhitelistRouteByService(accessDTO.getService()); if (route != null) { - throw new IllegalArgumentException(accessDTO.getService() + " is existed."); + throw new ParamValidationException(accessDTO.getService() + " is existed."); } route = new Route(); @@ -136,6 +138,9 @@ public void createAccess(@RequestBody AccessDTO accessDTO, @PathVariable String @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public void updateAccess(@PathVariable String id, @RequestBody AccessDTO accessDTO, @PathVariable String env) { Route route = routeService.findRoute(id); + if (Objects.isNull(route)) { + throw new ResourceNotFoundException("Unknown ID!"); + } Map<String, RouteRule.MatchPair> when = new HashMap<>(); RouteRule.MatchPair matchPair = new RouteRule.MatchPair(new HashSet<>(), new HashSet<>()); when.put(Route.KEY_CONSUMER_HOST, matchPair); diff --git a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/LoadBalanceController.java b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/LoadBalanceController.java index 29665c2..e5812f5 100644 --- a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/LoadBalanceController.java +++ b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/LoadBalanceController.java @@ -17,13 +17,24 @@ package org.apache.dubbo.admin.controller; +import org.apache.dubbo.admin.common.exception.ParamValidationException; +import org.apache.dubbo.admin.common.exception.ResourceNotFoundException; import org.apache.dubbo.admin.dto.BalancingDTO; import org.apache.dubbo.admin.governance.service.OverrideService; import org.apache.dubbo.admin.registry.common.domain.LoadBalance; import org.apache.dubbo.admin.registry.common.domain.Override; import org.apache.dubbo.admin.registry.common.util.OverrideUtils; + import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; +import org.springframework.http.HttpStatus; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; @@ -38,10 +49,11 @@ private OverrideService overrideService; @RequestMapping(method = RequestMethod.POST) - public boolean createLoadbalance(@RequestBody BalancingDTO balancingDTO, @PathVariable String env) { + @ResponseStatus(HttpStatus.CREATED) + public boolean createLoadbalance(@RequestBody BalancingDTO balancingDTO, @PathVariable String env) throws ParamValidationException { String serviceName = balancingDTO.getService(); - if (serviceName == null || serviceName.length() == 0) { - //TODO throw exception + if (StringUtils.isEmpty(serviceName)) { + throw new ParamValidationException("serviceName is Empty!"); } LoadBalance loadBalance = new LoadBalance(); loadBalance.setService(serviceName); @@ -52,10 +64,10 @@ public boolean createLoadbalance(@RequestBody BalancingDTO balancingDTO, @PathVa } @RequestMapping(value = "/{id}", method = RequestMethod.PUT) - public boolean updateLoadbalance(@PathVariable String id, @RequestBody BalancingDTO balancingDTO, @PathVariable String env) { + public boolean updateLoadbalance(@PathVariable String id, @RequestBody BalancingDTO balancingDTO, @PathVariable String env) throws ParamValidationException { Override override = overrideService.findById(id); if (override == null) { - //TODO throw exception + throw new ResourceNotFoundException("Unknown ID!"); } LoadBalance old = overrideToLoadBalance(override); LoadBalance loadBalance = new LoadBalance(); @@ -94,10 +106,10 @@ public boolean updateLoadbalance(@PathVariable String id, @RequestBody Balancing } @RequestMapping(value = "/{id}", method = RequestMethod.GET) - public BalancingDTO detailLoadBalance(@PathVariable String id, @PathVariable String env) { + public BalancingDTO detailLoadBalance(@PathVariable String id, @PathVariable String env) throws ParamValidationException { Override override = overrideService.findById(id); if (override == null) { - //TODO throw exception + throw new ResourceNotFoundException("Unknown ID!"); } LoadBalance loadBalance = OverrideUtils.overrideToLoadBalance(override); diff --git a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/OverridesController.java b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/OverridesController.java index cfd5e6a..3d7f157 100644 --- a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/OverridesController.java +++ b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/OverridesController.java @@ -18,11 +18,16 @@ package org.apache.dubbo.admin.controller; import com.alibaba.dubbo.common.URL; + +import org.apache.dubbo.admin.common.exception.ParamValidationException; +import org.apache.dubbo.admin.common.exception.ResourceNotFoundException; import org.apache.dubbo.admin.dto.BaseDTO; import org.apache.dubbo.admin.dto.OverrideDTO; import org.apache.dubbo.admin.governance.service.OverrideService; import org.apache.dubbo.admin.registry.common.domain.Override; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; @@ -38,10 +43,11 @@ private OverrideService overrideService; @RequestMapping(method = RequestMethod.POST) + @ResponseStatus(HttpStatus.CREATED) public boolean createOverride(@RequestBody OverrideDTO overrideDTO, @PathVariable String env) { String serviceName = overrideDTO.getService(); - if (serviceName == null || serviceName.length() == 0) { - //TODO throw exception + if (StringUtils.isEmpty(serviceName)) { + throw new ParamValidationException("serviceName is Empty!"); } Override override = new Override(); override.setService(serviceName); @@ -57,7 +63,7 @@ public boolean createOverride(@RequestBody OverrideDTO overrideDTO, @PathVariabl public boolean updateOverride(@PathVariable String id, @RequestBody OverrideDTO overrideDTO, @PathVariable String env) { Override old = overrideService.findById(id); if (old == null) { - //TODO handle exception + throw new ResourceNotFoundException("Unknown ID!"); } Override override = new Override(); override.setService(overrideDTO.getService()); @@ -95,7 +101,7 @@ public boolean updateOverride(@PathVariable String id, @RequestBody OverrideDTO public OverrideDTO detailOverride(@PathVariable String id, @PathVariable String env) { Override override = overrideService.findById(id); if (override == null) { - //TODO throw exception + throw new ResourceNotFoundException("Unknown ID!"); } OverrideDTO overrideDTO = new OverrideDTO(); overrideDTO.setAddress(override.getAddress()); diff --git a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/RoutesController.java b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/RoutesController.java index 888fe5c..1f920ed 100644 --- a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/RoutesController.java +++ b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/RoutesController.java @@ -17,6 +17,8 @@ package org.apache.dubbo.admin.controller; +import org.apache.dubbo.admin.common.exception.ParamValidationException; +import org.apache.dubbo.admin.common.exception.ResourceNotFoundException; import org.apache.dubbo.admin.dto.BaseDTO; import org.apache.dubbo.admin.dto.RouteDTO; import org.apache.dubbo.admin.governance.service.ProviderService; @@ -24,6 +26,7 @@ import org.apache.dubbo.admin.registry.common.domain.Route; import org.apache.dubbo.admin.util.MD5Util; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; @@ -39,11 +42,12 @@ private ProviderService providerService; @RequestMapping(method = RequestMethod.POST) + @ResponseStatus(HttpStatus.CREATED) public boolean createRule(@RequestBody RouteDTO routeDTO, @PathVariable String env) { String serviceName = routeDTO.getService(); String app = routeDTO.getApp(); if (serviceName == null && app == null) { - + throw new ParamValidationException("serviceName and app is Empty!"); } if (serviceName != null) { //2.6 @@ -77,7 +81,7 @@ public boolean createRule(@RequestBody RouteDTO routeDTO, @PathVariable String e public boolean updateRule(@PathVariable String id, @RequestBody RouteDTO routeDTO, @PathVariable String env) { Route route = routeService.findRoute(id); if (route == null) { - //TODO Exception + throw new ResourceNotFoundException("Unknown ID!"); } String[] conditions = routeDTO.getConditions(); String rule = parseCondition(conditions); @@ -128,7 +132,7 @@ public boolean updateRule(@PathVariable String id, @RequestBody RouteDTO routeDT public RouteDTO detailRoute(@PathVariable String id, @PathVariable String env) { Route route = routeService.findRoute(id); if (route == null) { - // TODO throw exception + throw new ResourceNotFoundException("Unknown ID!"); } RouteDTO routeDTO = new RouteDTO(); routeDTO.setDynamic(route.isDynamic()); diff --git a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/WeightController.java b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/WeightController.java index 7a8e3de..4f11626 100644 --- a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/WeightController.java +++ b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/controller/WeightController.java @@ -17,12 +17,15 @@ package org.apache.dubbo.admin.controller; +import org.apache.dubbo.admin.common.exception.ParamValidationException; +import org.apache.dubbo.admin.common.exception.ResourceNotFoundException; import org.apache.dubbo.admin.dto.WeightDTO; import org.apache.dubbo.admin.governance.service.OverrideService; import org.apache.dubbo.admin.registry.common.domain.Override; import org.apache.dubbo.admin.registry.common.domain.Weight; import org.apache.dubbo.admin.registry.common.util.OverrideUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; @@ -36,6 +39,7 @@ private OverrideService overrideService; @RequestMapping(method = RequestMethod.POST) + @ResponseStatus(HttpStatus.CREATED) public boolean createWeight(@RequestBody WeightDTO weightDTO, @PathVariable String env) { String[] addresses = weightDTO.getProvider(); for (String address : addresses) { @@ -51,11 +55,11 @@ public boolean createWeight(@RequestBody WeightDTO weightDTO, @PathVariable Stri @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public boolean updateWeight(@PathVariable String id, @RequestBody WeightDTO weightDTO, @PathVariable String env) { if (id == null) { - //TODO throw exception + throw new ParamValidationException("Unknown ID!"); } Override override = overrideService.findById(id); if (override == null) { - //TODO throw exception + throw new ResourceNotFoundException("Unknown ID!"); } Weight old = OverrideUtils.overrideToWeight(override); Weight weight = new Weight(); diff --git a/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/handler/CustomExceptionHandler.java b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/handler/CustomExceptionHandler.java new file mode 100644 index 0000000..fd9ec0b --- /dev/null +++ b/dubbo-admin-backend/src/main/java/org/apache/dubbo/admin/handler/CustomExceptionHandler.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.admin.handler; + +import org.apache.dubbo.admin.common.CommonResponse; +import org.apache.dubbo.admin.common.exception.ParamValidationException; +import org.apache.dubbo.admin.common.exception.PermissionDeniedException; +import org.apache.dubbo.admin.common.exception.ResourceNotFoundException; +import org.apache.dubbo.admin.common.exception.ServiceException; + +import com.alibaba.dubbo.common.logger.Logger; +import com.alibaba.dubbo.common.logger.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * Custom Exception handler + */ +@ControllerAdvice(annotations = ResponseBody.class) +public class CustomExceptionHandler { + + private static final Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class); + + @ResponseBody + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler(value = Exception.class) + public CommonResponse commonExceptionHandle(Exception e) { + CommonResponse commonResponse = CommonResponse.createCommonResponse(); + logger.error("[SystemException]Exception:", e); + return commonResponse.fail("System Error, please try again later! Message:" + e.getMessage()); + } + + @ResponseBody + @ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE) + @ExceptionHandler(value = ServiceException.class) + public CommonResponse serviceExceptionHandle(Exception e) { + CommonResponse commonResponse = CommonResponse.createCommonResponse(); + logger.error("[ServiceException]Exception:", e); + return commonResponse.fail("ServiceException, message:" + e.getMessage()); + } + + @ResponseBody + @ResponseStatus(HttpStatus.UNAUTHORIZED) + @ExceptionHandler(value = PermissionDeniedException.class) + public CommonResponse permissionDeniedExceptionHandle(Exception e) { + CommonResponse commonResponse = CommonResponse.createCommonResponse(); + logger.error("[PermissionDeniedException]Exception:", e); + return commonResponse.fail("Permission Denied!"); + } + + @ResponseBody + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(value = {ParamValidationException.class}) + public CommonResponse paramValidationExceptionHandle(Exception e) { + CommonResponse commonResponse = CommonResponse.createCommonResponse(); + logger.error("[ParamValidationException]Exception:", e); + return commonResponse.fail("Parameter validation failure! Message:" + e.getMessage()); + } + + @ResponseBody + @ResponseStatus(HttpStatus.NOT_FOUND) + @ExceptionHandler(value = {ResourceNotFoundException.class}) + public CommonResponse resourceNotFoundExceptionHandle(Exception e) { + CommonResponse commonResponse = CommonResponse.createCommonResponse(); + logger.error("[ResourceNotFoundException]Exception:", e); + return commonResponse.fail("Resource not found! Message:" + e.getMessage()); + } +} \ No newline at end of file ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
