예외 발생 시 nexacro platform으로 stack trace 전송여부 옵션 처리

This commit is contained in:
ParkSeongMin
2015-10-22 02:03:44 +00:00
parent 2cebf5177f
commit 33fe8be432
3 changed files with 95 additions and 20 deletions

View File

@@ -3,9 +3,8 @@ package com.nexacro.spring;
/** /**
* *
* <p>Nexacro 예외를 의미하는 <code>Exception</code>이다. * <p>Nexacro 예외를 의미하는 <code>Exception</code>이다.
* 추가적으로 설정되는 에러코드(errorCode) 에러메시지(errorMsg)는 nexacro platform으로 전송되는 데이터이다.
* *
* @ClassName : NexacroException.java
* @Description : 클래스 설명을 기술합니다.
* @author Park SeongMin * @author Park SeongMin
* @since 2015. 7. 30. * @since 2015. 7. 30.
* @version 1.0 * @version 1.0
@@ -38,8 +37,7 @@ public class NexacroException extends Exception {
/** /**
* 메시지를 가지는 생성자이다. * 메시지를 가지는 생성자이다.
* *
* @param message * @param message 메시지
* 메시지
*/ */
public NexacroException(String message) { public NexacroException(String message) {
this(message, null); this(message, null);
@@ -48,28 +46,67 @@ public class NexacroException extends Exception {
/** /**
* 메시지와 원천(cause) 예외를 가지는 생성자이다. * 메시지와 원천(cause) 예외를 가지는 생성자이다.
* *
* @param message * @param message 메시지
* 메시지 * @param cause 원천 예외
* @param cause
* 원천 예외
*/ */
public NexacroException(String message, Throwable cause) { public NexacroException(String message, Throwable cause) {
super(message, cause); super(message, cause);
this.errorMsg = message;
} }
/**
* 메시지와 에러코드, 에러메시지를 가지는 생성자이다.
*
* @param message 메시지
* @param errorCode 에러코드
* @param errorMsg 에러메시지
*/
public NexacroException(String message, int errorCode, String errorMsg) {
this(message, null, errorCode, errorMsg);
}
/**
* 메시지와 원천(cause), 에러코드, 에러메시지 예외를 가지는 생성자이다.
*
* @param message 메시지
* @param cause 원천 예외
* @param errorCode 에러코드
* @param errorMsg 에러메시지
*/
public NexacroException(String message, Throwable cause, int errorCode, String errorMsg) {
super(message, cause);
setErrorCode(errorCode);
setErrorMsg(errorMsg);
}
/**
* 설정 된 에러코드를 반환한다.
* @return errorCode
*/
public int getErrorCode() { public int getErrorCode() {
return errorCode; return errorCode;
} }
/**
* 에러코드를 설정한다.
*
* @param errorCode 에러코드
*/
public void setErrorCode(int errorCode) { public void setErrorCode(int errorCode) {
this.errorCode = errorCode; this.errorCode = errorCode;
} }
/**
* 설정 된 에러메시지를 반환한다.
* @return errorMsg
*/
public String getErrorMsg() { public String getErrorMsg() {
return errorMsg; return errorMsg;
} }
/**
* 에러메시지를 설정한다.
* @param errorMsg 에러메시지
*/
public void setErrorMsg(String errorMsg) { public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg; this.errorMsg = errorMsg;
} }

View File

@@ -14,9 +14,13 @@ import com.nexacro.spring.util.NexacroUtil;
import com.nexacro.spring.view.NexacroModelAndView; import com.nexacro.spring.view.NexacroModelAndView;
import com.nexacro.spring.view.NexacroView; import com.nexacro.spring.view.NexacroView;
public class NexcroMappingExceptionResolver extends AbstractHandlerExceptionResolver { public class NexacroMappingExceptionResolver extends AbstractHandlerExceptionResolver {
private final Logger logger = LoggerFactory.getLogger(NexcroMappingExceptionResolver.class); private final Logger logger = LoggerFactory.getLogger(NexacroMappingExceptionResolver.class);
private String defaultErrorMsg = NexacroException.DEFAULT_MESSAGE;
private boolean shouldSendStackTrace = false;
private boolean shouldLogStackTrace = false;
private View view; private View view;
@@ -32,7 +36,19 @@ public class NexcroMappingExceptionResolver extends AbstractHandlerExceptionReso
this.view = view; this.view = view;
} }
public NexcroMappingExceptionResolver() { public void setDefaultErrorMsg(String defaultErrorMsg) {
this.defaultErrorMsg = defaultErrorMsg;
}
public void setShouldSendStackTrace(boolean shouldSendStackTrace) {
this.shouldSendStackTrace = shouldSendStackTrace;
}
public void setShouldLogStackTrace(boolean shouldLogStackTrace) {
this.shouldLogStackTrace = shouldLogStackTrace;
}
public NexacroMappingExceptionResolver() {
} }
@Override @Override
@@ -50,12 +66,12 @@ public class NexcroMappingExceptionResolver extends AbstractHandlerExceptionReso
if(ex instanceof NexacroException){ // NexacroConvertException if(ex instanceof NexacroException){ // NexacroConvertException
NexacroException nexaExp = (NexacroException) ex; NexacroException nexaExp = (NexacroException) ex;
mav.setErrorCode(nexaExp.getErrorCode()); mav.setErrorCode(nexaExp.getErrorCode());
mav.setErrorMsg(nexaExp.getErrorMsg() != null? nexaExp.getErrorMsg(): nexaExp.toString()); mav.setErrorMsg(getExceptionMessage(ex));
} else { } else {
// PlatformException.. // PlatformException..
mav.setErrorCode(NexacroException.DEFAULT_ERROR_CODE); //Undefined error Code mav.setErrorCode(NexacroException.DEFAULT_ERROR_CODE); //Undefined error Code
// mav.setErrorMsg(NexacroException.DEFAULT_MESSAGE); // mav.setErrorMsg(NexacroException.DEFAULT_MESSAGE);
mav.setErrorMsg(ex.toString()); mav.setErrorMsg(getExceptionMessage(ex));
} }
return mav; return mav;
@@ -66,12 +82,34 @@ public class NexcroMappingExceptionResolver extends AbstractHandlerExceptionReso
private void prepareResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { private void prepareResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Resolving exception from handler [" + handler + "]: " + ex);
}
logException(ex, request); logException(ex, request);
if(this.shouldLogStackTrace) {
logger.error(ex.getMessage(), ex);
} }
} }
private String getExceptionMessage(Exception e) {
if(this.shouldSendStackTrace) {
String message = e.getMessage();
if(e instanceof NexacroException) {
String errorMsg = ((NexacroException) e).getErrorMsg();
if(errorMsg != null) {
message = "errorMsg="+ errorMsg +", stackMessage=" +message;
}
}
return message;
} else {
return this.defaultErrorMsg;
}
}
}

