Browse Source

Merge pull request #91 from yiming187/ex_update

Refactor Exception
lepdou 9 years ago
parent
commit
d1c7c2a276

+ 32 - 0
apollo-core/src/main/java/com/ctrip/apollo/core/exception/AbstractBaseException.java

@@ -0,0 +1,32 @@
+package com.ctrip.apollo.core.exception;
+
+public abstract class AbstractBaseException extends RuntimeException{
+
+  /**
+   * 
+   */
+  private static final long serialVersionUID = -1713129594004951820L;
+  
+  private String errorCode;
+  
+  public AbstractBaseException(){
+    
+  }
+  
+  public AbstractBaseException(String str){
+    super(str);
+  }
+  
+  public AbstractBaseException(String str, String errorCode){
+    super(str);
+    this.setErrorCode(errorCode);
+  }
+
+  public String getErrorCode() {
+    return errorCode;
+  }
+
+  public void setErrorCode(String errorCode) {
+    this.errorCode = errorCode;
+  }
+}

+ 14 - 0
apollo-core/src/main/java/com/ctrip/apollo/core/exception/BadRequestException.java

@@ -0,0 +1,14 @@
+package com.ctrip.apollo.core.exception;
+
+public class BadRequestException extends AbstractBaseException {
+
+  /**
+   * 
+   */
+  private static final long serialVersionUID = 6060826407312134068L;
+
+
+  public BadRequestException(String str) {
+    super(str);
+  }
+}

+ 3 - 6
apollo-core/src/main/java/com/ctrip/apollo/core/exception/NotFoundException.java

@@ -1,16 +1,13 @@
 package com.ctrip.apollo.core.exception;
 
