添加全局异常处理

20230922-ljl-fixBug
qiushengjie 1 year ago
parent edcd5c17d5
commit 22f78366d2

@ -1,49 +1,31 @@
package com.ms.api;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinritemai.cloud.base.api.BaseRequest;
import com.jinritemai.cloud.base.api.BaseResponse;
import com.jinritemai.cloud.base.api.ExtensionService;
import com.jinritemai.cloud.base.api.ExtensionServiceHandler;
import com.ms.api.common.E;
import com.ms.api.common.R;
import com.ms.api.common.Ret;
import com.ms.api.common.SPIBaseService;
import com.ms.api.dto.ItemDTO;
import com.ms.api.tool.DsJsonRequestTemplate;
import com.ms.api.tool.SecurityTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
@ExtensionService("testSpi")
@Slf4j
public class TestSpiService extends SPIBaseService implements ExtensionServiceHandler<ItemDTO, JSONObject> {
@Autowired
private DsJsonRequestTemplate dsJsonRequestTemplate;
public class TestSpiService extends SPIBaseService implements ExtensionServiceHandler<ItemDTO, String> {
@Override
public BaseResponse<JSONObject> handle(BaseRequest<ItemDTO> req) {
initHandle(req);
getAuthCode();
HashMap<String, Object> params = new HashMap<>();
params.put("productId",req.getData().getTitle());
params.put("authCode",authCode);
String res = null;
JSONObject resObj = null;
public BaseResponse<String> handle(BaseRequest<ItemDTO> req) {
try{
res = dsJsonRequestTemplate.execute("/micro_move/get_product_info", params);
resObj = JSON.parseObject(res);
log.info(res);
} catch (Exception e) {
initHandle(req);
// do something
E.throwMSException("这里有全局异常抛出");
return R.ok("这里执行不到");
}catch (Throwable e){
e.printStackTrace();
log.error(e.getMessage());
return R.ok(e.getMessage());
}
return R.ok(resObj);
}
}

@ -0,0 +1,14 @@
package com.ms.api.common;
/**
*
*/
public class E {
public static void throwMSException(String msg) throws MSException {
throw new MSException(msg);
}
public static void throwMSException(String msg,Throwable e) throws MSException {
throw new MSException(msg, e);
}
}

@ -0,0 +1,16 @@
package com.ms.api.common;
/**
*
*/
public class MSException extends RuntimeException {
public MSException(String message) {
super(message);
}
public MSException(String message, Throwable cause) {
super(message, cause);
}
}

@ -24,6 +24,7 @@ import com.ms.dal.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
@ -55,6 +56,7 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
@Override
public BaseResponse<Ret> handle(BaseRequest<SaveBatchMoveTaskRequestDTO> req) {
try {
initHandle(req);
// ----参数校验----
SaveBatchMoveTaskRequestDTO fields = req.getData();
@ -107,6 +109,10 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
retData.put("submitCnt", submitCnt);
ret.setData(retData);
return R.ok(ret);
} catch (Throwable e) {
e.printStackTrace();
return R.ok(CommonTool.failResult(e.getMessage()));
}
}
private Ret checkAndFormatData(Long shopId, List<MoveProductDTO> curProductDetails) {
@ -188,6 +194,7 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
/**
*
*
* @param setting
* @return
*/
@ -247,11 +254,8 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
return addRet;
}
private Ret addShopCollectTask(long shopId, List<MoveTaskProductBO> shopProductsList, String name, String copyType, MoveConditionBO shopConditions) {
// TODO 添加事务
// int oldErrorMode = db.setErrorMode(ZcDb.ERROR_MODE_EXCEPTION);
// boolean transStatus = db.startTransaction();
@Transactional(rollbackFor = Exception.class)
public Ret addShopCollectTask(long shopId, List<MoveTaskProductBO> shopProductsList, String name, String copyType, MoveConditionBO shopConditions) {
int taskId = 0;
String cnaliDsDetailMap = "";
try {
@ -260,12 +264,7 @@ public class SaveBatchMoveTaskService extends SPIBaseService implements Extensio
taskId = (Integer) addRet.getData().get("taskId");
cnaliDsDetailMap = (String) addRet.getData().get("cnaliDsDetailMap");
}
// db.commit(transStatus);
// db.setErrorMode(oldErrorMode);
} catch (Exception ex) {
// moveLog.error(ex.getMessage());
// db.rollback(transStatus);
// db.setErrorMode(oldErrorMode);
ex.printStackTrace();
return CommonTool.failResult("添加任务数据失败");
}

Loading…
Cancel
Save