客户端的JavaScript错误记录 [英] Error-logging for javascript on client side

查看:107
本文介绍了客户端的JavaScript错误记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目包含大量带有表单的页面。这是银行CRM系统的后台,所以在工作流程中任何错误都要被捕获和调查。
在服务器端,我们增强了java异常系统,但是如果客户端发生错误,那么我们现在获得的唯一信息就是IE中的js-error窗口,有时候是高级用户页面的截图。 / p>

Javascript代码包含Jquery功能的UI扩展和硬编码的内联事件处理函数和函数。



所以我问是否可以使用任何捕获js错误的方法?
一些额外的图书馆或者可以给我一个Mozilla或Chrome的网页控制台中的firebug的堆栈跟踪?

解决方案

查看 window.onerror 。如果要捕获任何错误,并将其报告给服务器,那么您可以尝试使用AJAX请求。

 窗口.onerror = function(errorMessage,errorUrl,errorLine){
jQuery.ajax({
type:'POST',
url:'jserror.jsf',
data:{
msg:errorMessage,
url:errorUrl,
line:errorLine
},
success:function(){
if(console&& console.log){
console.log('JS error report successful。');
}
},
error:function(){
if(console && console.error){
console.error('JS错误报告提交失败!');
}
}
});

//防止触发默认错误处理程序。
返回true;
}


My project which contains a lot of pages with forms. This is a backend of banking CRM system, so any error during working process is to be captured and investigated. On the server side we have enhanced java exceptions system, but if error occurs on client side - javascript the only info we now get is an js-error window in IE or sometimes a screenshot of page made by advanced user.

Javascript code contains both Jquery-powered UI extensions and hard-coded inline event handlers and functions.

So I am asking whether any approach for capturing js-errors of any kind could be used? some additional library or something that could give me a stacktrace like firebug in Mozilla or web-console in Chrome?

解决方案

Look into window.onerror. If you want to capture any errors, and report them to the server, then you could try an AJAX request, perhaps.

window.onerror = function(errorMessage, errorUrl, errorLine) {
    jQuery.ajax({
        type: 'POST',
        url: 'jserror.jsf',
        data: {
            msg: errorMessage,
            url: errorUrl,
            line: errorLine
        },
        success: function() {
            if (console && console.log) {
                console.log('JS error report successful.');
            }
        },
        error: function() {
            if (console && console.error) {
                console.error('JS error report submission failed!');
            }
        }
    });

    // Prevent firing of default error handler.
    return true;
}

这篇关于客户端的JavaScript错误记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