var req;
function postxml(xmldoc) {
if (window.xmlhttprequest) req = new xmlhttprequest();
else if (window.activexobject) req = new activexobject("microsoft.xmlhttp");
else return; // fall on our sword
req.open(method, serveruri);
req.setrequestheader('content-type', 'text/xml');
req.onreadystatechange = xmlposted;
req.send(xmldoc);
}
function xmlposted() {
if (req.readystate != 4) return;
if (req.status == 200) {
var result = req.responsexml;
} else {
// fall on our sword
}
}
function riskybusiness() {
try {
riskyoperation1();
riskyoperation2();
} catch (e) {
// e是错误类型对象
// 至少有两个属性:name及message
} finally {
// 清理工作
}
}
window.onerror = handleerror; // 架起捕获错误的安全网
function handleerror(message, uri, line) {
// 提示用户,该页可能不能正确回应
return true; // 这将终止默认信息
}// 一个类构造方法
function logger() {
// 域
this.req;
// 方法
this.errortoxml = errortoxml;
this.log = log;
}
// 将一个错误映射到xml文档
function errortoxml(err) {
var xml = '<?xml version="1.0"?>\n' +
'<error>\n' +
'<name>' + err.name + '</name>\n' +
'<message>' + err.message + '</message>\n';
if (err.location) xml += '<location>' + err.location + '</location>';
xml += '</error>';
return xml;
}
// logger类的日记方法
function log(err) {
// 嗅探环境
if (window.xmlhttprequest) this.req = new xmlhttprequest();
else if (window.activexobject) this.req =
new activexobject("microsoft.xmlhttp");
else return; // 无功而返
// 确定方式及uri
this.req.open("post", "/cgi-bin/ajaxlogger.cgi");
// 设定request的headers。 如果错误出现在一个所包含的.js文件中,
//referer 这个最顶层的uri可能会与产生错误的地方不一致
this.req.setrequestheader('referer', location.href);
this.req.setrequestheader('content-type', 'text/xml');
// 请求完毕时要调用的函数
this.req.onreadystatechange = errorlogged;
this.req.send(this.errortoxml(err));
// 如果不能在10秒在完成请求,
// 需事务处理
this.timeout = window.settimeout("abortlog();", 10000);
}
// logger只能有一个实例
var logger = new logger();
// 尽管已经尝试,但如果有连接错误,放弃吧
function abortlog() {
logger.req.abort();
alert("attempt to log the error timed out.");
}
// 当request状态有变化则调用
function errorlogged() {
if (logger.req.readystate != 4) return;
window.cleartimeout(logger.timeout);
// 请求完毕
if (logger.req.status >= 400)
alert('attempt to log the error failed.');
}
<script type="text/javascript" src="logger.js"></script>
<script type="text/javascript">
function traperror(msg, uri, ln) {
// 将未知错误包装进一个对象
var error = new error(msg);
error.location = uri + ', line: ' + ln; // 增加定制属性
logger.log(error);
warnuser();
return true; // 避免出现黄色三角形符号
}
window.onerror = traperror;
function foo() {
try {
riskyoperation();
} catch (err) {
//增加定制属性
err.location = location.href + ', function: foo()';
logger.log(err);
warnuser();
}
}
function warnuser() {
alert("an error has occurred while processing this page."+
"our engineers have been alerted!");
// 错误处理
location.href = '/path/to/error/page.html';
}
</script>
use cgi;
use cgi::carp qw(set_progname);
use xml::simple;
my $request = cgi->new();
my $method = $request->request_method();
# method must be post
if ($method eq 'post') {
eval {
my $content_type = $request->content_type();
if ($content_type eq 'text/xml') {
print $request->header(-status =>
'415 unsupported media type', -type => 'text/xml');
croak "invalid content type: $content_type\n";
}
# when method is post and the content type is neither
# uri encoded nor multipart form, the entire post
# is stuffed into one param: postdata
my $error_xml = $request->param('postdata');
my $ref = xml::simple::xmlin($error_xml);
my ($name, $msg, $location) =
($ref->{'name'}, $ref->{'message'}, '');
$location = $ref->{'location'} if (defined($ref->{'location'}));
# this will change the name of the carper in the log
set_progname('client-side error');
my $remote_host = $request->remote_host();
carp "name: [$name], msg: [$msg], location: [$location]";
};
if ($@) {
print $request->header(-status => '500 internal server error',
-type => 'text/xml');
croak "error while logging: $@";
} else {
# this response code indicates that the operation was a
# success, but the client should not expect any content
print $request->header(-status => '204 no content',
-type => 'text/xml');
}
} else {
print $request->header(-status => '405 method not supported',
-type => 'text/xml');
croak "unsupported method: $method";
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器