-public class NotFoundException extends RuntimeException {
+public class NotFoundException extends AbstractBaseException {
   /**
    *
    */
   private static final long serialVersionUID = 7611357629749481796L;
 
-  public NotFoundException(){
-    
-  }
-  
-  public NotFoundException(String str){
+
+  public NotFoundException(String str) {
     super(str);
   }
 }

+ 2 - 2
apollo-core/src/main/java/com/ctrip/apollo/core/exception/ServiceException.java

@@ -1,11 +1,11 @@
 package com.ctrip.apollo.core.exception;
 
-public class ServiceException extends RuntimeException {
+public class ServiceException extends AbstractBaseException {
 
   /**
    * 
    */
-  private static final long serialVersionUID = 1L;
+  private static final long serialVersionUID = -6529123764065547791L;
 
   public ServiceException(String str) {
     super(str);

+ 11 - 20
apollo-portal/src/main/java/com/ctrip/apollo/portal/controller/AppController.java

@@ -1,21 +1,19 @@
 package com.ctrip.apollo.portal.controller;
 
-import com.google.common.base.Strings;
-
-import com.ctrip.apollo.core.dto.AppDTO;
-import com.ctrip.apollo.core.utils.StringUtils;
-import com.ctrip.apollo.portal.entity.ClusterNavTree;
-import com.ctrip.apollo.portal.service.AppService;
-
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
 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.RestController;
 
+import com.ctrip.apollo.core.dto.AppDTO;
+import com.ctrip.apollo.core.exception.BadRequestException;
+import com.ctrip.apollo.core.utils.StringUtils;
+import com.ctrip.apollo.portal.entity.ClusterNavTree;
+import com.ctrip.apollo.portal.service.AppService;
+import com.google.common.base.Strings;
+
 @RestController
 @RequestMapping("/apps")
 public class AppController {
@@ -26,30 +24,23 @@ public class AppController {
   @RequestMapping("/{appId}/navtree")
   public ClusterNavTree nav(@PathVariable String appId) {
     if (Strings.isNullOrEmpty(appId)) {
-      throw new IllegalArgumentException("app id can not be empty.");
+      throw new BadRequestException("app id can not be empty.");
     }
 
     return appService.buildClusterNavTree(appId);
   }
 
   @RequestMapping(value = "", method = RequestMethod.POST, consumes = {"application/json"})
-  public ResponseEntity<AppDTO> create(@RequestBody AppDTO app) {
+  public AppDTO create(@RequestBody AppDTO app) {
     if (isInvalidApp(app)){
-      return ResponseEntity.badRequest().body(null);
+      throw new BadRequestException("request payload contains empty");
     }
     AppDTO createdApp = appService.save(app);
-    if (createdApp != null){
-      return ResponseEntity.ok().body(createdApp);
-    }else {
-      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
-    }
+    return createdApp;
   }
 
   private boolean isInvalidApp(AppDTO app) {
     return StringUtils.isContainEmpty(app.getName(), app.getAppId(), app.getOwnerEmail(), app.getOwnerName());
   }
-
-
-
 }
 

+ 23 - 19
apollo-portal/src/main/java/com/ctrip/apollo/portal/controller/ConfigController.java

@@ -3,6 +3,7 @@ package com.ctrip.apollo.portal.controller;
 
 import com.ctrip.apollo.Apollo;
 import com.ctrip.apollo.core.dto.ReleaseDTO;
+import com.ctrip.apollo.core.exception.BadRequestException;
 import com.ctrip.apollo.core.utils.StringUtils;
 import com.ctrip.apollo.portal.entity.form.NamespaceModifyModel;
 import com.ctrip.apollo.portal.entity.NamespaceVO;
@@ -31,7 +32,7 @@ public class ConfigController {
 
   @RequestMapping("/apps/{appId}/env/{env}/clusters/{clusterName}/namespaces")
   public List<NamespaceVO> findNamespaces(@PathVariable String appId, @PathVariable String env,
-                                          @PathVariable String clusterName) {
+      @PathVariable String clusterName) {
     if (StringUtils.isContainEmpty(appId, env, clusterName)) {
       throw new IllegalArgumentException("app id and cluster name can not be empty");
     }
@@ -39,21 +40,22 @@ public class ConfigController {
     return configService.findNampspaces(appId, Apollo.Env.valueOf(env), clusterName);
   }
 
-  @RequestMapping(value = "/apps/{appId}/env/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items",method = RequestMethod.PUT, consumes = {"application/json"})
+  @RequestMapping(value = "/apps/{appId}/env/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.PUT, consumes = {
+      "application/json"})
   public ResponseEntity<SimpleMsg> modifyItems(@PathVariable String appId, @PathVariable String env,
-                                                 @PathVariable String clusterName, @PathVariable String namespaceName,
-                                                 @RequestBody NamespaceModifyModel model) {
+      @PathVariable String clusterName, @PathVariable String namespaceName,
+      @RequestBody NamespaceModifyModel model) {
 
-    if (model == null){
-      return ResponseEntity.badRequest().body(new SimpleMsg("form data exception."));
+    if (model == null) {
+      throw new BadRequestException("request payload shoud not be null");
     }
     model.setAppId(appId);
     model.setClusterName(clusterName);
     model.setEnv(env);
     model.setNamespaceName(namespaceName);
 
-    if (model.isInvalid()){
-      return ResponseEntity.badRequest().body(new SimpleMsg("form data exception."));
+    if (model.isInvalid()) {
+      throw new BadRequestException("request model is invalid");
     }
 
     TextResolverResult result = configService.resolveConfigText(model);
@@ -65,27 +67,29 @@ public class ConfigController {
     }
   }
 
-  @RequestMapping(value = "/apps/{appId}/env/{env}/clusters/{clusterName}/namespaces/{namespaceName}/release", method = RequestMethod.POST, consumes = {"application/json"})
-  public ResponseEntity<SimpleMsg> createRelease(@PathVariable String appId, @PathVariable String env,
-                                                 @PathVariable String clusterName, @PathVariable String namespaceName,
-                                                 @RequestBody NamespaceReleaseModel model){
-    if (model == null){
-      return ResponseEntity.badRequest().body(new SimpleMsg("form data exception."));
+  @RequestMapping(value = "/apps/{appId}/env/{env}/clusters/{clusterName}/namespaces/{namespaceName}/release", method = RequestMethod.POST, consumes = {
+      "application/json"})
+  public ResponseEntity<SimpleMsg> createRelease(@PathVariable String appId,
+      @PathVariable String env, @PathVariable String clusterName,
+      @PathVariable String namespaceName, @RequestBody NamespaceReleaseModel model) {
+    if (model == null) {
+      throw new BadRequestException("request payload shoud not be null");
     }
     model.setAppId(appId);
     model.setClusterName(clusterName);
     model.setEnv(env);
     model.setNamespaceName(namespaceName);
 
-    if (model.isInvalid()){
-      return ResponseEntity.badRequest().body(new SimpleMsg("form data exception."));
+    if (model.isInvalid()) {
+      throw new BadRequestException("request model is invalid");
     }
 
     ReleaseDTO release = configService.release(model);
 
-    if (release == null){
-      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new SimpleMsg("oops! some error in server."));
-    }else {
+    if (release == null) {
+      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
+          .body(new SimpleMsg("oops! some error in server."));
+    } else {
       return ResponseEntity.ok().body(new SimpleMsg("success"));
     }
   }

+ 8 - 3
apollo-portal/src/main/java/com/ctrip/apollo/portal/controller/GlobalDefaultExceptionHandler.java

@@ -7,8 +7,8 @@ import org.springframework.web.HttpMediaTypeException;
 import org.springframework.web.HttpRequestMethodNotSupportedException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.ResponseStatus;
 
+import com.ctrip.apollo.core.exception.BadRequestException;
 import com.ctrip.apollo.core.exception.NotFoundException;
 
 import java.time.LocalDateTime;
@@ -58,8 +58,13 @@ public class GlobalDefaultExceptionHandler {
   }
 
   @ExceptionHandler(NotFoundException.class)
-  @ResponseStatus(value = NOT_FOUND)
-  public void notFound(HttpServletRequest req, NotFoundException ex) {
+  public ResponseEntity<Map<String, Object>> notFound(HttpServletRequest request, NotFoundException ex) {
+    return handleError(request, NOT_FOUND, ex);
+  }
+  
+  @ExceptionHandler(BadRequestException.class)
+  public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request, BadRequestException ex){
+    return handleError(request, BAD_REQUEST, ex);
   }
 
   private Throwable resolveError(Throwable ex) {

+ 1 - 4
apollo-portal/src/main/java/com/ctrip/apollo/portal/service/AppService.java

@@ -10,12 +10,9 @@ import org.springframework.stereotype.Service;
 
 import com.ctrip.apollo.Apollo.Env;
 import com.ctrip.apollo.core.dto.AppDTO;
-import com.ctrip.apollo.core.utils.StringUtils;
 import com.ctrip.apollo.portal.PortalSettings;
 import com.ctrip.apollo.portal.api.AdminServiceAPI;
 import com.ctrip.apollo.portal.entity.ClusterNavTree;
-import com.ctrip.apollo.portal.entity.SimpleMsg;
-
 
 @Service
 public class AppService {
@@ -52,7 +49,7 @@ public class AppService {
       return appAPI.save(Env.LOCAL, app);
     } catch (Exception e) {
       logger.error("oops! save app error. app id:{}", app.getAppId(), e);
-      return null;
+      throw e;
     }
   }
 

+ 0 - 1
apollo-portal/src/main/java/com/ctrip/apollo/portal/service/ClusterService.java

@@ -7,7 +7,6 @@ import com.ctrip.apollo.portal.api.AdminServiceAPI;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.util.Arrays;
 import java.util.List;
 
 @Service