FdkkMiniReqErrorCallback.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package com.fdkankan.scene.callback;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.dtflys.forest.callback.OnError;
  5. import com.dtflys.forest.exceptions.ForestRuntimeException;
  6. import com.dtflys.forest.http.ForestRequest;
  7. import com.dtflys.forest.http.ForestResponse;
  8. import com.fdkankan.common.constant.ServerCode;
  9. import com.fdkankan.common.exception.BusinessException;
  10. import com.fdkankan.common.response.Result;
  11. import lombok.extern.slf4j.Slf4j;
  12. /**
  13. * <p>
  14. * TODO
  15. * </p>
  16. *
  17. * @author dengsixing
  18. * @since 2022/4/25
  19. **/
  20. @Slf4j
  21. public class FdkkMiniReqErrorCallback implements OnError {
  22. @Override
  23. public void onError(ForestRuntimeException e, ForestRequest forestRequest,
  24. ForestResponse forestResponse) {
  25. JSONObject jsonObject = JSON.parseObject(forestResponse.getContent());
  26. Integer status = jsonObject.getInteger("status");
  27. if(status != null && status == 500){
  28. log.error("v3接口报错,status:{},error:{}", status, jsonObject.getString("error"));
  29. throw new BusinessException(ServerCode.SYSTEM_ERROR);
  30. }
  31. Result result = JSON.parseObject(forestResponse.getContent(), Result.class);
  32. if(result.getCode() != ServerCode.SUCCESS.code()){
  33. throw new BusinessException(result.getCode(), result.getMsg());
  34. }
  35. log.error("跨服务请求失败!", e);
  36. throw new BusinessException(ServerCode.FEIGN_REQUEST_FAILD);
  37. }
  38. }