提交 d1c7c2a2 编写于 作者: L lepdou

Merge pull request #91 from yiming187/ex_update

Refactor Exception
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;
}
}
package com.ctrip.apollo.core.exception;
public class BadRequestException extends AbstractBaseException {
/**
*
*/
private static final long serialVersionUID = 6060826407312134068L;
public BadRequestException(String str) {
super(str);
}
}
package com.ctrip.apollo.core.exception; package com.ctrip.apollo.core.exception;
public class NotFoundException extends RuntimeException { public class NotFoundException extends AbstractBaseException {
/** /**
* *
*/ */
private static final long serialVersionUID = 7611357629749481796L; private static final long serialVersionUID = 7611357629749481796L;
public NotFoundException(){
} public NotFoundException(String str) {
public NotFoundException(String str){
super(str); super(str);
} }
} }
package com.ctrip.apollo.core.exception; 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) { public ServiceException(String str) {
super(str); super(str);
......
package com.ctrip.apollo.portal.controller; 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.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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; 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 @RestController
@RequestMapping("/apps") @RequestMapping("/apps")
public class AppController { public class AppController {
...@@ -26,30 +24,23 @@ public class AppController { ...@@ -26,30 +24,23 @@ public class AppController {
@RequestMapping("/{appId}/navtree") @RequestMapping("/{appId}/navtree")
public ClusterNavTree nav(@PathVariable String appId) { public ClusterNavTree nav(@PathVariable String appId) {
if (Strings.isNullOrEmpty(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); return appService.buildClusterNavTree(appId);
} }
@RequestMapping(value = "", method = RequestMethod.POST, consumes = {"application/json"}) @RequestMapping(value = "", method = RequestMethod.POST, consumes = {"application/json"})
public ResponseEntity<AppDTO> create(@RequestBody AppDTO app) { public AppDTO create(@RequestBody AppDTO app) {
if (isInvalidApp(app)){ if (isInvalidApp(app)){
return ResponseEntity.badRequest().body(null); throw new BadRequestException("request payload contains empty");
} }
AppDTO createdApp = appService.save(app); AppDTO createdApp = appService.save(app);
if (createdApp != null){ return createdApp;
return ResponseEntity.ok().body(createdApp);
}else {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
} }
private boolean isInvalidApp(AppDTO app) { private boolean isInvalidApp(AppDTO app) {
return StringUtils.isContainEmpty(app.getName(), app.getAppId(), app.getOwnerEmail(), app.getOwnerName()); return StringUtils.isContainEmpty(app.getName(), app.getAppId(), app.getOwnerEmail(), app.getOwnerName());
} }
} }
...@@ -3,6 +3,7 @@ package com.ctrip.apollo.portal.controller; ...@@ -3,6 +3,7 @@ package com.ctrip.apollo.portal.controller;
import com.ctrip.apollo.Apollo; import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.dto.ReleaseDTO; 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.core.utils.StringUtils;
import com.ctrip.apollo.portal.entity.form.NamespaceModifyModel; import com.ctrip.apollo.portal.entity.form.NamespaceModifyModel;
import com.ctrip.apollo.portal.entity.NamespaceVO; import com.ctrip.apollo.portal.entity.NamespaceVO;
...@@ -39,21 +40,22 @@ public class ConfigController { ...@@ -39,21 +40,22 @@ public class ConfigController {
return configService.findNampspaces(appId, Apollo.Env.valueOf(env), clusterName); 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, public ResponseEntity<SimpleMsg> modifyItems(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName, @PathVariable String namespaceName, @PathVariable String clusterName, @PathVariable String namespaceName,
@RequestBody NamespaceModifyModel model) { @RequestBody NamespaceModifyModel model) {
if (model == null){ if (model == null) {
return ResponseEntity.badRequest().body(new SimpleMsg("form data exception.")); throw new BadRequestException("request payload shoud not be null");
} }
model.setAppId(appId); model.setAppId(appId);
model.setClusterName(clusterName); model.setClusterName(clusterName);
model.setEnv(env); model.setEnv(env);
model.setNamespaceName(namespaceName); model.setNamespaceName(namespaceName);
if (model.isInvalid()){ if (model.isInvalid()) {
return ResponseEntity.badRequest().body(new SimpleMsg("form data exception.")); throw new BadRequestException("request model is invalid");
} }
TextResolverResult result = configService.resolveConfigText(model); TextResolverResult result = configService.resolveConfigText(model);
...@@ -65,27 +67,29 @@ public class ConfigController { ...@@ -65,27 +67,29 @@ public class ConfigController {
} }
} }
@RequestMapping(value = "/apps/{appId}/env/{env}/clusters/{clusterName}/namespaces/{namespaceName}/release", method = RequestMethod.POST, consumes = {"application/json"}) @RequestMapping(value = "/apps/{appId}/env/{env}/clusters/{clusterName}/namespaces/{namespaceName}/release", method = RequestMethod.POST, consumes = {
public ResponseEntity<SimpleMsg> createRelease(@PathVariable String appId, @PathVariable String env, "application/json"})
@PathVariable String clusterName, @PathVariable String namespaceName, public ResponseEntity<SimpleMsg> createRelease(@PathVariable String appId,
@RequestBody NamespaceReleaseModel model){ @PathVariable String env, @PathVariable String clusterName,
if (model == null){ @PathVariable String namespaceName, @RequestBody NamespaceReleaseModel model) {
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.setAppId(appId);
model.setClusterName(clusterName); model.setClusterName(clusterName);
model.setEnv(env); model.setEnv(env);
model.setNamespaceName(namespaceName); model.setNamespaceName(namespaceName);
if (model.isInvalid()){ if (model.isInvalid()) {
return ResponseEntity.badRequest().body(new SimpleMsg("form data exception.")); throw new BadRequestException("request model is invalid");
} }
ReleaseDTO release = configService.release(model); ReleaseDTO release = configService.release(model);
if (release == null){ if (release == null) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new SimpleMsg("oops! some error in server.")); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
}else { .body(new SimpleMsg("oops! some error in server."));
} else {
return ResponseEntity.ok().body(new SimpleMsg("success")); return ResponseEntity.ok().body(new SimpleMsg("success"));
} }
} }
......
...@@ -7,8 +7,8 @@ import org.springframework.web.HttpMediaTypeException; ...@@ -7,8 +7,8 @@ import org.springframework.web.HttpMediaTypeException;
import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; 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 com.ctrip.apollo.core.exception.NotFoundException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
...@@ -58,8 +58,13 @@ public class GlobalDefaultExceptionHandler { ...@@ -58,8 +58,13 @@ public class GlobalDefaultExceptionHandler {
} }
@ExceptionHandler(NotFoundException.class) @ExceptionHandler(NotFoundException.class)
@ResponseStatus(value = NOT_FOUND) public ResponseEntity<Map<String, Object>> notFound(HttpServletRequest request, NotFoundException ex) {
public void notFound(HttpServletRequest req, 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) { private Throwable resolveError(Throwable ex) {
......
...@@ -10,12 +10,9 @@ import org.springframework.stereotype.Service; ...@@ -10,12 +10,9 @@ import org.springframework.stereotype.Service;
import com.ctrip.apollo.Apollo.Env; import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.dto.AppDTO; 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.PortalSettings;
import com.ctrip.apollo.portal.api.AdminServiceAPI; import com.ctrip.apollo.portal.api.AdminServiceAPI;
import com.ctrip.apollo.portal.entity.ClusterNavTree; import com.ctrip.apollo.portal.entity.ClusterNavTree;
import com.ctrip.apollo.portal.entity.SimpleMsg;
@Service @Service
public class AppService { public class AppService {
...@@ -52,7 +49,7 @@ public class AppService { ...@@ -52,7 +49,7 @@ public class AppService {
return appAPI.save(Env.LOCAL, app); return appAPI.save(Env.LOCAL, app);
} catch (Exception e) { } catch (Exception e) {
logger.error("oops! save app error. app id:{}", app.getAppId(), e); logger.error("oops! save app error. app id:{}", app.getAppId(), e);
return null; throw e;
} }
} }
......
...@@ -7,7 +7,6 @@ import com.ctrip.apollo.portal.api.AdminServiceAPI; ...@@ -7,7 +7,6 @@ import com.ctrip.apollo.portal.api.AdminServiceAPI;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List; import java.util.List;
@Service @Service
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册