Browse Source

feat: Error RS 포맷팅 변경, 서버용 로그 추가

pull/2/head
lkd9125(이경도) 8 months ago
parent
commit
e5f77b3de6
  1. 54
      app/kac-app/src/main/java/kr/co/palnet/kac/app/core/exception/ServerExceptionHandler.java
  2. 2
      app/kac-app/src/main/resources/application.yml
  3. 30
      common/core/src/main/java/kr/co/palnet/kac/core/exception/AppException.java
  4. 6
      common/core/src/main/java/kr/co/palnet/kac/core/exception/BaseErrorCode.java
  5. 12
      common/core/src/main/java/kr/co/palnet/kac/core/exception/model/BaseErrorModel.java
  6. 7
      web/api-com/src/main/java/kr/co/palnet/kac/api/v1/com/code/controller/ComCodeController.java

54
app/kac-app/src/main/java/kr/co/palnet/kac/app/core/exception/ServerExceptionHandler.java

@ -2,43 +2,81 @@ package kr.co.palnet.kac.app.core.exception;
import kr.co.palnet.kac.core.exception.AppException; import kr.co.palnet.kac.core.exception.AppException;
import kr.co.palnet.kac.core.exception.ExceptionCode; import kr.co.palnet.kac.core.exception.BaseErrorCode;
import kr.co.palnet.kac.core.exception.model.BaseErrorModel; import kr.co.palnet.kac.core.exception.model.BaseErrorModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
@RestControllerAdvice @RestControllerAdvice
@Slf4j
public class ServerExceptionHandler { public class ServerExceptionHandler {
@ExceptionHandler(AppException.class) @ExceptionHandler(AppException.class)
public ResponseEntity<BaseErrorModel> appExceptionHandle(AppException e) { public ResponseEntity<BaseErrorModel> appExceptionHandle(AppException e) {
ExceptionCode errorType = e.getErrorCode(); BaseErrorCode errorType = e.getErrorCode();
BaseErrorModel baseBody = new BaseErrorModel(); BaseErrorModel baseBody = new BaseErrorModel();
baseBody.setCode(errorType.code()); baseBody.setTimestamp(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(Instant.now().atZone(ZoneId.systemDefault())));
baseBody.setStatus(errorType.status());
baseBody.setError(errorType.code());
baseBody.setMessage(errorType.message()); baseBody.setMessage(errorType.message());
baseBody.setPath(this.getUrl());
e.printStackTrace(); log.warn("",e);
return ResponseEntity.status(errorType.status()).body(baseBody); return ResponseEntity.status(errorType.status()).body(baseBody);
} }
@ExceptionHandler(NoResourceFoundException.class)
public ResponseEntity<BaseErrorModel> noResourceFoundException(NoResourceFoundException e){
BaseErrorModel baseBody = new BaseErrorModel();
baseBody.setTimestamp(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(Instant.now().atZone(ZoneId.systemDefault())));
baseBody.setStatus(e.getBody().getStatus());
baseBody.setError(BaseErrorCode.NOT_FOUND_RESOURCE.code());
baseBody.setMessage(e.getBody().getDetail());
baseBody.setPath(e.getResourcePath());
log.warn("",e);
return ResponseEntity.status(e.getBody().getStatus()).body(baseBody);
}
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public ResponseEntity<BaseErrorModel> exceptionHandle(Exception e) { public ResponseEntity<BaseErrorModel> exceptionHandle(Exception e) {
BaseErrorCode errorType = BaseErrorCode.INTERNAL_SERVER_ERROR;
ExceptionCode errorType = ExceptionCode.INTERNAL_SERVER_ERROR;
BaseErrorModel baseBody = new BaseErrorModel(); BaseErrorModel baseBody = new BaseErrorModel();
baseBody.setCode(errorType.code()); baseBody.setTimestamp(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(Instant.now().atZone(ZoneId.systemDefault())));
baseBody.setStatus(500);
baseBody.setError(e.getClass().getSimpleName());
baseBody.setPath(this.getUrl());
baseBody.setMessage(errorType.message()); baseBody.setMessage(errorType.message());
e.printStackTrace(); log.warn("",e);
return ResponseEntity.status(errorType.status()).body(baseBody); return ResponseEntity.status(errorType.status()).body(baseBody);
} }
private String getUrl() {
ServletRequestAttributes servletRequestAttr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (servletRequestAttr != null) return servletRequestAttr.getRequest().getRequestURI();
else return null;
}
} }

2
app/kac-app/src/main/resources/application.yml

@ -8,7 +8,7 @@ spring:
enabled: true enabled: true
jpa: jpa:
hibernate: hibernate:
ddl-auto: update ddl-auto: none
naming: naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

