异常处理机制

---恢复内容开始---

1.流程介绍

         首先介绍一下异常错误处理机制的一个简单的真实项目中的流程,切切实实感觉到快速定位问题,快速处理问题。

         

         页面加载报错,弹出友好的提示框供用户选择。

     2.点击错误报告

         

       

         可以看出这个异常,明显的可以定位出问题,然后去解决问题。

         那么问题来了,这个开发人员很好懂,但是真实的用户如何知道怎么操作呢?

        1.首先根据提示,用户可以提交错误报告,也可以不提交错误报告。

        2.这个并不影响,但是如果用户操作了,将快速定位到问题,解决问题。

        如果提交了将大大提高开发人员异常处理的效率。

   3.提交错误报告成功 

        

2.思路讲解

   1. 前台发起请求,后台进行业务处理,但是业务处理的时候进行异常捕捉然后进行处理。

   2.后台业务处理主类返回给前台错误信息,然后前台进行显示。

   后台捕获样例代码:

   

/** 下载数据 查看该用户所有的房间申请记录 **/
    @ResponseBody
    @RequestMapping(value = "listEntranceOpenLog")
    public String listEntranceOpenLog(HttpSession session, Model model,
            String userToken,String findKey) {
        CommWriteJson cjson = new CommWriteJson();
        EntranceOpenLogModel entranceOpenLogModel = null;
        cjson.beginJson();
        try {
            /* 更换用户令牌 */
            cjson.appendCloumn("userToken",
                    UserToken.updateUserToken(session, userToken));

            entranceOpenLogModel = new EntranceOpenLogModel();
            boolean isData;
            isData = entranceOpenLogModel.listEntranceOpenLog(findKey);
            cjson.appendCompleteJson();
            if (isData) {
                // /** 获取jsonarray数据值 **/
                cjson.appendAarrayDataCloumn("data", entranceOpenLogModel
                        .getEntranceOpenLogData().getJsonArray());
                cjson.appendCloumn("datacount", Integer.toString(entranceOpenLogModel
                        .getEntranceOpenLogData().getJsonArrayCount()));
            } else {
                cjson.appendCloumn("datacount", "0");
            }
//异常信息返回核心处理类 }
catch (CommExceptionPostBack ex) {
//拼接异常信息 cjson.appendErrorJson(ex); }
catch (Exception e) {
//拼接异常信息 cjson.appendExceptionJson(e); }
finally { if (entranceOpenLogModel != null) { entranceOpenLogModel.finalize(); } cjson.endJson(); } return cjson.getJson(); }

    前台接收错误信息方法

   

$.confirm({
                                    title : '错误!',
                                    type : 'red',
                                    backgroundDismiss : true,
                                    content : jsondata.errmsg,
                                    buttons : {
                                        确定 : function() 
                                        {
                                            return true;
                                        },
                                        错误报告 : function() 
                                        {
                                            openDebugPage(jsondata);
                                        }
                                    }
                                });
function openDebugPage(jsonData,pageTree)
{
    if (!DEBUG_PAGE_SHOW)
        {
            alert('gr.收集错误报告服务已经关闭,请联系管理员.');
            return false;
        }
    debug_page_jsondata=jsonData;
    var pageurl="";
    if (pageTree!=null)
        {
            debug_page_grade=pageTree;
        }
    else
        {
            debug_page_grade=3;//默认为3级报告.
        }
//错误信息加载页面 pageurl="../../commdebug/jsondebug/jsondebug.jsp"; layer_showEx('错误报告', pageurl); }

3.设计思路

定制化异常错误提示和等级

//系统异常
hash_code.put("0xa00089","Excel文件类I/O写入错误"); hash_code.put("0xa00091","非 Unicode字符集保存错误"); hash_code.put("0xa00092","文件I/O类错误"); hash_code.put("0xa00093","错误的日期时间格式"); hash_code.put("0xa00094","客户端传输文件时发生超时异常"); hash_code.put("0xa00095","有必需的参数值未传递或为零值");

//拼接异常信息

/** 写入json基本错误值(包含系统错误代码) */
public void appendErrorJson(CommExceptionPostBack e)
{
appendCloumn("errcode",e.getCommExceptionCode());
//如果附加错误 信息不为空 则跟在提示信息后面
if (e.getExceptionCustomMsg()!=null)
{
appendCloumn("errmsg",CommException.getCode(e.getCommExceptionCode())+" ["+e.getExceptionCustomMsg()+"]");
}
else
{
appendCloumn("errmsg",CommException.getCode(e.getCommExceptionCode()));
}
//这是用户自己指定的错误码
appendCloumn("errmsgcode",e.getExceptionCustomCode());
appendCloumn("errmsgdebug",e.getExceptionMsgDebug());
appendCloumn("errmsgsystem",e.getExceptionMsgSystem());

}

      

---恢复内容结束---

原文地址:https://www.cnblogs.com/fupengpeng/p/10507354.html