View File

@@ -239,7 +239,7 @@ public class NexacroMethodArgumentResolver implements HandlerMethodArgumentResol
DataSet dataSet = nexacroCachedData.getPlatformData().getDataSet(dsName); DataSet dataSet = nexacroCachedData.getPlatformData().getDataSet(dsName);
if(dataSet == null) { if(dataSet == null) {
if(logger.isDebugEnabled()) { if(logger.isDebugEnabled()) {
logger.debug(dsName + " is null."); logger.debug("@ParamDataSet '" + dsName + "' argument is null.");
} }
return null; return null;
} }
@@ -291,7 +291,7 @@ public class NexacroMethodArgumentResolver implements HandlerMethodArgumentResol
Variable variable = nexacroCachedData.getPlatformData().getVariable(varName); Variable variable = nexacroCachedData.getPlatformData().getVariable(varName);
if(variable == null) { if(variable == null) {
if(logger.isDebugEnabled()) { if(logger.isDebugEnabled()) {
logger.debug(varName + " is null."); logger.debug("@ParamVariable '" + varName + "' argument is null.");
} }
return null; return null;
//throw new IllegalArgumentException("invalid @ParamVariable. ex)@ParamVariable(name=\"variableName\") Object var"); //throw new IllegalArgumentException("invalid @ParamVariable. ex)@ParamVariable(name=\"variableName\") Object var");