Ajax的错误处理机制探讨(2)

来源:yesky 时间:2007-09-07 09:53:00 

最后的两个函数只是用于琐碎事务管理的。如果在记录错误的时候出现了问题,除了干扰用户之外,我们几乎不能做任务事务。但是,这种情况永远不会出现。这些不是类的方法,因为事件没有指向我们的对象的指针,但是它会指向我们建立的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.’);
}


  前面的所有代码都被包装到一个.js文件中了,我们可以在站点的任何(或每一个)页面中包含这个文件。下面是如何包含这个文件的例子:


<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>


  现在你已经知道如何把日志记录器集成到HTML页面中了,剩余的工作就是定义一种接收和转换消息的方法了。我选择使用最底层的通用命名方法,在Perl中建立了一个CGI脚本,这个脚本使用了我喜欢的一些模块,它使用XML::Simple来分析post数据,使用CGI::Carp把结果直接导入到httpd错误日志,这样可以节约系统管理员的时间,因为他不需要查看另外一个日志了。这个脚本还包含了很多良好的示例,它们适当地记录了不同的成功和失败条件。


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";
}


  已经完成了!现在,当某些难以理解的JavaScript进入系统的时候,你就可以期待着自己的日志监视器开始闪红灯,你的客户端开发人员在深夜接到电话了。

标签:Ajax,错误
0
投稿

猜你喜欢

  • JavaScript中clientWidth,offsetWidth,scrollWidth的区别

    2024-04-22 22:24:59
  • ProC 连接Oracle代码

    2009-06-10 18:12:00
  • 用python对oracle进行简单性能测试

    2021-07-08 16:51:59
  • javascript中解析四则运算表达式的算法和示例

    2024-04-28 09:41:37
  • Python实现随机森林回归与各自变量重要性分析与排序

    2023-05-04 05:52:21
  • Python获取、格式化当前时间日期的方法

    2021-05-27 17:34:06
  • vscode 一键规范代码格式的实现

    2022-01-14 17:24:53
  • SQL Server 查询分析器快捷键集合

    2007-08-17 09:42:00
  • 详解tensorflow之过拟合问题实战

    2022-08-13 00:46:30
  • 浅谈常用Java数据库连接池(小结)

    2024-01-18 06:50:25
  • Python爬虫破解登陆哔哩哔哩的方法

    2021-03-28 14:17:40
  • 解决MySQL5.1安装时出现Cannot create windows service for mysql.error:0

    2024-01-28 09:56:13
  • Python 数据分析之Beautiful Soup 提取页面信息

    2022-04-30 04:34:10
  • 详解如何在Javascript中使用Object.freeze()

    2024-04-10 16:10:17
  • Python实战之实现简单的名片管理系统

    2023-07-18 06:48:02
  • asp文章中随机插入网站版权文字的实现代码

    2011-04-15 11:11:00
  • python数字图像处理环境安装与配置过程示例

    2023-03-05 07:00:25
  • 基于webstorm卡顿问题的2种解决方法

    2023-02-23 09:03:17
  • asp.net 为FCKeditor开发代码高亮插件实现代码

    2023-09-26 00:30:16
  • Python把csv数据写入list和字典类型的变量脚本方法

    2021-05-27 22:04:20
  • asp之家 网络编程 m.aspxhome.com