Skip to content

Commit b09d766

Browse files
committed
Merge pull request #537 from getsentry/no-undefined-name
Attempt to parse error name when no 5th error arg (fixes #535)
2 parents f34d0ea + 31495ec commit b09d766

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

test/vendor/tracekit.test.js

+51-2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ describe('TraceKit', function(){
106106

107107
describe('error notifications', function(){
108108
var testMessage = "__mocha_ignore__";
109+
var testLineNo = 1337;
110+
109111
var subscriptionHandler;
110112
// TraceKit waits 2000ms for window.onerror to fire, so give the tests
111113
// some extra time.
@@ -119,8 +121,8 @@ describe('TraceKit', function(){
119121
// we can't do that without clobbering TraceKit's handler, which can only
120122
// be installed once.
121123
var oldOnError = window.onerror;
122-
window.onerror = function(message) {
123-
if (message == testMessage) {
124+
window.onerror = function(message, url, lineNo) {
125+
if (message == testMessage || lineNo === testLineNo) {
124126
return true;
125127
}
126128
return oldOnError.apply(this, arguments);
@@ -134,6 +136,53 @@ describe('TraceKit', function(){
134136
}
135137
});
136138

139+
describe('with undefined arguments', function () {
140+
it('should pass undefined:undefined', function () {
141+
// this is probably not good behavior; just writing this test to verify
142+
// that it doesn't change unintentionally
143+
subscriptionHandler = function (stackInfo, extra) {
144+
assert.equal(stackInfo.name, undefined);
145+
assert.equal(stackInfo.message, undefined);
146+
};
147+
TraceKit.report.subscribe(subscriptionHandler);
148+
window.onerror(undefined, undefined, testLineNo);
149+
});
150+
});
151+
describe('when no 5th argument (error object)', function () {
152+
it('should seperate name, message for default error types (e.g. ReferenceError)', function (done) {
153+
subscriptionHandler = function (stackInfo, extra) {
154+
assert.equal(stackInfo.name, 'ReferenceError');
155+
assert.equal(stackInfo.message, 'foo is undefined');
156+
};
157+
TraceKit.report.subscribe(subscriptionHandler);
158+
// should work with/without "Uncaught"
159+
window.onerror('Uncaught ReferenceError: foo is undefined', 'http://example.com', testLineNo);
160+
window.onerror('ReferenceError: foo is undefined', 'http://example.com', testLineNo)
161+
done();
162+
});
163+
164+
it('should ignore unknown error types', function (done) {
165+
// TODO: should we attempt to parse this?
166+
subscriptionHandler = function (stackInfo, extra) {
167+
assert.equal(stackInfo.name, undefined);
168+
assert.equal(stackInfo.message, 'CustomError: woo scary');
169+
done();
170+
};
171+
TraceKit.report.subscribe(subscriptionHandler);
172+
window.onerror('CustomError: woo scary', 'http://example.com', testLineNo);
173+
});
174+
175+
it('should ignore arbitrary messages passed through onerror', function (done) {
176+
subscriptionHandler = function (stackInfo, extra) {
177+
assert.equal(stackInfo.name, undefined);
178+
assert.equal(stackInfo.message, 'all work and no play makes homer: something something');
179+
done();
180+
};
181+
TraceKit.report.subscribe(subscriptionHandler);
182+
window.onerror('all work and no play makes homer: something something', 'http://example.com', testLineNo);
183+
});
184+
});
185+
137186
function testErrorNotification(collectWindowErrors, callOnError, numReports, done) {
138187
var extraVal = "foo";
139188
var numDone = 0;

vendor/TraceKit/tracekit.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ var TraceKit = {
2323
var _slice = [].slice;
2424
var UNKNOWN_FUNCTION = '?';
2525

26+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types
27+
var ERROR_TYPES_RE = /^(?:Uncaught )?((?:Eval|Internal|Range|Reference|Syntax|Type|URI)Error)\: ?(.*)$/;
28+
2629
function getLocationHref() {
2730
if (typeof document === 'undefined')
2831
return '';
@@ -161,8 +164,21 @@ TraceKit.report = (function reportModuleWrapper() {
161164
};
162165
location.func = TraceKit.computeStackTrace.guessFunctionName(location.url, location.line);
163166
location.context = TraceKit.computeStackTrace.gatherContext(location.url, location.line);
167+
168+
var name = undefined;
169+
var msg = message; // must be new var or will modify original `arguments`
170+
var groups;
171+
if (isString(message)) {
172+
var groups = message.match(ERROR_TYPES_RE);
173+
if (groups) {
174+
name = groups[1];
175+
msg = groups[2];
176+
}
177+
}
178+
164179
stack = {
165-
'message': message,
180+
'name': name,
181+
'message': msg,
166182
'url': getLocationHref(),
167183
'stack': [location]
168184
};

0 commit comments

Comments
 (0)