java自定义异常
1:业务异常类信息
package com.protal.exception;
/**
* @Description 业务异常类信息
* @Author pei
* @Date 2020-7-13 15:42
*/
public class BaseException extends RuntimeException {
/**
* The Failure cause.
*/
private IFailureCause failureCause;
/**
* Instantiates a new Gp runtime exception.
*
* @param failureCause the failure cause
*/
public BaseException(IFailureCause failureCause) {
this.failureCause = failureCause;
}
/**
* Instantiates a new Gp runtime exception.
*
* @param failureCause the failure cause
* @param cause the cause
*/
public BaseException(IFailureCause failureCause, Throwable cause) {
super(cause);
this.failureCause = failureCause;
}
/**
* Instantiates a new Gp runtime exception.
*
* @param failureCause the failure cause
* @param message the message
*/
public BaseException(IFailureCause failureCause, String message) {
super(message);
this.failureCause = failureCause;
}
/**
* Instantiates a new Gp runtime exception.
*
* @param failureCause the failure cause
* @param message the message
* @param args the args
*/
public BaseException(IFailureCause failureCause, String message, Object... args) {
super(rebuildMessage(message, args));
this.failureCause = failureCause;
}
/**
* Instantiates a new Gp runtime exception.
*
* @param failureCause the failure cause
* @param cause the cause
* @param message the message
*/
public BaseException(IFailureCause failureCause, Throwable cause, String message) {
super(message, cause);
this.failureCause = failureCause;
}
/**
* Instantiates a new Gp runtime exception.
*
* @param failureCause the failure cause
* @param cause the cause
* @param message the message
* @param args the args
*/
public BaseException(IFailureCause failureCause, Throwable cause, String message,
Object... args) {
super(rebuildMessage(message, args), cause);
this.failureCause = failureCause;
}
/**
* Gets failure cause.
*
* @return the failure cause
*/
public IFailureCause getFailureCause() {
return failureCause;
}
/**
* Sets failure cause.
*
* @param failureCause the failure cause
*/
public void setFailureCause(IFailureCause failureCause) {
this.failureCause = failureCause;
}
/**
* To string string.
*
* @return the string
*/
@Override
public String toString() {
return super.toString() + "\n" + "[IFailureCause=" + failureCause + "]\n";
}
/**
* 重新构建message
*
* @param source message
* @param args 参数
* @return string
*/
private static String rebuildMessage(String source, Object... args) {
if (null != args && args.length > 0) {
for (Object arg : args) {
int index = source.indexOf("{}");
if (index >= 0) {
source = source.substring(0, index) + arg.toString() +
source.substring(index + 2, source.length());
} else {
break;
}
}
return source;
} else {
return source;
}
}
}
错误码接口
package com.protal.exception;
/**
* @Description 错误码接口
* @Author pei
* @Date 2020-7-13 15:42
*/
public interface IFailureCause {
/**
* @describe: 获取错误码
* @param: 无
* @return: 错误码
*/
int getResultCode();
/**
* @describe: 获取异常信息
* @param: void
* @return: 错误信息
*/
String getResultMsg();
}
3.项目异常
package com.protal.exception;
/**
* @Description TODO
* @Author pei
* @Date 2020-7-13 15:47
*/
public class PortalException extends BaseException {
private IFailureCause failureCause;
/**
* Instantiates a new Gp sysmessage exception.
*
* @param failureCause the failure cause
*/
public PortalException(
IFailureCause failureCause) {
super(failureCause);
}
/**
* Instantiates a new Gp sysmessage exception.
*
* @param failureCause the failure cause
* @param cause the cause
*/
public PortalException(
IFailureCause failureCause, Throwable cause) {
super(failureCause, cause);
}
/**
* Instantiates a new Gp sysmessage exception.
*
* @param failureCause the failure cause
* @param message the sysmessage
*/
public PortalException(
IFailureCause failureCause, String message) {
super(failureCause, message);
}
/**
* Instantiates a new Gp sysmessage exception.
*
* @param failureCause the failure cause
* @param message the sysmessage
* @param args the args
*/
public PortalException(
IFailureCause failureCause, String message, Object... args) {
super(failureCause, message, args);
}
/**
* Instantiates a new Gp sysmessage exception.
*
* @param failureCause the failure cause
* @param cause the cause
* @param message the sysmessage
*/
public PortalException(
IFailureCause failureCause, Throwable cause, String message) {
super(failureCause, cause, message);
}
/**
* Instantiates a new Gp sysmessage exception.
*
* @param failureCause the failure cause
* @param cause the cause
* @param message the sysmessage
* @param args the args
*/
public PortalException(
IFailureCause failureCause, Throwable cause, String message, Object... args) {
super(failureCause, cause, message, args);
}
public IFailureCause getErrorCode() {
return failureCause;
}
}
4.枚举
package com.protal.exception;
/**
* @Description TODO
* @Author pei
* @Date 2020-7-13 15:42
*/
public enum PortalExceptionCause implements IFailureCause {
/**
* 消息发送类型为空
*/
MESSAGE_SEND_TYPE_EMPTY(4000,"参数为空"),;
/**
* 错误异常码
*/
private final int resultCode;
/**
* 错误异常说明
*/
private final String resultMsg;
PortalExceptionCause(int resultCode, String resultMsg) {
this.resultCode = resultCode;
this.resultMsg = resultMsg;
}
@Override
public int getResultCode() {
return resultCode;
}
@Override
public String getResultMsg() {
return resultMsg;
}
}
5.全局抓取异常
package com.protal.exception;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.converter.HttpMessageNotReadableException;
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.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.nio.file.AccessDeniedException;
/**
* @Description 全局异常抓取,返回json给前端
* @Author pei
* @Date 2020-7-13 14:41
*/
@ControllerAdvice
public class ControllerAdviceProcessor {
@Autowired
protected MessageSource messageSource;
@ExceptionHandler(Exception.class)
@ResponseBody
public GenericResponse> handleException(HttpServletRequest request, Exception ex) {
String code = "500";
if (ex instanceof HttpMessageNotReadableException) {
code = "400";
} else if (ex instanceof HttpRequestMethodNotSupportedException) {
code = "999";
}
String msg = null;
if (ex instanceof PortalException) {
PortalException GpException = (PortalException) ex;
msg = GpException.getMessage();
code = String.valueOf(GpException.getFailureCause().getResultCode());
} else if (ex instanceof AccessDeniedException) {
msg = "无权限访问";
code = "403";
}
if (msg == null) {
msg = ex.getMessage();
}
GenericResponse> resp = new GenericResponse<>();
resp.setCode(code);
resp.setMessage(msg);
return resp;
}
}
6.返回前端异常信息
package com.protal.exception;
/**
* @Description TODO
* @Author pei
* @Date 2020-7-13 14:43
*/
public class GenericResponse {
/**
* @describe: 状态码
*/
private String code;
/**
* @describe: 信息
*/
private String message;
/**
* @describe: 泛型
*/
private T data;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
7.测试类
package com.protal.information.controller;
import com.protal.exception.PortalException;
import com.protal.exception.PortalExceptionCause;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description 测试类
* @Author pei
* @Date 2020-7-13 15:42
*/
@RestController
@RequestMapping(value = "test")
@Slf4j
public class TestController {
@GetMapping("test")
public int test() {
int a = 0;
int b = 0;
if (true) {
try{
int c = a/b;
}catch (Exception e){
throw new PortalException(PortalExceptionCause.MESSAGE_SEND_TYPE_EMPTY, "系统出错,请联系管理员");
}
}
return 33333;
}
}