var req;
function postXML(xmlDoc) {
if (window.XMLHttpRequest) req = new XMLHttpRequest();
else if (window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");
else return; // 失败了
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 {
// 失败了
}
}
function riskyBusiness() {
try {
riskyOperation1();
riskyOperation2();
} catch (e) {
// e是一个Error类型的对象,至少有两个属性: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;
}
// 日志记录类的log方法
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");
// 设置请求头信息。REFERER 是顶层URI,如果它发生在一个包含的.js文件中
// 那么它的位置与错误的位置可能不同
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);
}
// 只有一个日志记录器实例
var logger = new Logger();
// 我们试过了,但是连接错误,没有希望了
function abortLog() {
logger.req.abort();
alert("Attempt to log the error timed out.");
}
// 请求的状态发生改变的时候调用
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();
# 方法必须是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";
}
# 如果方法是POST,内容既不是URL编码也不是多部分形式,
#那么整个post会被填充到一个参数中: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’}));
# 改变日志中的名字
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 {
# 这部分响应代码表明操作成功了,但是客户端不应该期待任何内容
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";
}