diff --git a/app/kac-app/src/main/java/kr/co/palnet/kac/app/core/exception/ServerExceptionHandler.java b/app/kac-app/src/main/java/kr/co/palnet/kac/app/core/exception/ServerExceptionHandler.java index 68aee78..0968d92 100644 --- a/app/kac-app/src/main/java/kr/co/palnet/kac/app/core/exception/ServerExceptionHandler.java +++ b/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.ExceptionCode; +import kr.co.palnet.kac.core.exception.BaseErrorCode; import kr.co.palnet.kac.core.exception.model.BaseErrorModel; +import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; 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 +@Slf4j public class ServerExceptionHandler { @ExceptionHandler(AppException.class) public ResponseEntity appExceptionHandle(AppException e) { - ExceptionCode errorType = e.getErrorCode(); + BaseErrorCode errorType = e.getErrorCode(); 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()); - - e.printStackTrace(); + baseBody.setPath(this.getUrl()); + + log.warn("",e); return ResponseEntity.status(errorType.status()).body(baseBody); } + @ExceptionHandler(NoResourceFoundException.class) + public ResponseEntity 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) public ResponseEntity exceptionHandle(Exception e) { - - ExceptionCode errorType = ExceptionCode.INTERNAL_SERVER_ERROR; + BaseErrorCode errorType = BaseErrorCode.INTERNAL_SERVER_ERROR; 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()); - - e.printStackTrace(); + + log.warn("",e); 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; + } + + + } diff --git a/app/kac-app/src/main/resources/application.yml b/app/kac-app/src/main/resources/application.yml index 92f5077..d72bf35 100644 --- a/app/kac-app/src/main/resources/application.yml +++ b/app/kac-app/src/main/resources/application.yml @@ -8,7 +8,7 @@ spring: enabled: true jpa: hibernate: - ddl-auto: update + ddl-auto: none naming: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl diff --git a/common/core/src/main/java/kr/co/palnet/kac/core/exception/AppException.java b/common/core/src/main/java/kr/co/palnet/kac/core/exception/AppException.java index 6a65f5c..5058031 100644 --- a/common/core/src/main/java/kr/co/palnet/kac/core/exception/AppException.java +++ b/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 ExceptionCode errorCode; + private final BaseErrorCode errorCode; private final Object[] paramArray; @@ -24,7 +24,7 @@ public class AppException extends RuntimeException { 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); this.level = level; this.errorCode = errorCode; @@ -34,15 +34,15 @@ public class AppException extends RuntimeException { 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); } - 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); } - public AppException(Throwable th, ExceptionCode errorCode, Object... paramArray) { + public AppException(Throwable th, BaseErrorCode errorCode, Object... 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); } - public AppException(ExceptionCode errorCode, Object... paramArray) { + public AppException(BaseErrorCode errorCode, Object... paramArray) { this(null, Level.WARN, Source.INTERNAL, errorCode, paramArray); } // public BaseException(String errorCode) { // this(errorCode, new Object()); // } - public AppException(Throwable th, ExceptionCode erorCode, String sourceErrorMessage) { - this(th, Level.WARN, ExceptionCode.INTERNAL_SERVER_ERROR, (Object[])null); + public AppException(Throwable th, BaseErrorCode erorCode, String sourceErrorMessage) { + this(th, Level.WARN, BaseErrorCode.INTERNAL_SERVER_ERROR, (Object[])null); this.sourceErrorCode = erorCode.code(); this.sourceErrorMessage = errorCode.message(); } - public AppException(ExceptionCode sourceErrorCode, String sourceErrorMessage) { + public AppException(BaseErrorCode sourceErrorCode, String sourceErrorMessage) { this(null, sourceErrorCode, sourceErrorMessage); } @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); } @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); } @@ -89,7 +89,7 @@ public class AppException extends RuntimeException { return errorCode.code(); } - public ExceptionCode getErrorCode(){ + public BaseErrorCode getErrorCode(){ return errorCode; } @@ -107,14 +107,14 @@ public class AppException extends RuntimeException { public String getErrorMessage(MessageSource ms) { if (ms == null) { - return ExceptionCode.INTERNAL_SERVER_ERROR.message(); + return BaseErrorCode.INTERNAL_SERVER_ERROR.message(); } log.debug("############ getErrorMessage : {}",ms); log.debug("############ getErrorCode : {}",getCode()); 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()); - 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() { diff --git a/common/core/src/main/java/kr/co/palnet/kac/core/exception/ExceptionCode.java b/common/core/src/main/java/kr/co/palnet/kac/core/exception/BaseErrorCode.java similarity index 89% rename from common/core/src/main/java/kr/co/palnet/kac/core/exception/ExceptionCode.java rename to common/core/src/main/java/kr/co/palnet/kac/core/exception/BaseErrorCode.java index 1112755..4846d32 100644 --- a/common/core/src/main/java/kr/co/palnet/kac/core/exception/ExceptionCode.java +++ b/common/core/src/main/java/kr/co/palnet/kac/core/exception/BaseErrorCode.java @@ -1,7 +1,7 @@ package kr.co.palnet.kac.core.exception; -public enum ExceptionCode { +public enum BaseErrorCode { SUCCESS("1", 200, "성공"), FAIL("-1" , 500, "실패"), @@ -20,6 +20,8 @@ public enum ExceptionCode { DB_ERROR("DB001", 500, "디비 처리중 오류"), + NOT_FOUND_RESOURCE("N001", 404, "정의되지 않은 요청입니다."), + TIME_OUT("T001", 400, "인증 시간 초과"), FILE_ERROR("F001", 500, "파일 읽어오기 오류"), @@ -41,7 +43,7 @@ public enum ExceptionCode { private final String message; - private ExceptionCode(String code, int status, String message) { + private BaseErrorCode(String code, int status, String message) { this.code = code; this.status = status; this.message = message; diff --git a/common/core/src/main/java/kr/co/palnet/kac/core/exception/model/BaseErrorModel.java b/common/core/src/main/java/kr/co/palnet/kac/core/exception/model/BaseErrorModel.java index 6d1f7f4..2ff17fd 100644 --- a/common/core/src/main/java/kr/co/palnet/kac/core/exception/model/BaseErrorModel.java +++ b/common/core/src/main/java/kr/co/palnet/kac/core/exception/model/BaseErrorModel.java @@ -5,10 +5,18 @@ import lombok.Data; @Data public class BaseErrorModel { - private String code; + private String timestamp; + + private int status; + + private String error; + + private String path; private String message; - private String desc; +// private String code; +// +// private String desc; } diff --git a/web/api-com/src/main/java/kr/co/palnet/kac/api/v1/com/code/controller/ComCodeController.java b/web/api-com/src/main/java/kr/co/palnet/kac/api/v1/com/code/controller/ComCodeController.java index 11b2411..37446ec 100644 --- a/web/api-com/src/main/java/kr/co/palnet/kac/api/v1/com/code/controller/ComCodeController.java +++ b/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 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 org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; @@ -48,9 +44,6 @@ public class ComCodeController { @GetMapping("/all") @Operation(summary = "전체 코드 조회", description = "전체 코드를 조회합니다.") public ResponseEntity> getAllGroup(SearchCodeAllRQ rq) { - log.warn("ㅇㅇ => {}", rq); - if(true) throw new AppException(ExceptionCode.DB_ERROR); - List allCode = comCodeService.getAllGroup(rq); return ResponseEntity.ok(allCode); }