Skip to content

Commit 08b4436

Browse files
committed
New ErrorParser
1 parent 60a7f55 commit 08b4436

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

error-parser.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// TODO: AMD/CommonJS/etc wrapper
2+
(function() {
3+
function ErrorParser() {
4+
return {
5+
/**
6+
* Given an Error object, return the function that
7+
* will extract the most information from it.
8+
* @param e {Error}
9+
* @return Function parser
10+
*/
11+
chooseParser: function(e) {
12+
if (e['arguments'] && e.stack) {
13+
return this.parseV8;
14+
} else if (e.stack && e.sourceURL) {
15+
return this.parseSAFARI;
16+
} else if (e.stack && e.number) {
17+
return this.parseIE;
18+
} else if (typeof e.message === 'string' && typeof window !== 'undefined' && window.opera) {
19+
return this.parseOPERA;
20+
} else if (e.stack) {
21+
return this.parseFIREFOX;
22+
}
23+
return this.parseCRAPPY;
24+
},
25+
parseV8: function(e) {
26+
var stack = (e.stack + '\n').replace(/^\S[^\(]+?[\n$]/gm, '').
27+
replace(/^\s+(at eval )?at\s+/gm, '').
28+
replace(/^([^\(]+?)([\n$])/gm, '{anonymous}()@$1$2').
29+
replace(/^Object.<anonymous>\s*\(([^\)]+)\)/gm, '{anonymous}()@$1').split('\n');
30+
stack.pop();
31+
return stack;
32+
},
33+
parseSAFARI: function(e) {},
34+
parseIE: function(e) {},
35+
parseFIREFOX: function(e) {},
36+
parseOPERA: function(e) {
37+
// e.message.indexOf("Backtrace:") > -1 -> opera
38+
// !e.stacktrace -> opera
39+
if (!e.stacktrace) {
40+
return 'opera9'; // use e.message
41+
}
42+
// 'opera#sourceloc' in e -> opera9, opera10a
43+
if (e.message.indexOf('\n') > -1 && e.message.split('\n').length > e.stacktrace.split('\n').length) {
44+
return 'opera9'; // use e.message
45+
}
46+
// e.stacktrace && !e.stack -> opera10a
47+
if (!e.stack) {
48+
return 'opera10a'; // use e.stacktrace
49+
}
50+
// e.stacktrace && e.stack -> opera10b
51+
if (e.stacktrace.indexOf("called from line") < 0) {
52+
return 'opera10b'; // use e.stacktrace, format differs from 'opera10a'
53+
}
54+
// e.stacktrace && e.stack -> opera11
55+
return 'opera11'; // use e.stacktrace, format differs from 'opera10a', 'opera10b'
56+
},
57+
parseCRAPPY: function(e) {}
58+
};
59+
}
60+
61+
Error.prototype.parseError = function parseError(e) {
62+
63+
};
64+
65+
window.ErrorParser = ErrorParser;
66+
})();

0 commit comments

Comments
 (0)