X-Internet 연동시 에러처리하기
From JCFWiKi
|
목차 |
[편집] 공통모듈 에러처리 그림
[편집] 특징
- 비지니스 로직에 try catch 를 처리하지 않음 (try catch를 해주지 않아도 에러가 나면 필터에서 Miplatform_error.jsp로 이동함.)
- 에러를 시스템에러, Biz 에러로 나눔
- System에러
- 예상할 수 없거나, 검사할 수 없는 에러.
- 예)무결성에러, 커넥션 획득 실패 등
- 처리방법
- System에러는 에러 메시지를 struts.xml에서 잡아서 간단 정보를 화면에 뿌려줌, 콘솔에도 로그 남김
- 순서 : dao --> service ---> action --> struts.xml -->Miplatform_error.jsp -->화면
- Biz 에러
- 검사할 수 있거나 로직상의 에러
- 계좌 이체 한도 초과 등
- 처리방법
- Biz 에러는 service에서 에러를 검사하여 에러가 발생하면 Exception 클래스에 에러메시지를 보내고 Miplatform_error.jsp에서 그 에러메시를 받아서 화면에 뿌려줌
- 순서 : dao -->service --> Exception -> action -->화면
- System에러
[편집] 에러처리 소스
[편집] 공통적인부분
[편집] struts.xml
1. struts.xml 에서 struts_default를 확장한 jcf-default package를 만들어 에러를 처리할 페이지의 주소를 지정해줌.
- struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.objectFactory" value="spring" /> //struts 설정파일들을 include <include file="config/struts-term.xml"/> <include file="config/struts-code.xml"/> <include file="config/struts-user.xml"/> <include file="config/struts-menu.xml"/> <include file="config/struts-program.xml"/> <include file="config/struts-auth.xml"/> <package name="jcf-default" extends="struts-default"> //struts-default를 확장한 jcf-default라는 package 를 만들어 exception를 처리할 페이지의 주소(miplatform_error.jsp)를 error라는 이름으로 지정합니다.<global-results> <result name="error">/commonJSP/miplatform_error.jsp</result> </global-results><global-exception-mappings><exception-mapping exception="java.lang.Exception" result="error"/></global-exception-mappings> </package> </struts>
[편집] struts 설정파일
2. struts 설정파일에서 jcf-default를 확장한 패키지를 만들어줍니다.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> //jcf-default를 확장한 패키지<package name="/code" extends="jcf-default" namespace="/code"><action name="findCodesOnLoad"class="com.daewoobrenic.com.code.action.CodeAction" method="findCodesOnLoad" />
<action name="saveCodes" class="com.daewoobrenic.com.code.action.CodeAction" method="saveCodes" /> </package> </struts>
[편집] service
3. service 에서 에러를 검사해서 biz exception으로 에러메시지를 보내줍니다.
public class CodeService { public void saveCodes(List changedCodes) { Code code = null; for (int i = 0; i < changedCodes.size(); i++) { code = (Code) changedCodes.get(i); //id가 null일지 확인 if(CodeConstant.CODE_STATUS_INSERT.equalsIgnoreCase(code.getRowStatus()) & code.getId().equalsIgnoreCase("")) { //d가 null이라면 BusinessException에 메시지를 보내줌. throw new BusinessException("id를 입력하지 않았습니다."); }else if(CodeConstant.CODE_STATUS_INSERT.equalsIgnoreCase(code.getRowStatus())) codeDao.insertCode(code);else if(CodeConstant.CODE_STATUS_UPDATE.equalsIgnoreCase(code.getRowStatus())) codeDao.updateCode(code);else if(CodeConstant.CODE_STATUS_DELETE.equalsIgnoreCase(code.getRowStatus())) codeDao.deleteCode(code.getId());else throw new BusinessException("Row Status" + code.getRowStatus() + " is not appropriate.");}
[편집] miplatform에 종속적인 부분
[편집] miplatform_error.jsp
4. miplatform_error.jsp 에 에러를 받아 화면으로 넘기는 로직을 구현합니다.
<%@page contentType="text/html; charset=EUC-KR" %> <%@page import="com.daewoobrenic.jcf.ui.miplatform.MiResponse"%> <%@page import="com.daewoobrenic.jcf.util.ExceptionUtil"%> <%@page import="com.opensymphony.xwork2.interceptor.ExceptionHolder"%> <%@page import="com.opensymphony.xwork2.ActionContext"%> <%@page import="com.opensymphony.xwork2.ActionInvocation"%> <% MiResponse miResponse = new MiResponse(response); try{ // get the Exception from struts2's valuestack. ActionInvocation invocation = ActionContext.getContext().getActionInvocation(); Object obj = invocation.getStack().pop(); ExceptionHolder eh = (ExceptionHolder) obj; Exception e = eh.getException(); // print the error trace message on console. e.printStackTrace(); // print & flush the Exception message caught by exception interceptor throught response. //struts2's valuestack.에서 Exception을 받아 miResponse에 "-1"이라는 errorCode 값으로 넘겨줍니다. miResponse.error("-1", ExceptionUtil.getRootCause(e).toString()); //위에 에러처리 중 문제가 발생하면 catch해서 miResponse에 넘겨줍니다. }catch(Exception e){ // print the error trace message on console. e.printStackTrace(); // print & flush the Exception message caught by exception interceptor throught response. miResponse.error("-1", "예외 처리중 문제가 발생하였습니다. :" + System.getProperty("line.separator ") + ExceptionUtil.getRootCause(e).toString()); } %>
[편집] Miplatform 화면소스
5. Miplatform의 trancallback 메소드에서 에러를 받아 화면에 뿌려줍니다.
//==============================================================================
// Trancallback 함수 처리
//==============================================================================
var out_var;
function trancallback( trid, ErrorCode, ErrorMsg) {
if( trid == "save" ) {
if( ErrorCode < 0 )
alert("저장 실패 : " + ErrorMsg); // 실패시 Messasge처리
else
postSave();
}
}
