Skip to content

Commit

Permalink
common模块:完善异常、响应码
Browse files Browse the repository at this point in the history
  • Loading branch information
guolanren committed Apr 11, 2020
1 parent b53d5bb commit d60f2ca
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

Expand All @@ -13,6 +14,7 @@
*/
@Configuration
@EnableConfigurationProperties(BootStarterProperties.class)
@ComponentScan("name.guolanren")
@Import({})
public class BootStarterAutoConfiguration {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package name.guolanren.exception;

/**
* @author guolanren
*/
public class BusinessException extends RuntimeException {

public BusinessException() {
super();
}

public BusinessException(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

public BusinessException(String message, Throwable cause) {
super(message, cause);
}

public BusinessException(String message) {
super(message);
}

public BusinessException(Throwable cause) {
super(cause);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package name.guolanren.exception;

import name.guolanren.http.ResultCode;
import name.guolanren.http.ResultEntity;
import name.guolanren.http.response.ResultCode;
import name.guolanren.http.response.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
Expand All @@ -18,44 +18,33 @@ public class GlobalExceptionHandler {
private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);

@ExceptionHandler
public ResultEntity requestHandle(RequestException e) {
public ResultEntity illegalParamHandle(IllegalParamException e) {
LOG.error(e.getMessage(), e);
return ResultEntity.faild(ResultCode.REQUEST_FAILD, e.getMessage());
}
@ExceptionHandler
public ResultEntity InvalidParamHandle(InvalidParamException e) {
LOG.error(e.getMessage(), e);
return ResultEntity.faild(ResultCode.INVALID_PARAM_FAILD, e.getMessage());
}

@ExceptionHandler
public ResultEntity IllegalParamHandle(IllegalParamException e) {
LOG.error(e.getMessage(), e);
return ResultEntity.faild(ResultCode.REQUEST_FAILD, e.getMessage());
return ResultEntity.failed(ResultCode.ILLEGAL_PARAM_FAILED, e.getMessage());
}

@ExceptionHandler
public ResultEntity serverErrorHandle(ServerErrorException e) {
LOG.error(e.getMessage(), e);
return ResultEntity.faild(ResultCode.SERVER_ERROR, e.getMessage());
return ResultEntity.failed(ResultCode.SERVER_ERROR, e.getMessage());
}

@ExceptionHandler
public ResultEntity expiredSessionHandle(ExpiredSessionException e) {
public ResultEntity expiredSessionHandle(SessionInvalidException e) {
LOG.error(e.getMessage(), e);
return ResultEntity.faild(ResultCode.EXPIRED_SESSION, e.getMessage());
return ResultEntity.failed(ResultCode.EXPIRED_SESSION, e.getMessage());
}

@ExceptionHandler
public ResultEntity unauthorizedHandle(UnauthorizedException e) {
LOG.error(e.getMessage(), e);
return ResultEntity.faild(ResultCode.UNAUTHORIZED, e.getMessage());
return ResultEntity.failed(ResultCode.UNAUTHORIZED, e.getMessage());
}

@ExceptionHandler
public ResultEntity unknownHandle(Exception e) {
LOG.error(e.getMessage(), e);
return ResultEntity.faild(ResultCode.UNKNOWN_FAILED, e.getMessage());
return ResultEntity.failed(ResultCode.UNKNOWN_FAILED, e.getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/
public class IllegalParamException extends RuntimeException {

private static final long serialVersionUID = 975102355123055799L;

public IllegalParamException() {
super();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package name.guolanren.exception;

/**
* @author guolanren
*/
public class SessionInvalidException extends RuntimeException {

public SessionInvalidException() {
super();
}

public SessionInvalidException(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

public SessionInvalidException(String message, Throwable cause) {
super(message, cause);
}

public SessionInvalidException(String message) {
super(message);
}

public SessionInvalidException(Throwable cause) {
super(cause);
}
}
58 changes: 0 additions & 58 deletions common/src/main/java/name/guolanren/http/ResultEntity.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,49 +1,54 @@
package name.guolanren.http;
package name.guolanren.http.response;

/**
* @author guolanren
*/
public enum ResultCode {

/**
* 响应成功
* 请求成功
*/
REQUEST_SUCCESS(100, "响应成功"),
REQUEST_SUCCESS(1000, "请求成功"),

/**
* 请求失败
* 非法参数
*/
REQUEST_FAILED(200, "请求失败"),
ILLEGAL_PARAM_FAILED(2001, "非法参数"),

/**
* 无效请求参数
* 服务端错误
*/
INVALID_PARAM_FAILED(201, "无效参数"),
SERVER_ERROR(3000, "服务器错误"),

/**
* 非法请求参数
* 无效会话
*/
ILLEGAL_PARAM_FAILED(202, "非法参数"),
EXPIRED_SESSION(4001, "无效失效"),

/**
* 服务器错误
* 未授权
*/
SERVER_ERROR(301, "服务器错误"),
UNAUTHORIZED(4002, "未授权"),

/**
* Session失效
* 重定向
*/
EXPIRED_SESSION(401, "SESSION失效"),
REDIRECT(5001, "重定向"),

/**
* 未授权
* 转发
*/
FORWARD(5002, "转发"),

/**
* 业务错误
*/
UNAUTHORIZED(402, "未授权"),
BUSINESS_FAIL(10000, "业务异常"),

/**
* 未知异常
*/
UNKNOWN_FAILED(900, "未知异常");
UNKNOWN_FAILED(-1, "未知异常");

private Integer code;
private String description;
Expand Down
Loading

0 comments on commit d60f2ca

Please sign in to comment.