1、在controller、service、dao中程序员抛出自定义异常;springMVC框架抛出框架异常类型;
2、统一由异常捕获类捕获异常,并进行处理;
3、捕获到自定义异常则直接取出错误代码及错误信息,响应给用户;
4、捕获到非自定义异常类型首先从Map中找该异常类型是否对应具体的错误代码,如果有则取出错误代码和错误信息并响应给用户,如果从Map中找不到异常类型所对应的错误代码则统一为99999错误代码并响应给用户。
5、将错误代码及错误信息以Json格式响应给用户。
1. 可预知异常处理
1.1 自定义异常类
在common工程定义异常类型。
package com.guohao.framework.exception;
import com.guohao.framework.model.response.ResultCode;
public class CustomException extends RuntimeException {
private ResultCode resultCode;
public CustomException(ResultCode resultCode) {
//异常信息为错误代码+异常信息
super("错误代码:"+resultCode.code()+"错误信息:"+resultCode.message());
this.resultCode = resultCode;
}
public ResultCode getResultCode(){
return this.resultCode;
}
}
1.2 异常抛出类
package com.xuecheng.framework.exception;
import com.xuecheng.framework.model.response.ResultCode;
public class ExceptionCast {
//使用此静态方法抛出自定义异常
public static void cast(ResultCode resultCode){
throw new CustomException(resultCode);
}
}
1.3 异常捕获类
使用 @ControllerAdvice和@ExceptionHandler注解来捕获指定类型的异常
package com.xuecheng.framework.exception;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.framework.model.response.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class ExceptionCatch {
private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);
//捕获 CustomException异常
@ExceptionHandler(CustomException.class)
@ResponseBody
public ResponseResult customException(CustomException e) {
LOGGER.error("catch exception : {}\r\nexception: ",e.getMessage(), e);
ResultCode resultCode = e.getResultCode();
ResponseResult responseResult = new ResponseResult(resultCode);
return responseResult;
}
}
1.4 定义错误代码
根据业务需求,进行编写错误代码枚举类,定义错误代码
package com.xuecheng.framework.domain.cms.response;
import com.xuecheng.framework.model.response.ResultCode;
import lombok.ToString;
@ToString
public enum CmsCode implements ResultCode {
CMS_ADDPAGE_EXISTS(false,24001,"页面已存在!");
//操作结果
boolean success;
//操作代码
int code;
//提示信息
String message;
private CmsCode(boolean success, int code, String message){
this.success = success;
this.code = code;
this.message = message;
}
@Override
public boolean success() {
return success;
}
@Override
public int code() {
return code;
}
@Override
public String message() {
return message;
}
}
2. 不可预知异常处理
针对上边的问题其解决方案是:
1、我们在map中配置HttpMessageNotReadableException和错误代码。
2、在异常捕获类中对Exception异常进行捕获,并从map中获取异常类型对应的错误代码,如果存在错误代码则返回此错误,否则统一返回99999错误。
具体的开发实现如下:
1、在通用错误代码类CommCode中配置非法参数异常
INVALID_PARAM(false,10003,"非法参数!"),
2、在异常捕获类中配置 HttpMessageNotReadableException 为非法参数异常。
异常捕获类代码如下:
package com.xuecheng.framework.exception;
import com.google.common.collect.ImmutableMap;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.framework.model.response.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @ClassName: ExceptionCatch
* @description: //TODO 捕获springMVC抛出的异常
* @author: edison_Kwok
* @Date: create in 2019/4/15 9:25
* @Version: 1.0
*/
@ControllerAdvice
public class ExceptionCatch {
//日志记录工具
public static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);
//使用EXCEPTIONS存放异常类型和错误代码的映射,ImmutableMap的特点的一旦创建不可改变,并且线程安全
private static ImmutableMap<Class<? extends Throwable>,ResultCode> EXCEPTIONS;
//使用builder来构建一个异常类型和错误代码的异常
protected static ImmutableMap.Builder<Class<? extends Throwable>,ResultCode> builder = ImmutableMap.builder();
static{
//在这里加入一些基础的异常类型判断
builder.put(HttpMessageNotReadableException.class, CommonCode.INVALIDPARAM);
}
/**
* @Author edison_Kwok
* @Description //TODO 捕获springMVC抛出的本地可预测异常,并向客户端返回自定义错误信息
* @Date 2019/4/15
* @Param []
* @Return com.xuecheng.framework.model.response.ResponseResult
**/
@ExceptionHandler(CustomException.class)
@ResponseBody
public ResponseResult customException(CustomException e) {
//记录日志
LOGGER.error("CatchException : {}\r\nexception:", e.getMessage(), e);
ResultCode resultCode = e.getResultCode();
return new ResponseResult(resultCode);
}
/**
* @Author edison_Kwok
* @Description //TODO 捕获不可预测异常,并记录日志
* @Date 2019/4/15
* @Param [exception]
* @Return com.xuecheng.framework.model.response.ResponseResult
**/
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseResult exception(Exception e){
//记录日志
LOGGER.error("catch exception:{}\r\nexception:",e.getMessage(),e);
if(EXCEPTIONS == null){
EXCEPTIONS = builder.build();
}
final ResultCode resultCode = EXCEPTIONS.get(e.getClass());
final ResponseResult responseResult;
if (resultCode != null) {
responseResult = new ResponseResult(resultCode);
} else {
responseResult = new ResponseResult(CommonCode.SERVER_ERROR);
}
return responseResult;
}
}