fix: API 호출 결과가 JSON 타입이 아닌 경우 방어로직 추가

dev
gitea-관리자 1 year ago
parent d34771631d
commit b862542654

@ -1,7 +1,5 @@
package kr.xit.core.spring.util.error; package kr.xit.core.spring.util.error;
import org.springframework.http.HttpStatus;
import lombok.Getter; import lombok.Getter;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -19,7 +17,7 @@ import org.springframework.http.HttpStatus;
* 2023-05-25 limju * 2023-05-25 limju
* *
* </pre> * </pre>
* @see kr.xit.core.spring.util.ApiWebClient * @see kr.xit.core.spring.util.ApiWebClientUtil
*/ */
@Getter @Getter
public class ClientError extends RuntimeException { public class ClientError extends RuntimeException {

@ -10,7 +10,6 @@ import kr.xit.core.model.ErrorDTO;
import kr.xit.core.support.utils.Checks; import kr.xit.core.support.utils.Checks;
import kr.xit.core.support.utils.JsonUtils; 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.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClientRequestException; import org.springframework.web.reactive.function.client.WebClientRequestException;
@ -38,19 +37,25 @@ public class ErrorParse {
if(e instanceof BizRuntimeException) { if(e instanceof BizRuntimeException) {
BizRuntimeException be = (BizRuntimeException)e; BizRuntimeException be = (BizRuntimeException)e;
return getStringObjectMap(be.getCode(), be.getMessage(), HttpStatus.BAD_REQUEST); return ApiResponseDTO.error(be.getCode(), be.getMessage(), HttpStatus.BAD_REQUEST);
} }
if(e instanceof ClientError) { if(e instanceof ClientError) {
ClientError ce = (ClientError)e; ClientError ce = (ClientError)e;
ErrorDTO error = JsonUtils.toObject(ce.getBody(), ErrorDTO.class); if(JsonUtils.isJson(ce.getBody())){
return getStringObjectMap(Objects.requireNonNull(error).getErrorCode(), error.getErrorMessage(), ce.getStatus()); ErrorDTO error = JsonUtils.toObject(ce.getBody(), ErrorDTO.class);
return ApiResponseDTO.error(Objects.requireNonNull(error).getErrorCode(), error.getErrorMessage(), ce.getStatus());
}
return ApiResponseDTO.error(String.valueOf(ce.getStatus().value()), ce.getBody(), ce.getStatus());
} }
if(e instanceof ServerError) { if(e instanceof ServerError) {
ServerError ce = (ServerError) e; ServerError ce = (ServerError) e;
ErrorDTO error = JsonUtils.toObject(ce.getBody(), ErrorDTO.class); if(JsonUtils.isJson(ce.getBody())){
return getStringObjectMap(Objects.requireNonNull(error).getErrorCode(), error.getErrorMessage(), ce.getStatus()); ErrorDTO error = JsonUtils.toObject(ce.getBody(), ErrorDTO.class);
return ApiResponseDTO.error(Objects.requireNonNull(error).getErrorCode(), error.getErrorMessage(), ce.getStatus());
}
return ApiResponseDTO.error(String.valueOf(ce.getStatus().value()), ce.getBody(), ce.getStatus());
} }
// Async(React) Exception 처리 // Async(React) Exception 처리
@ -85,16 +90,11 @@ public class ErrorParse {
message = e.getCause().getMessage(); message = e.getCause().getMessage();
} }
return getStringObjectMap(errCode, message, httpStatus);
}
@NotNull
private static ApiResponseDTO getStringObjectMap(String errCode, String message, HttpStatus httpStatus) {
return ApiResponseDTO.error(errCode, message, httpStatus); return ApiResponseDTO.error(errCode, message, httpStatus);
} }
private static ApiResponseDTO getTimeoutException(){ private static ApiResponseDTO getTimeoutException(){
return getStringObjectMap( return ApiResponseDTO.error(
String.valueOf(HttpStatus.REQUEST_TIMEOUT.value()), String.valueOf(HttpStatus.REQUEST_TIMEOUT.value()),
HttpStatus.REQUEST_TIMEOUT.getReasonPhrase(), HttpStatus.REQUEST_TIMEOUT.getReasonPhrase(),
HttpStatus.REQUEST_TIMEOUT); HttpStatus.REQUEST_TIMEOUT);

@ -1,7 +1,5 @@
package kr.xit.core.spring.util.error; package kr.xit.core.spring.util.error;
import org.springframework.http.HttpStatus;
import lombok.Getter; import lombok.Getter;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -19,7 +17,7 @@ import org.springframework.http.HttpStatus;
* 2023-05-25 limju * 2023-05-25 limju
* *
* </pre> * </pre>
* @see kr.xit.core.spring.util.ApiWebClient * @see kr.xit.core.spring.util.ApiWebClientUtil
*/ */
@Getter @Getter
public class ServerError extends RuntimeException { public class ServerError extends RuntimeException {

@ -1,12 +1,5 @@
package kr.xit.core.support.utils; package kr.xit.core.support.utils;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.annotation.PropertyAccessor;
@ -14,7 +7,12 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import kr.xit.core.exception.BizRuntimeException; import kr.xit.core.exception.BizRuntimeException;
import kr.xit.core.spring.util.SpringUtils; import kr.xit.core.spring.util.SpringUtils;
import lombok.AccessLevel; import lombok.AccessLevel;
@ -29,6 +27,20 @@ public class JsonUtils {
private static final ObjectMapper OM = SpringUtils.getObjectMapper(); private static final ObjectMapper OM = SpringUtils.getObjectMapper();
/**
* json string
* @param str String
* @return boolean
*/
public static boolean isJson(final String str) {
try {
OM.readTree(str);
return true;
} catch (JsonProcessingException e) {
return false;
}
}
/** /**
* Object -> json string * Object -> json string
* @return String * @return String

Loading…
Cancel
Save