fix(service): 修正物料分类接口和添加统一HTTP异常处理

- 修改物料分类API请求路径,去除多余/api前缀
- 在全局异常处理类中新增对HttpClientErrorException的捕获处理
- 记录HTTP请求错误的详细日志,返回统一错误状态和信息
- 在状态常量中新增HTTP错误状态码和描述
This commit is contained in:
曹鹏飞 2026-05-13 08:51:57 +08:00
parent c2518c6fb7
commit cf746ceb00
3 changed files with 12 additions and 3 deletions

View File

@ -27,7 +27,8 @@ public enum STATE {
InconsistentDataError(117, "需要用户确认"),
NoOrderData(118, "订单不存在"),
SAPErr(119, "SAP错误"),
OutOfStock(120, "库存不足");
OutOfStock(120, "库存不足"),
HttpError(121, "HTTP错误");
@Getter
private final Integer state;

View File

@ -20,6 +20,7 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import java.util.ArrayList;
@ -86,10 +87,17 @@ public class GlobalRestControllerAdvice {
@ExceptionHandler(DataAlertException.class)
public ApiResult<Void> handleDataAlertException(DataAlertException ex) {
printLog("返回数据错误", ex);
printLog("返回数据错误", ex);
return ApiResult.errorWithExtras(ex.getState(), ex.getObj());
}
@ExceptionHandler(HttpClientErrorException.class)
public ApiResult<Void> handleHttpClientErrorException(HttpClientErrorException ex) {
Throwable rootCause = ex.getRootCause() != null ? ex.getRootCause() : ex;
printLog("HTTP请求错误", rootCause);
return ApiResult.error(STATE.HttpError, rootCause.getMessage());
}
private void printLog(String msg, Throwable ex) {
log.error("{}:{},{}", msg, ex.getMessage(), ExceptionUtil.stacktraceToString(ex));
}

View File

@ -68,7 +68,7 @@ public class BomMaterialService {
headers.add("authorization", getToken());
HttpEntity<MaterialCategoryQO> requestEntity = new HttpEntity<>(qo, headers);
ResponseEntity<BomResultDTO<List<MaterialCategoryVO>>> response = restTemplate.exchange(
baseUrl + "/api/material/category/getAlllist",
baseUrl + "/material/category/getAlllist",
HttpMethod.POST,
requestEntity,
new ParameterizedTypeReference<>() {