This is an automated email from the ASF dual-hosted git repository.

min pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo-ops.git


The following commit(s) were added to refs/heads/develop by this push:
     new aa7fd5d  Add exception uniform catch and response status 
specification(#109) (#147)
aa7fd5d is described below

commit aa7fd5d81248cf80f7c2684592f4fac1b831e8da
Author: Zhiguo.Chen <[email protected]>
AuthorDate: Fri Oct 19 14:41:09 2018 +0800

    Add exception uniform catch and response status specification(#109) (#147)
    
    * Add exception uniform catch and response status specification
    
    * error code for restful api #109
    
    * Revert code for test
---
 .../apache/dubbo/admin/common/CommonResponse.java  | 107 +++++++++++++++++++++
 .../common/exception/ParamValidationException.java |  31 ++++++
 .../exception/PermissionDeniedException.java       |  39 ++++++++
 .../exception/ResourceNotFoundException.java       |  43 +++++++++
 .../admin/common/exception/ServiceException.java   |  47 +++++++++
 .../admin/common/exception/SystemException.java    |  43 +++++++++
 .../dubbo/admin/controller/AccessesController.java |  18 ++--
 .../admin/controller/LoadBalanceController.java    |  28 ++++--
 .../admin/controller/OverridesController.java      |  14 ++-
 .../dubbo/admin/controller/RoutesController.java   |  11 ++-
 .../dubbo/admin/controller/WeightController.java   |   8 +-
 .../admin/handler/CustomExceptionHandler.java      |  85 ++++++++++++++++
 12 files changed, 450 insertions(+), 24 deletions(-)

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..6c3ff1b 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 @@ package org.apache.dubbo.admin.controller;
 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,7 +68,6 @@ public class AccessesController {
                 result.add(accessDTO);
             }
         }
-
         return result;
     }
 
@@ -88,25 +91,23 @@ public class AccessesController {
 
     @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 +137,9 @@ public class AccessesController {
     @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 @@ public class LoadBalanceController {
     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 class LoadBalanceController {
     }
 
     @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 class LoadBalanceController {
     }
 
     @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 21bab9a..b69cfd9 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,10 +18,15 @@
 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;
@@ -37,10 +42,11 @@ public class OverridesController {
     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);
@@ -56,7 +62,7 @@ public class OverridesController {
     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());
@@ -94,7 +100,7 @@ public class OverridesController {
     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 4a1e847..3c4de21 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,11 +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.BaseDTO;
 import org.apache.dubbo.admin.dto.RouteDTO;
 import org.apache.dubbo.admin.governance.service.ProviderService;
 import org.apache.dubbo.admin.governance.service.RouteService;
 import org.apache.dubbo.admin.registry.common.domain.Route;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.ArrayList;
@@ -37,11 +41,12 @@ public class RoutesController {
     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
@@ -67,7 +72,7 @@ public class RoutesController {
     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!");
         }
         routeDTO.setVersion(route.getVersion());
         routeDTO.setService(route.getService());
@@ -100,7 +105,7 @@ public class RoutesController {
     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 = convertRoutetoRouteDTO(route, id);
         return routeDTO;
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 @@ public class WeightController {
     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 class WeightController {
     @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

Reply via email to