Commit c445e626 by huangfusuper

【全局异常处理】增加SpringMvc全局异常处理

parent 6396ec6f
package com.byit.advice;
import com.byit.job.enums.IEnum;
import com.byit.job.exceptions.IException;
import com.byit.job.vo.ResponseResult;
import com.byit.util.ResponseResultUtil;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @program: byit-myth-job->ExceptionHandle
* @description: 全局异常处理
* @author: huangfu
* @date: 2019/12/25 16:02
**/
@RestControllerAdvice
public class ExceptionHandle {
/**
* 全局异常捕捉
* @param e 异常
* @return return
*/
@ExceptionHandler(value = {Exception.class})
public ResponseResult requestException(Exception e){
e.printStackTrace();
if(e instanceof IException){
return error(e);
}
return ResponseResultUtil.error("未知错误");
}
/**
* 处理全局异常处理,将错误枚举传入当前封装结果集
* @param e 异常
* @return return
*/
private ResponseResult error(Exception e){
IException iException = (IException)e;
IEnum iEnum = iException.getIEnum();
return ResponseResultUtil.error(iEnum);
}
}
package com.byit.enums;
import com.byit.job.enums.IEnum;
import lombok.AllArgsConstructor;
/**
* @program: byit-myth-job->AdminEnums
* @description: 调度中心的异常枚举
* @author: huangfu
* @date: 2019/12/25 15:57
**/
@AllArgsConstructor
public enum AdminEnums implements IEnum {
;
String code;
String msg;
@Override
public String getCode() {
return this.code;
}
@Override
public String getMsg() {
return this.msg;
}
}
package com.byit.util;
import com.byit.job.enums.IEnum;
import com.byit.job.vo.ResponseResult;
/**
* @author huangfu
*/
public class ResponseResultUtil<T> {
/**
* 成功
* @param t
* @param <T>
* @return
*/
public static<T> ResponseResult<T> ok(T t){
ResponseResult<T> tResponseResult = new ResponseResult<>();
tResponseResult.setCode("0000000");
tResponseResult.setMsg("成功");
tResponseResult.setResult(t);
return tResponseResult;
}
/**
* 错误
* @param iEnum
* @param <T>
* @return
*/
public static<T> ResponseResult<T> error(IEnum iEnum){
ResponseResult<T> tResponseResult = new ResponseResult<>();
tResponseResult.setCode(iEnum.getCode());
tResponseResult.setMsg(iEnum.getMsg());
return tResponseResult;
}
/**
* 错误
* @param msg
* @param <T>
* @return
*/
public static<T> ResponseResult<T> error(String msg){
ResponseResult<T> tResponseResult = new ResponseResult<>();
tResponseResult.setMsg(msg);
return tResponseResult;
}
}
package com.byit.job.exceptions;
import com.byit.job.enums.IEnum;
/**
* @program: byit-myth-job->BusinessException
* @description: 全局异常的封装
* @author: huangfu
* @date: 2019/12/25 15:52
**/
public class BusinessException extends RuntimeException implements IException{
/**
*
*/
private static final long serialVersionUID = 1461967305270457527L;
private IEnum e;
public BusinessException() {
}
public <E extends IEnum> BusinessException(E e) {
super(e.getMsg());
this.e = e;
}
@Override
public IEnum getIEnum() {
return this.e;
}
public BusinessException(String message) {
super(message);
}
public BusinessException(String message, Throwable cause) {
super(message, cause);
}
public BusinessException(Throwable cause) {
super(cause);
}
}
package com.byit.job.vo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 全局结果集封装
* @author huangfu
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ResponseResult<T> {
private String code;
private T result;
private String msg;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment