如果是ajax请求在做web层时,把返回的jsp变成json格式或者流格式输出即可,不影响异常框架。如采用struts2结构的代码:

public String testAjax()
{
    try
    {
         genAjaxDataStr(0, "{}");
    } catch (BizException e)
    {
         getRequest().setAttribute(this.ERRORMESSAGE, e.getErrorMessage());
         return this.ERRORJSON;
    } catch (BizSystemException e)
    {
         getRequest().setAttribute(this.ERRORMESSAGE, this.SYSTEMERROR);
         return this.ERRORJSON;
    } catch (Exception e)
    {
         this.errorTrace("test", e.getMessage(), e);
         getRequest().setAttribute(this.ERRORMESSAGE, this.SYSTEMERROR);
         return this.ERRORJSON;
    }
    return this.NONE;
}

  这样前台能根据我们的异常显示对应的错误页面了,并能把系统知道的和未知的异常记入log。

  针对struts2还有个问题,在开发模式时,struts2和webwork的异常打印在页面,我们可以根据页面输出进行调试。一但部署在生产环境,需要将这个模式关闭,log没有了。

<constant name="struts.devMode" value="false" />

  为了记录一些未知的错误,需要做以下步骤:

  1、将全局的异常映射页面从struts2的包定义里去掉。如果不去掉,在webwork不会抛出异常也找不到出了什么问题。

  2、扩展struts的DispatcherFilter捕捉未知的异常并记录入log。

<global-exception-mappings>
    <exception-mapping exception="com.linktong.sdk.biz.exception.BizException"
                result="checkedException" />
</global-exception-mappings>

  这样,基本的异常框架搭建完成。更进一步需要做的是:

  1、分布式全局错误码体系,保证所有机器都共用一套错误码。

  2、分布式部署,异常传递。可以采用hessian序列化错误码的机制,不用传输整个异常链节省带宽。

  3、集中logger服务处理,所有机器的log统一发送到集中服务器处理。logger框架和log4j也有服务器的机制。

  针对web框架、分布式部署、log服务器再讨论:)