캐모마일 예외처리 가이드
개요
캐모마일 프레임워크에서 예외처리 방법에 대해 설명한다.
캐모마일 내부적 모든 모듈에서는 ChamomileCode라는 Enum 코드와 ChamomileException을 기반으로 Exception을 throw한다.
public <T> List<T> toCustomClass(String path, Class<T> clazz) throws ChamomileExcelException;
public <T> List<T> toCustomClass(String path, int sheetNum, Class<T> clazz) throws ChamomileExcelException;
개발 방법
비즈니스 개발자는 커스텀 예외 처리를 위해 아래와 같이 net.lotte.chamomile.core.exception.BusinessCode 상속 받아 구현한다.
import net.lotte.chamomile.core.exception.BusinessCode
public enum CustomCode implements BusinessCode {
// 사용할 ENUM 리스트 정의
LOGIN_ERROR(501, "Business Login error");
FILE_ERROR(502, "Business File error");
MENU_NOT_VISIBLE(1502, "Menu Not Visible");
private final int code;
private final String codeDescription;
CustomCode(int code, String codeDescription) {
this.code = code;
this.codeDescription = codeDescription;
}
@Override
public int getCode() {
return this.code;
}
@Override
public final String toString() {
return this.codeDescription;
}
}
위와 같이 구현한 Enum 코드를 기반으로 BusinessException을 이용하여 아래와 같이 throw 한다
throw CustomCode.LOGIN_ERROR.exception(); // LOGIN_ERROR 코드 기반으로 exception
throw CustomCode.FILE_ERROR.exception(); // FILE_ERROR 코드 기반으로 exception
상황에 따라 아래와 같이 메서드를 호출할 수 있다.
CustomCode.LOGIN_ERROR.exception(); // 일반 exception throw
CustomCode.LOGIN_ERROR.exception(String customMessage); // custom message
CustomCode.LOGIN_ERROR.exception(String customMessage, Throwable t);
// custom message와 다른 Exception을 파라미터로 받을때.
이렇게 예외를 날린 경우 chamomile-web 모듈 사용시 global excepton handler에서 catch하여 ChamomileErrorLogger로 에러 로깅 처리 후 아래와 같이 json으로 리턴한다.
// 아래의 예외를 날렸을 때 예시
throw CustomCode.LOGIN_ERROR.exception();
{
code: 501,
message: "Business Login error",
data:""
}