Commit 32a056b9 authored by 강현수's avatar 강현수

refactor : exception modify

parent a161150f
......@@ -83,7 +83,10 @@ public class OmsLogger {
OmsObject oms = LoggerContext.getTxLogger();
oms.setOmsData(key, value);
}
public void addOmsDatat(OmsLogField key, BackendServiceStatus value) {
OmsObject oms = LoggerContext.getTxLogger();
oms.setOmsData(key, String.valueOf(value.getStatusCode()));
}
public String getOmsData(OmsLogField key) {
OmsObject oms = LoggerContext.getTxLogger();
return oms.getOmsData(key);
......
......@@ -2,12 +2,16 @@ package net.herit.ami.commons.response;
public enum BackendServiceStatus {
SUCCESS(200, 0, ""),
INTERNAL_SERVER_ERROR(500, 0, "internal server error"),
NOT_PROVIDE_SERVICE(404, 1, "not support : "),
INVALID_PARAM(400, 2, "invalid parameter"),
DUPLICATED_CONTENTS(400, 3, "duplicated contents"),
RESPONSE_CANNOT_BE_NULL(404,401,"Response Connot Be null"),
SYSTEM_ERROR(500, 4, "Exception"),
RESPONSE_READ_SYSTEM_ERROR(500, 501, "IOException"),
MAPPER_ERROR(500, 502, "Mapper pasing error"),
JSON_PROCESS_ONE(500, 503 ,"One Day Process JSON process Excetion"),
INSERT_ERROR(500, 504 ,"INSERT ERROR Parameters"),
DB_INSERT_ERROR(500, 3, "db insert error"),
SMP_SERVER_RECV_NULL(502, 850, "SMP List Null"),
SND_SERVER_RECV_NULL(502, 850, "SMP List Null"),
;
......
......@@ -45,7 +45,7 @@ public class SmpEnterConsumeDataJob extends QuartzJobBeans{
call.info("callId:{}", callId);
int resultCount = 0;
for (int i = 0; i < areaCd.length; ++i) resultCount += smpService.toDoGetSmpDataAndInsert(areaCd[i], serviceKey);
for (int i = 0; i < areaCd.length; ++i) resultCount += smpService.GetSmpDataAndInsert(areaCd[i], serviceKey);
oms.addOmsData(OmsLogField.R1, String.valueOf(resultCount));
oms.setStatusAndResult(BackendServiceStatus.SUCCESS);
......
......@@ -10,6 +10,7 @@ import net.herit.ami.commons.exception.ExceptionType;
import net.herit.ami.commons.logger.call.log.CallLogger;
import net.herit.ami.commons.logger.oms.field.OmsLogField;
import net.herit.ami.commons.logger.oms.log.OmsLogger;
import net.herit.ami.commons.response.BackendServiceStatus;
import net.herit.ami.commons.util.JsonUtil;
import net.herit.ami.features.stat.dto.Holiday.HolidayDataDAO;
import net.herit.ami.features.stat.dto.response_model.HolidaRootResponse;
......@@ -38,13 +39,9 @@ public class DefaultConnectService implements ConnectionService{
@Override
public void connect(Response connect) {
call.debug("\n---- Start to Connection And Convert----");
try {
validateResponse(connect);
apiConnectLog(connect);
commonProcess(connect);
}catch (Exception e){
}
validateResponse(connect);
apiConnectLog(connect);
commonProcess(connect);
}
public void commonProcess(Response connect) throws CommonException{
......@@ -64,7 +61,8 @@ public class DefaultConnectService implements ConnectionService{
}
}catch (Exception ex){
call.error("Error Occurred with External Server request. cause: ", ex);
throw new CommonException(ExceptionType.EXTERNAL_API_CALL_ERROR);
oms.addOmsData(OmsLogField.R1, getClass().getName());
oms.addOmsDatat(OmsLogField.R2, BackendServiceStatus.SYSTEM_ERROR);
}
}
private void apiConnectLog(Response connect) {
......@@ -77,25 +75,26 @@ public class DefaultConnectService implements ConnectionService{
call.info("\nConnection:\n\n{}", getConnectionLog(printRequest, printResponse));
}
private String builderResponseBody(Response connect) {
StringBuilder builder = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(connect.body().asReader(Charset.defaultCharset()));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null){
builder.append(line);
}
return builder.toString();
} catch (IOException e) {
throw new RuntimeException(e);
call.error("Error IOException:{} / {} ", getClass().getName(), e);
oms.addOmsData(OmsLogField.R1, getClass().getName());
oms.addOmsDatat(OmsLogField.R2, BackendServiceStatus.SYSTEM_ERROR);
}
return builder.toString();
}
/*
* oneDayBody : 하나일때 사용
* anotherDaybody : 공유일이 많을때 사용하기 위함
* itmeMap : 공유일이 없는 경우를 확인하기 위함
* */
// 원시타입만 사용해서 보기가 힘든것같습니다 클래스로 래핑하면 좋을것같습니다
private Map<String, String> segregate(String oneDayBody) {
Map<String, String> buildMap = new HashMap<>();
try {
......@@ -107,20 +106,24 @@ public class DefaultConnectService implements ConnectionService{
buildMap.put("anotherDaybody", anotherDaybody.toString());
buildMap.put("item", itmeMap.get("totalCount").toString());
return buildMap;
} catch (IOException e) {
throw new CommonException(e.getMessage());
call.error("Error IOException:{} / {} ", getClass().getName(), e);
oms.addOmsData(OmsLogField.R1, getClass().getName());
oms.addOmsDatat(OmsLogField.R2, BackendServiceStatus.MAPPER_ERROR);
}
return buildMap;
}
// is 접두사는 boolean 리턴 메소드에 주로 쓰이는것으로 압니다. validateResponse 정도가 적당할것같습니다.
private void validateResponse(Response connect) {
if(JsonUtil.isNull(connect)){
call.info("Failed Connect to External Server.");
throw new CommonException(ExceptionType.RESPONSE_CANNOT_BE_NULL);
oms.addOmsData(OmsLogField.R1, getClass().getName());
oms.addOmsDatat(OmsLogField.R2, BackendServiceStatus.RESPONSE_CANNOT_BE_NULL);
}
if(connect.body() == null){
call.info("response of Conntion. {} , {}"+ connect, connect.body());
throw new CommonException(ExceptionType.RESPONSE_CANNOT_BE_NULL);
oms.addOmsData(OmsLogField.R1, getClass().getName());
oms.addOmsDatat(OmsLogField.R2, BackendServiceStatus.JSON_PROCESS_ONE);
}
}
private void anotherDayProcess(List<HolidayDataItem> resBody, String count) {
......@@ -136,6 +139,8 @@ public class DefaultConnectService implements ConnectionService{
successCount += success;
}else {
call.debug("INSERT ERROR Parameters : " + holidayanotherDataDAO);
oms.addOmsData(OmsLogField.R1, getClass().getName());
oms.addOmsDatat(OmsLogField.R2, BackendServiceStatus.INSERT_ERROR);
}
}
oms.addOmsData(OmsLogField.R2, String.valueOf(successCount));
......@@ -154,7 +159,9 @@ public class DefaultConnectService implements ConnectionService{
HolidayDataDAO holidayDataDAO = buildHolidayDataDAO(mapper.readValue(objectBody, SoloHolidayDataItem.class));
success = holidayRepository.insertHoliDayData(holidayDataDAO);
} catch (JsonProcessingException e) {
throw new CommonException(ExceptionType.JSON_PROCESS_ONE);
call.error("Excetion {} onlyDayProcess, {}", getClass().getName(), e.getMessage());
oms.addOmsData(OmsLogField.R1, getClass().getName());
oms.addOmsDatat(OmsLogField.R2, BackendServiceStatus.JSON_PROCESS_ONE);
}
finally {
oms.addOmsData(OmsLogField.R1, "1");
......
......@@ -17,7 +17,6 @@ public class ResponseService {
private final CallLogger call;
private final String JOB_NAME = this.getClass().getSimpleName();
private final DefaultConnectService defaultconnectService;
// 접근제어자가 없음
private String line = null;
public String responseXmlToString(Response response) {
StringBuilder builder = new StringBuilder();
......
......@@ -26,9 +26,7 @@ public class SmpService {
private final ResponseService responseService;
private final SmpEnterFeignClient smpEnterFeignClient;
// 메소드명의 접두사가 여러개이고(to,Do,Get), Get과 Insert 두가지의 동작을 명시하고있어서 역할을 명확히 알기가 힘듭니다
public int toDoGetSmpDataAndInsert(int i, String serviceKey) {
; // ???
public int GetSmpDataAndInsert(int i, String serviceKey) {
ArrayList<SmpDTO> smpList = getResponseItme(i, serviceKey);
return sendSmp(smpList, i);
......@@ -37,10 +35,8 @@ public class SmpService {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(SmpRoot.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
// 언마샬링 하는 부분과 response 받아오는 부분을 분리해보는건 어떨까요?
SmpRoot smpResponse = (SmpRoot) unmarshaller.unmarshal( new StringReader(
responseService.responseXmlToString(smpEnterFeignClient.requestConsumeDataByAll(area, serviceKeys))));
String ResponseXml= responseService.responseXmlToString(smpEnterFeignClient.requestConsumeDataByAll(area, serviceKeys));
SmpRoot smpResponse = (SmpRoot) unmarshaller.unmarshal( new StringReader(ResponseXml));
return smpResponse.getItem();
} catch (JAXBException e) {
call.error("ResponseService class in method explorerSnd JAXBException : {}", e.getMessage());
......@@ -60,14 +56,13 @@ public class SmpService {
private SmpDTO buildSmpDto(SmpDTO smpDTO) {
// 잘못된 빌더패턴 사용법인것같습니다.
return new SmpDTO().builder()
.idx(smpDTO.getIdx())
return SmpDTO.builder().idx(smpDTO.getIdx())
.areaCd(smpDTO.getAreaCd())
.smp(smpDTO.getSmp())
.tradeHour(String.format("%02d", Integer.parseInt(smpDTO.getTradeHour())))
.tradeDay(smpDTO.getTradeDay())
.created_at(smpDTO.getCreated_at())
.build();
.created_at(smpDTO.getCreated_at()).
build();
}
private int insertCall(SmpDTO params){
......
......@@ -26,12 +26,12 @@ public class SndService {
call.debug("explorerSnd item pasing : {}", snd);
return sndRepository.insertSnd(snd);
}
//애도 관심사를 분리하는게 어렵네...
public SndRoot xmlToSndRoot(String serviceKeys) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(SndRoot.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return (SndRoot) unmarshaller.unmarshal(new StringReader(responseService.responseXmlToString(sndEnterFeignClient.requestConsumeData(serviceKeys))));
String resString=responseService.responseXmlToString(sndEnterFeignClient.requestConsumeData(serviceKeys));
return (SndRoot) unmarshaller.unmarshal(new StringReader(resString));
} catch (JAXBException e) {
throw new CommonException(ExceptionType.XML_TO_OBJECT_EXCEPTION);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment