This commit is contained in:
2025-11-12 10:23:18 +08:00
parent 36c80bd718
commit 3bae9504a9
4 changed files with 27 additions and 23 deletions

View File

@@ -30,25 +30,6 @@ import java.util.stream.Collectors;
@RestControllerAdvice
@Slf4j
public class CzgControllerAdvice {
@ResponseBody
@ExceptionHandler(value = Exception.class)
@ResponseStatus(HttpStatus.OK)
public CzgResult<Object> errorHandler(Exception ex) {
Throwable rootCause = ex;
while (rootCause.getCause() != null) {
rootCause = rootCause.getCause();
if (rootCause instanceof CzgException) {
break;
}
}
// 处理自定义异常
if (rootCause instanceof CzgException czgException) {
return CzgResult.failure(czgException.getCode(), czgException.getMessage());
}
setErrorLog(ex);
return CzgResult.failure(CzgRespCode.SYSTEM_ERROR.getCode(), ex.getMessage());
}
@ResponseBody
@ExceptionHandler(value = NoResourceFoundException.class)
@ResponseStatus(HttpStatus.OK)
@@ -113,9 +94,9 @@ public class CzgControllerAdvice {
return CzgResult.failure(ex.getCode(), ex.getMsg());
}
@ResponseBody
// @ResponseBody
@ExceptionHandler(OrderValidateException.class)
@ResponseStatus(HttpStatus.OK)
// @ResponseStatus(HttpStatus.OK)
public CzgResult<Object> handleOrderValidateException(OrderValidateException ex) {
log.error("订单校验异常:{}", ex.getMessage());
return CzgResult.failure(ex.getCode(), ex.getMsg());
@@ -161,6 +142,25 @@ public class CzgControllerAdvice {
return CzgResult.failure(CzgRespCode.RECORD_EXISTED);
}
@ResponseBody
@ExceptionHandler(value = Exception.class)
@ResponseStatus(HttpStatus.OK)
public CzgResult<Object> errorHandler(Exception ex) {
Throwable rootCause = ex;
while (rootCause.getCause() != null) {
rootCause = rootCause.getCause();
if (rootCause instanceof CzgException) {
break;
}
}
// 处理自定义异常
if (rootCause instanceof CzgException czgException) {
return CzgResult.failure(czgException.getCode(), czgException.getMessage());
}
setErrorLog(ex);
return CzgResult.failure(CzgRespCode.SYSTEM_ERROR.getCode(), ex.getMessage());
}
private void setErrorLog(Exception ex) {
log.error(ex.getMessage());
log.error("错误", ex);