@ControllerAdvice
public
class
ExceptionHandle {
private
final
static
Logger logger = LoggerFactory.getLogger(ExceptionHandle.
class
);
@ExceptionHandler
(value = Exception.
class
)
@ResponseBody
public
Result handle(Exception e){
if
(e
instanceof
CourseException){
CourseException courseException = (CourseException) e;
return
ResultUtil.error(courseException.getCode(),courseException.getMessage());
}
logger.error(
"系统异常={}"
,e);
return
ResultUtil.error(
0
,e.getMessage());
}
}
|
public
class
CourseException
extends
RuntimeException {
private
Integer code;
public
CourseException(ResultEnum resultEnum){
super
(resultEnum.getMsg());
this
.code = resultEnum.getCode();
}
public
Integer getCode() {
return
code;
}
public
void
setCode(Integer code) {
this
.code = code;
}
}
|