Skip to content

Commit

Permalink
fix issue Tencent#40, empty stack in window.onerror
Browse files Browse the repository at this point in the history
  • Loading branch information
Maizify committed Jun 20, 2016
1 parent ae5d4af commit 5f866d4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
6 changes: 3 additions & 3 deletions dist/vconsole.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vconsole",
"version": "2.0.1",
"version": "2.0.2-pre",
"description": "A lightweight, extendable front-end developer tool for mobile web page.",
"homepage": "https://github.com/WechatFE/vConsole",
"main": "dist/vconsole.min.js",
Expand Down
26 changes: 26 additions & 0 deletions src/log/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class VConsoleDefaultTab extends VConsoleLogTab {
constructor(...args) {
super(...args);
this.tplTabbox = tplTabbox;
this.windowOnError = null;
}

onReady() {
Expand All @@ -32,6 +33,31 @@ class VConsoleDefaultTab extends VConsoleLogTab {
});
}

/**
* replace window.console & window.onerror with vConsole method
* @private
*/
mockConsole() {
super.mockConsole();
var that = this;
if (tool.isFunction(window.onerror)) {
this.windowOnError = window.onerror;
}
window.onerror = function(message, source, lineNo, colNo, error) {
let msg = message;
if (source) {
msg += "\n" + source.replace(location.origin, '');
}
if (lineNo || colNo) {
msg += ':' + lineNo + ':' + colNo;
}
that.printLog({logType:'error', logs:[msg], noOrigin:true});
if (tool.isFunction(that.windowOnError)) {
that.windowOnError.apply(window, message, source, lineNo, colNo, error);
}
};
}

/**
*
* @private
Expand Down
17 changes: 7 additions & 10 deletions src/log/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class VConsoleLogTab extends VConsolePlugin {
}

/**
* replace window.console & window.onerror with vConsole method
* replace window.console with vConsole method
* @private
*/
mockConsole() {
Expand All @@ -102,13 +102,6 @@ class VConsoleLogTab extends VConsolePlugin {
window.console.warn = function() { that.printLog({logType:'warn', logs:arguments}); };
window.console.debug = function() { that.printLog({logType:'debug', logs:arguments}); };
window.console.error = function() { that.printLog({logType:'error', logs:arguments}); };

window.onerror = function(message, source, lineNo, colNo, error) {
let stack = error.stack.split('at');
stack = stack[0] + ' ' + stack[1];
stack = stack.replace(location.origin, '');
console.error(stack);
};
}

clearLog() {
Expand Down Expand Up @@ -159,7 +152,9 @@ class VConsoleLogTab extends VConsolePlugin {

if (!shouldBeHere) {
// ignore this log and throw it to origin console
this.printOriginLog(item);
if (!item.noOrigin) {
this.printOriginLog(item);
}
return;
}

Expand Down Expand Up @@ -214,7 +209,9 @@ class VConsoleLogTab extends VConsolePlugin {
});

// print log to origin console
this.printOriginLog(item);
if (!item.noOrigin) {
this.printOriginLog(item);
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions test/log.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<a onclick="circularObject()" href="javascript:;" class="weui_btn weui_btn_default">circularObject</a>
<a onclick="circularArray()" href="javascript:;" class="weui_btn weui_btn_default">circularArray</a>
<a onclick="largeObject()" href="javascript:;" class="weui_btn weui_btn_default">largeObject</a>
<a onclick="windowError()" href="javascript:;" class="weui_btn weui_btn_default">window.error</a>
</div>
</body>
</html>
Expand Down Expand Up @@ -79,4 +80,10 @@
console.info('largeObject() End');
}

function windowError() {
console.info('windowError() Start');
a.b = 1;
console.info('windowError() End');
}

</script>

0 comments on commit 5f866d4

Please sign in to comment.