te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>webkit - How do I capture JavaScript errors generated in a page fetched by PhantomJS? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

webkit - How do I capture JavaScript errors generated in a page fetched by PhantomJS? - Stack Overflow

programmeradmin3浏览0评论

I have a PhantomJS script that loads a local HTML file, injects some javascript files, then executes some javascript in the context of the page. The javascript that runs generates an exception, but I only get output from the console, which doesn't seem to distinguish between an error and a normal log and doesn't have file, line numbers or a stacktrace.

What I need is a way to capture or otherwise distinguish these errors. I have already tried:

  • Wrapping my PhantomJS script in a try-catch
    • Result: nothing is thrown far enough to be caught by this
  • Define a window.onerror function
    • Result: nothing happens. WebKit does not implement an onerror event on the window

I would prefer to be able to retrieve the error object itself so that I can retrieve the stacktrace.

I have a PhantomJS script that loads a local HTML file, injects some javascript files, then executes some javascript in the context of the page. The javascript that runs generates an exception, but I only get output from the console, which doesn't seem to distinguish between an error and a normal log and doesn't have file, line numbers or a stacktrace.

What I need is a way to capture or otherwise distinguish these errors. I have already tried:

  • Wrapping my PhantomJS script in a try-catch
    • Result: nothing is thrown far enough to be caught by this
  • Define a window.onerror function
    • Result: nothing happens. WebKit does not implement an onerror event on the window

I would prefer to be able to retrieve the error object itself so that I can retrieve the stacktrace.

Share Improve this question edited Jan 15, 2012 at 0:08 James van Dyke asked Sep 21, 2011 at 15:59 James van DykeJames van Dyke 5,0703 gold badges30 silver badges25 bronze badges 1
  • I guess you mean defining a window.onerror function, right? – Julian D. Commented Dec 29, 2011 at 16:38
Add a ment  | 

2 Answers 2

Reset to default 8

I think there were issues with window.onerror not properly working in WebKit (https://bugs.webkit/show_bug.cgi?id=8519). Don't know if this has been fixed at all, and if so, if the QT WebKit version is already up-to-date.

However, you should be able to catch the exceptions thrown in your code. If you are using something like webPage.evaluate(...)to run your code, you cannot wrap the plete call in a try/catch block, since the script is evaluated in a different context and the errors will not appear in the main execution context. Insteadyou will need to catch the errors in page execution context. Unfortunately, there is no way of accessing any functions defined in the main context, we therefore have to explicitly write the wrapping code around your code to be executed.

The following is a modified example of the phantomwebintro.js file as included in the PhantomJS source. It loads an HTML page, inserts a script and then runs some code in the page context (here with a line throwing a type error). This code is wrapped with a try/catch block and will return the wrapped result or error object to the main context.

...

// Load an HTML page:
page.open("http://www.phantomjs", function(status) {
    if (status == "success") {

        // Inject some scripts:
        page.includeJs("http://ajax.googleapis./ajax/libs/jquery/1.6.1/jquery.min.js", function() {

            // Run your own code in the loaded page context:
            var resultWrapper = page.evaluate(function() {
                var wrapper = {};
                try {
                    // Your code goes here
                    // ...

                    var x = undefined.x; // force an error

                    // store your return values in the wrapper
                    wrapper.result = 42;
                } catch(error) {
                    wrapper.error = error;
                }
                return wrapper;
            });

            // Handle the result and possible errors:
            if (resultWrapper.error) {
                var error = resultWrapper.error;
                console.log("An error occurred: " + error.message);
                // continue handling the error
                // ...
            } else {
                var result = resultWrapper.result;
                // continue using the returned result
                // ...
            }

            ...

        });
    }
});

...

The solution? return true!

      // Error tracking
      page.onError = function(msg, trace) {
          console.log('= onError()');
          var msgStack = ['  ERROR: ' + msg];
          if (trace) {
            msgStack.push('  TRACE:');
            trace.forEach(function(t) {
              msgStack.push('    -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
            }); 
          }   
          console.log(msgStack.join('\n'));

          // Consider error fully handled
          return true;
      };
发布评论

评论列表(0)

  1. 暂无评论