HandleException.java
1.27 KB
package com.trash.garbage.handle;
import com.trash.garbage.custom.BizException;
import com.trash.garbage.global.Result;
//import org.springframework.core.annotation.Order;
import com.trash.garbage.global.ResultCode;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.sql.SQLIntegrityConstraintViolationException;
/**
* @author 20412
*/
@RestControllerAdvice(basePackages = {"com.trash.garbage.controller"})
// 多个RestControllerAdvice注解是需要注意是否有兜底得处理方法 RestControllerAdvice注解有优先级
@Order(Ordered.HIGHEST_PRECEDENCE)
public class HandleException {
@ExceptionHandler(BizException.class)
public Result<?> handleBizException(BizException e) {
return Result.ERROR(e.getCode(), e.getMsg());
}
@ExceptionHandler(SQLIntegrityConstraintViolationException.class)
public Result<?> handleSqlDuplicate(SQLIntegrityConstraintViolationException e) {
String message = e.getMessage();
String str = message.split(" ")[2];
return Result.ERROR(ResultCode.CODE_500, "已经下发给:" + str.substring(str.lastIndexOf("-")));
}
}