什么是通用异常:
在项目代码中如果没有查询导数据就返回null,这种操作并不好或者 如果发生系统异常,则直接会给用户抛出不友好的异常信息。为了增加用户的体验,应该给一些适当信息进行提示,那该如何处理呢?
项目开发中肯定会设置全局异常处理,不管系统发生了任何不可知的异常信息,都应该给用户返回友好提示信息。
异常枚举类:
可根据自己的项目需求修改:
@Getter
public enum AppHttpCodeEnum {
// 成功段0
SUCCESS(0,"操作成功"),
// 登录段1~50
NEED_LOGIN(1,"需要登录后操作"),
LOGIN_PASSWORD_ERROR(2,"密码错误"),
// TOKEN50~100
TOKEN_INVALID(50,"无效的TOKEN"),
TOKEN_EXPIRE(51,"TOKEN已过期"),
TOKEN_REQUIRE(52,"TOKEN是必须的"),
// SIGN验签 100~120
SIGN_INVALID(100,"无效的SIGN"),
SIG_TIMEOUT(101,"SIGN已过期"),
// 参数错误 500~1000
PARAM_REQUIRE(500,"缺少参数"),
PARAM_INVALID(501,"无效参数"),
PARAM_IMAGE_FORMAT_ERROR(502,"图片格式有误"),
SERVER_ERROR(503,"服务器内部错误"),
UPDATE_ERROR(504,"数据更新异常"),
TEXT_ILLEGAL(505,"文本内容不符合规范" ),
UUID_ILLEGAL(506,"UUID不能为空" ),
// 数据错误 1000~2000
DATA_EXIST(1000,"数据已经存在"),
USER_DATA_NOT_EXIST(1001,"用户数据不存在"),
DATA_NOT_EXIST(1002,"数据不存在"),
TOKEN_NOT_EXIST(1002,"获取用户身份信息失败,请重新登录"),
EMAIL_CODE_EXIST(1003,"验证码已经发送,请勿重复操作"),
// 数据错误 3000~3500
NO_OPERATOR_AUTH(3000,"无权限操作");
int code;
String msg;
AppHttpCodeEnum(int code, String msg){
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
自定义异常
创建NsException作为项目自己的异常
public class NsException extends RuntimeException {
private static final long serialVersionUID = 1L;
private AppHttpCodeEnum enums;
public AppHttpCodeEnum getEnums() {
return enums;
}
public void setEnums(AppHttpCodeEnum enums) {
this.enums = enums;
}
private String msg;
private int code = 500;
public NsException(String msg) {
super(msg);
this.msg = msg;
}
public NsException(String msg, Throwable e) {
super(msg, e);
this.msg = msg;
}
public NsException(String msg, int code) {
super(msg);
this.msg = msg;
this.code = code;
}
public NsException(String msg, int code, Throwable e) {
super(msg, e);
this.msg = msg;
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public NsException(AppHttpCodeEnum enums, String message) {
super(message);
this.enums = enums;
}
public NsException(AppHttpCodeEnum enums) {
this.enums = enums;
}
}
通用异常配置:
新建类ExceptionCatch:
@RestControllerAdvice
@Slf4j
public class ExceptionCatch {
@ExceptionHandler(Exception.class) // 捕获异常类型
public ResponseJson exception(Exception e) {
// 记录日志
log.error(e.getMessage());
// 返回通用异常信息
return ResponseJson.error(AppHttpCodeEnum.SERVER_ERROR);
}
/**
* 自定义异常处理
* @param nsException
* @return
*/
@ExceptionHandler(NsException.class)
public ResponseJson leadExceptionHandler(NsException nsException) {
nsException.printStackTrace();
if(StringUtils.isNotBlank(nsException.getMessage())){
log.error("catch exception:{}", nsException.getMessage());
return ResponseJson.error(nsException.getCode(),nsException.getMessage());
}else{
log.error("catch exception:{}", nsException.getEnums().getMsg());
return ResponseJson.error(nsException.getEnums());
}
}
}
集成到项目中使用:
在需要用到的项目添加包扫描:
/**
* 初始化配置
*/
@Configuration
@ComponentScan({"com.xxx.common.exception"})
public class InitConfig {
}
自己用到的统一返回:
public class ResponseJson extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
public ResponseJson() {
put("code", 200);
put("data", "");
put("msg", "success");
}
public static ResponseJson error() {
return error(500, "未知异常,请联系管理员");
}
public static ResponseJson error(String msg) {
return error(1000, msg);
}
public static ResponseJson error(int code, String msg) {
ResponseJson r = new ResponseJson();
r.put("code", code);
r.put("msg", msg);
return r;
}
public static ResponseJson error(AppHttpCodeEnum codeEnum) {
ResponseJson r = new ResponseJson();
r.put("code", codeEnum.getCode());
r.put("msg", codeEnum.getMsg());
return r;
}
public static ResponseJson success() {
return new ResponseJson();
}
public static ResponseJson success(Object data) {
ResponseJson r = new ResponseJson();
r.put("data", data);
return r;
}
public static ResponseJson success(Object data, String msg) {
ResponseJson r = new ResponseJson();
r.put("data", data);
r.put("msg", msg);
return r;
}
public ResponseJson output(String key, Object value) {
super.put(key, value);
return this;
}
}
设置完成返回的示例:
可以给出更具体的提示