fix: API 에러 처리 fix

dev
gitea-관리자 1 year ago
parent a352bbfc69
commit c0689d5f1b

@ -269,28 +269,26 @@ log.info("@@@@@@@@@@@@@@@@@로깅 start : [\n{}\n]",MDC.getCopyOfContextMap());
log.info("~~~~~~~~~~~~~~~~~~~~~~~>>>>{}", MDC.get(REQUEST_TRACE_ID)); log.info("~~~~~~~~~~~~~~~~~~~~~~~>>>>{}", MDC.get(REQUEST_TRACE_ID));
if(Checks.isEmpty(MDC.get(REQUEST_TRACE_ID))) return; if(Checks.isEmpty(MDC.get(REQUEST_TRACE_ID))) return;
Map<String,Object> map = ErrorParse.extractError(e); ApiResponseDTO dto = ErrorParse.extractError(e);
loggingService.modifyLogging( loggingService.modifyLogging(
LoggingDTO LoggingDTO
.builder() .builder()
.requestId(MDC.get(REQUEST_TRACE_ID)) .requestId(MDC.get(REQUEST_TRACE_ID))
.success("false") .success("false")
.response(JsonUtils.toJson(ApiResponseDTO.error(String.valueOf(map.get(CODE)), String.valueOf(map.get(MESSAGE)), (HttpStatus)map.get("httpStatus")))) .response(JsonUtils.toJson(dto))
.message(String.valueOf(map.get(MESSAGE))) .message(dto.getMessage())
.build()); .build());
//FIXME :: slack webhook log push //FIXME :: slack webhook log push
if(isSlackEnabled){ if(isSlackEnabled){
if(RequestContextHolder.getRequestAttributes() != null) { if(RequestContextHolder.getRequestAttributes() != null) {
slackWebhookPush.sendSlackAlertLog( slackWebhookPush.sendSlackAlertLog(
String.format("[%s]%s(%s)", MDC.get(REQUEST_TRACE_ID), String.valueOf(map.get(CODE)), String.format("[%s]%s(%s)", MDC.get(REQUEST_TRACE_ID), dto.getCode(), dto.getMessage()),
String.valueOf(map.get(MESSAGE))),
((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest()); ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest());
}else{ }else{
slackWebhookPush.sendSlackAlertLog( slackWebhookPush.sendSlackAlertLog(
String.format("[%s]%s(%s)", MDC.get(REQUEST_TRACE_ID), String.valueOf(map.get(CODE)), String.format("[%s]%s(%s)", MDC.get(REQUEST_TRACE_ID), dto.getCode(), dto.getMessage()),
String.valueOf(map.get(MESSAGE))),
MDC.get("uri"), MDC.get("uri"),
"batch call"); "batch call");
} }

@ -39,11 +39,12 @@ contract:
ci: /digital/niceid/cert/v1.0/ipin/addinfo/ci ci: /digital/niceid/cert/v1.0/ipin/addinfo/ci
kt: kt:
dummy: true dummy: true
# vpn 사용전
host: http://172.16.0.13:10210/ONLWeb
# vpn 사용 # vpn 사용
# host: http://211.43.10.163:10210/ONLWeb # host: http://172.16.0.13:10210/ONLWeb
# 공인 IP
host: http://211.43.10.163:10210/ONLWeb
# 운영 # 운영
#host: http://172.16.1.182:10210/ONLWeb #host: http://172.16.1.182:10210/ONLWeb
# signgu-code: 11000 api:
# ffnlg-code: 11 generate-token: /oauth/token
before-send: /api/message/before/send

@ -227,28 +227,26 @@ log.info("@@@@@@@@@@@@@@@@@로깅 start : [\n{}\n]",MDC.getCopyOfContextMap());
log.info("~~~~~~~~~~~~~~~~~~~~~~~>>>>{}", MDC.get(REQUEST_TRACE_ID)); log.info("~~~~~~~~~~~~~~~~~~~~~~~>>>>{}", MDC.get(REQUEST_TRACE_ID));
if(Checks.isEmpty(MDC.get(REQUEST_TRACE_ID))) return; if(Checks.isEmpty(MDC.get(REQUEST_TRACE_ID))) return;
Map<String,Object> map = ErrorParse.extractError(e); ApiResponseDTO dto = ErrorParse.extractError(e);
loggingService.modifyLogging( loggingService.modifyLogging(
LoggingDTO LoggingDTO
.builder() .builder()
.requestId(MDC.get(REQUEST_TRACE_ID)) .requestId(MDC.get(REQUEST_TRACE_ID))
.success("false") .success("false")
.response(JsonUtils.toJson(ApiResponseDTO.error(String.valueOf(map.get(CODE)), String.valueOf(map.get(MESSAGE)), (HttpStatus)map.get("httpStatus")))) .response(JsonUtils.toJson(dto))
.message(String.valueOf(map.get(MESSAGE))) .message(dto.getMessage())
.build()); .build());
//FIXME :: slack webhook log push //FIXME :: slack webhook log push
if(isSlackEnabled){ if(isSlackEnabled){
if(RequestContextHolder.getRequestAttributes() != null) { if(RequestContextHolder.getRequestAttributes() != null) {
slackWebhookPush.sendSlackAlertLog( slackWebhookPush.sendSlackAlertLog(
String.format("[%s]%s(%s)", MDC.get(REQUEST_TRACE_ID), String.valueOf(map.get(CODE)), String.format("[%s]%s(%s)", MDC.get(REQUEST_TRACE_ID), dto.getCode(), dto.getMessage()),
String.valueOf(map.get(MESSAGE))),
((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest()); ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest());
}else{ }else{
slackWebhookPush.sendSlackAlertLog( slackWebhookPush.sendSlackAlertLog(
String.format("[%s]%s(%s)", MDC.get(REQUEST_TRACE_ID), String.valueOf(map.get(CODE)), String.format("[%s]%s(%s)", MDC.get(REQUEST_TRACE_ID), dto.getCode(), dto.getMessage()),
String.valueOf(map.get(MESSAGE))),
MDC.get("uri"), MDC.get("uri"),
"batch call"); "batch call");
} }

@ -1,33 +1,31 @@
package kr.xit.core.model; package kr.xit.core.model;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.google.gson.GsonBuilder;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import org.apache.commons.lang.StringUtils;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import com.google.gson.GsonBuilder;
import io.swagger.v3.oas.annotations.media.Schema;
import kr.xit.core.consts.ErrorCode; import kr.xit.core.consts.ErrorCode;
import kr.xit.core.exception.BizRuntimeException; import kr.xit.core.exception.BizRuntimeException;
import kr.xit.core.support.utils.Checks; import kr.xit.core.support.utils.Checks;
import kr.xit.core.support.utils.ConvertHelper; import kr.xit.core.support.utils.ConvertHelper;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import org.egovframe.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
/** /**
* <pre> * <pre>
@ -46,7 +44,11 @@ import lombok.Setter;
@Schema(name = "ApiResponseDTO", description = "Restful API 결과") @Schema(name = "ApiResponseDTO", description = "Restful API 결과")
@Getter @Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED) @NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ApiResponseDTO<T> implements Serializable { @AllArgsConstructor
@Builder
@JsonRootName("result")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResponseDTO<T> implements IApiResponse {
private static final String FAIL_STATUS = "fail"; private static final String FAIL_STATUS = "fail";
private static final String ERROR_STATUS = "error"; private static final String ERROR_STATUS = "error";

@ -1,12 +1,11 @@
package kr.xit.core.model; package kr.xit.core.model;
import javax.validation.constraints.Size; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import javax.validation.constraints.Size;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
@ -26,10 +25,11 @@ import lombok.experimental.SuperBuilder;
* </pre> * </pre>
*/ */
@Schema(name = "ErrorDTO", description = "에러 DTO") @Schema(name = "ErrorDTO", description = "에러 DTO")
@Data @Getter
@SuperBuilder @SuperBuilder
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ErrorDTO implements IApiResponse { public class ErrorDTO implements IApiResponse {
/** /**
* (max:40) * (max:40)

@ -4,15 +4,17 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.concurrent.CompletionException; import java.util.concurrent.CompletionException;
import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException;
import kr.xit.core.consts.ErrorCode;
import kr.xit.core.exception.BizRuntimeException;
import kr.xit.core.model.ApiResponseDTO;
import kr.xit.core.spring.util.error.ErrorParse;
import kr.xit.core.support.utils.Checks;
import lombok.extern.slf4j.Slf4j;
import org.egovframe.rte.fdl.cmmn.exception.EgovBizException; import org.egovframe.rte.fdl.cmmn.exception.EgovBizException;
import org.slf4j.MDC;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
@ -22,13 +24,6 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest; import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import kr.xit.core.consts.ErrorCode;
import kr.xit.core.model.ApiResponseDTO;
import kr.xit.core.exception.BizRuntimeException;
import kr.xit.core.spring.util.error.ErrorParse;
import kr.xit.core.support.utils.Checks;
import lombok.extern.slf4j.Slf4j;
/** /**
* <pre> * <pre>
* description : ExceptionHandlerFilter(Filter ) * description : ExceptionHandlerFilter(Filter )
@ -204,7 +199,6 @@ public class CustomRestExceptionHandler extends ResponseEntityExceptionHandler {
private ApiResponseDTO sendError(Throwable e) { private ApiResponseDTO sendError(Throwable e) {
Map<String,Object> map = ErrorParse.extractError(Checks.checkVal(e.getCause(), e)); return ErrorParse.extractError(Checks.checkVal(e.getCause(), e));
return ApiResponseDTO.error(String.valueOf(map.get("code")), String.valueOf(map.get("message")), (HttpStatus)map.get("httpStatus"));
} }
} }

@ -154,8 +154,7 @@ public class ApiWebClientUtil {
public <T> ApiResponseDTO<T> sendError(final Throwable e) { public <T> ApiResponseDTO<T> sendError(final Throwable e) {
Map<String,Object> map = ErrorParse.extractError(e.getCause()); return ErrorParse.extractError(e.getCause());
return ApiResponseDTO.error(String.valueOf(map.get("code")), String.valueOf(map.get("message")), (HttpStatus)map.get("httpStatus"));
} }
private URI createUrl(final String endPoint, final String... value) { private URI createUrl(final String endPoint, final String... value) {

@ -1,23 +1,19 @@
package kr.xit.core.spring.util.error; package kr.xit.core.spring.util.error;
import java.util.HashMap; import io.netty.channel.ConnectTimeoutException;
import java.util.Map; import io.netty.handler.timeout.ReadTimeoutException;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import kr.xit.core.exception.BizRuntimeException; import kr.xit.core.exception.BizRuntimeException;
import kr.xit.core.model.ApiResponseDTO;
import kr.xit.core.model.ErrorDTO;
import kr.xit.core.support.utils.Checks;
import kr.xit.core.support.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClientRequestException; import org.springframework.web.reactive.function.client.WebClientRequestException;
import io.netty.channel.ConnectTimeoutException;
import io.netty.handler.timeout.ReadTimeoutException;
import kr.xit.core.model.ErrorDTO;
import kr.xit.core.support.utils.Checks;
import kr.xit.core.support.utils.JsonUtils;
/** /**
* <pre> * <pre>
* description : * description :
@ -35,7 +31,7 @@ import kr.xit.core.support.utils.JsonUtils;
*/ */
public class ErrorParse { public class ErrorParse {
public static Map<String,Object> extractError(final Throwable e){ public static ApiResponseDTO extractError(final Throwable e){
String errCode = String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()); String errCode = String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value());
String message = Checks.isNotNull(e) ? e.getLocalizedMessage() : StringUtils.EMPTY; String message = Checks.isNotNull(e) ? e.getLocalizedMessage() : StringUtils.EMPTY;
HttpStatus httpStatus = null; HttpStatus httpStatus = null;
@ -93,15 +89,11 @@ public class ErrorParse {
} }
@NotNull @NotNull
private static Map<String, Object> getStringObjectMap(String errCode, String message, HttpStatus httpStatus) { private static ApiResponseDTO getStringObjectMap(String errCode, String message, HttpStatus httpStatus) {
Map<String, Object> errorMap = new HashMap<>(); return ApiResponseDTO.error(errCode, message, httpStatus);
errorMap.put("code", errCode);
errorMap.put("message", message);
errorMap.put("httpStatus", httpStatus);
return errorMap;
} }
private static Map<String, Object> getTimeoutException(){ private static ApiResponseDTO getTimeoutException(){
return getStringObjectMap( return getStringObjectMap(
String.valueOf(HttpStatus.REQUEST_TIMEOUT.value()), String.valueOf(HttpStatus.REQUEST_TIMEOUT.value()),
HttpStatus.REQUEST_TIMEOUT.getReasonPhrase(), HttpStatus.REQUEST_TIMEOUT.getReasonPhrase(),

Loading…
Cancel
Save