Skip to content

Commit

Permalink
Refactor Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
yiming187 committed Apr 15, 2016
1 parent d93d3cc commit 5f99ea3
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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());
}



}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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;
Expand Down Expand Up @@ -31,29 +32,30 @@ 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");
}

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);
Expand All @@ -65,27 +67,29 @@ public ResponseEntity<SimpleMsg> modifyItems(@PathVariable String appId, @PathVa
}
}

@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"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
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;
Expand Down Expand Up @@ -58,8 +58,13 @@ public ResponseEntity<Map<String, Object>> methodNotSupportedException(HttpServl
}

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@

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 {
Expand Down Expand Up @@ -52,7 +49,7 @@ public AppDTO save(AppDTO app) {
return appAPI.save(Env.LOCAL, app);
} catch (Exception e) {
logger.error("oops! save app error. app id:{}", app.getAppId(), e);
return null;
throw e;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
Expand Down

0 comments on commit 5f99ea3

Please sign in to comment.