30
common/core/src/main/java/kr/co/palnet/kac/core/exception/AppException.java

@ -15,7 +15,7 @@ public class AppException extends RuntimeException {
private final Source source; private final Source source;
private final ExceptionCode errorCode; private final BaseErrorCode errorCode;
private final Object[] paramArray; private final Object[] paramArray;
@ -24,7 +24,7 @@ public class AppException extends RuntimeException {
private String sourceErrorMessage; private String sourceErrorMessage;
public AppException(Throwable th, Level level, Source source, ExceptionCode errorCode, Object... paramArray) { public AppException(Throwable th, Level level, Source source, BaseErrorCode errorCode, Object... paramArray) {
super(th != null ? th.getMessage() : errorCode.message(), th); super(th != null ? th.getMessage() : errorCode.message(), th);
this.level = level; this.level = level;
this.errorCode = errorCode; this.errorCode = errorCode;
@ -34,15 +34,15 @@ public class AppException extends RuntimeException {
this.sourceErrorMessage = errorCode.message(); this.sourceErrorMessage = errorCode.message();
} }
public AppException(Throwable th, Level level, ExceptionCode errorCode, Object... paramArray) { public AppException(Throwable th, Level level, BaseErrorCode errorCode, Object... paramArray) {
this(th, level, Source.INTERNAL, errorCode, paramArray); this(th, level, Source.INTERNAL, errorCode, paramArray);
} }
public AppException(Throwable th, Source source, ExceptionCode errorCode, Object... paramArray) { public AppException(Throwable th, Source source, BaseErrorCode errorCode, Object... paramArray) {
this(th, Level.WARN, source, errorCode, paramArray); this(th, Level.WARN, source, errorCode, paramArray);
} }
public AppException(Throwable th, ExceptionCode errorCode, Object... paramArray) { public AppException(Throwable th, BaseErrorCode errorCode, Object... paramArray) {
this(th, Level.WARN, Source.INTERNAL, errorCode, paramArray); this(th, Level.WARN, Source.INTERNAL, errorCode, paramArray);
} }
@ -58,30 +58,30 @@ public class AppException extends RuntimeException {
this(null, Level.WARN, source, errorCode, paramArray); this(null, Level.WARN, source, errorCode, paramArray);
} }
public AppException(ExceptionCode errorCode, Object... paramArray) { public AppException(BaseErrorCode errorCode, Object... paramArray) {
this(null, Level.WARN, Source.INTERNAL, errorCode, paramArray); this(null, Level.WARN, Source.INTERNAL, errorCode, paramArray);
} }
// public BaseException(String errorCode) { // public BaseException(String errorCode) {
// this(errorCode, new Object()); // this(errorCode, new Object());
// } // }
public AppException(Throwable th, ExceptionCode erorCode, String sourceErrorMessage) { public AppException(Throwable th, BaseErrorCode erorCode, String sourceErrorMessage) {
this(th, Level.WARN, ExceptionCode.INTERNAL_SERVER_ERROR, (Object[])null); this(th, Level.WARN, BaseErrorCode.INTERNAL_SERVER_ERROR, (Object[])null);
this.sourceErrorCode = erorCode.code(); this.sourceErrorCode = erorCode.code();
this.sourceErrorMessage = errorCode.message(); this.sourceErrorMessage = errorCode.message();
} }
public AppException(ExceptionCode sourceErrorCode, String sourceErrorMessage) { public AppException(BaseErrorCode sourceErrorCode, String sourceErrorMessage) {
this(null, sourceErrorCode, sourceErrorMessage); this(null, sourceErrorCode, sourceErrorMessage);
} }
@Deprecated @Deprecated
public AppException(Throwable th, Level level, ExceptionCode errorCode, Source source, Object... paramArray) { public AppException(Throwable th, Level level, BaseErrorCode errorCode, Source source, Object... paramArray) {
this(th, level, source, errorCode, paramArray); this(th, level, source, errorCode, paramArray);
} }
@Deprecated @Deprecated
public AppException(Level level, ExceptionCode errorCode, Source source, Object... paramArray) { public AppException(Level level, BaseErrorCode errorCode, Source source, Object... paramArray) {
this(null, level, source, errorCode, paramArray); this(null, level, source, errorCode, paramArray);
} }
@ -89,7 +89,7 @@ public class AppException extends RuntimeException {
return errorCode.code(); return errorCode.code();
} }
public ExceptionCode getErrorCode(){ public BaseErrorCode getErrorCode(){
return errorCode; return errorCode;
} }
@ -107,14 +107,14 @@ public class AppException extends RuntimeException {
public String getErrorMessage(MessageSource ms) { public String getErrorMessage(MessageSource ms) {
if (ms == null) { if (ms == null) {
return ExceptionCode.INTERNAL_SERVER_ERROR.message(); return BaseErrorCode.INTERNAL_SERVER_ERROR.message();
} }
log.debug("############ getErrorMessage : {}",ms); log.debug("############ getErrorMessage : {}",ms);
log.debug("############ getErrorCode : {}",getCode()); log.debug("############ getErrorCode : {}",getCode());
log.debug("############ getParamArray : {}",getParamArray()); log.debug("############ getParamArray : {}",getParamArray());
log.debug("############ ErrorCode.NOT_REGIST_ERROR_CODE.message() : {}",ExceptionCode.NOT_REGIST_ERROR_CODE.message()); log.debug("############ ErrorCode.NOT_REGIST_ERROR_CODE.message() : {}", BaseErrorCode.NOT_REGIST_ERROR_CODE.message());
log.debug("############ Locale.getDefault() : {}",Locale.getDefault()); log.debug("############ Locale.getDefault() : {}",Locale.getDefault());
return ms.getMessage(getCode(), getParamArray(), ExceptionCode.NOT_REGIST_ERROR_CODE.message(), Locale.getDefault()); return ms.getMessage(getCode(), getParamArray(), BaseErrorCode.NOT_REGIST_ERROR_CODE.message(), Locale.getDefault());
} }
public String getSourceErrorCode() { public String getSourceErrorCode() {

6
common/core/src/main/java/kr/co/palnet/kac/core/exception/ExceptionCode.java → common/core/src/main/java/kr/co/palnet/kac/core/exception/BaseErrorCode.java

@ -1,7 +1,7 @@
package kr.co.palnet.kac.core.exception; package kr.co.palnet.kac.core.exception;
public enum ExceptionCode { public enum BaseErrorCode {
SUCCESS("1", 200, "성공"), SUCCESS("1", 200, "성공"),
FAIL("-1" , 500, "실패"), FAIL("-1" , 500, "실패"),
@ -20,6 +20,8 @@ public enum ExceptionCode {
DB_ERROR("DB001", 500, "디비 처리중 오류"), DB_ERROR("DB001", 500, "디비 처리중 오류"),
NOT_FOUND_RESOURCE("N001", 404, "정의되지 않은 요청입니다."),
TIME_OUT("T001", 400, "인증 시간 초과"), TIME_OUT("T001", 400, "인증 시간 초과"),
FILE_ERROR("F001", 500, "파일 읽어오기 오류"), FILE_ERROR("F001", 500, "파일 읽어오기 오류"),
@ -41,7 +43,7 @@ public enum ExceptionCode {
private final String message; private final String message;
private ExceptionCode(String code, int status, String message) { private BaseErrorCode(String code, int status, String message) {
this.code = code; this.code = code;
this.status = status; this.status = status;
this.message = message; this.message = message;

12
common/core/src/main/java/kr/co/palnet/kac/core/exception/model/BaseErrorModel.java

@ -5,10 +5,18 @@ import lombok.Data;
@Data @Data
public class BaseErrorModel { public class BaseErrorModel {
private String code; private String timestamp;
private int status;
private String error;
private String path;
private String message; private String message;
private String desc; // private String code;
//
// private String desc;
} }

7
web/api-com/src/main/java/kr/co/palnet/kac/api/v1/com/code/controller/ComCodeController.java

@ -2,10 +2,6 @@ package kr.co.palnet.kac.api.v1.com.code.controller;
import java.util.List; import java.util.List;
import kr.co.palnet.kac.core.exception.AppException;
import kr.co.palnet.kac.core.exception.ExceptionCode;
import kr.co.palnet.kac.data.com.domain.ComCdBas;
import kr.co.palnet.kac.data.com.domain.QComCdBas;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
@ -48,9 +44,6 @@ public class ComCodeController {
@GetMapping("/all") @GetMapping("/all")
@Operation(summary = "전체 코드 조회", description = "전체 코드를 조회합니다.") @Operation(summary = "전체 코드 조회", description = "전체 코드를 조회합니다.")
public ResponseEntity<List<CodeGroupRS>> getAllGroup(SearchCodeAllRQ rq) { public ResponseEntity<List<CodeGroupRS>> getAllGroup(SearchCodeAllRQ rq) {
log.warn("ㅇㅇ => {}", rq);
if(true) throw new AppException(ExceptionCode.DB_ERROR);
List<CodeGroupRS> allCode = comCodeService.getAllGroup(rq); List<CodeGroupRS> allCode = comCodeService.getAllGroup(rq);
return ResponseEntity.ok(allCode); return ResponseEntity.ok(allCode);
} }

Loading…
Cancel
Save