Skip to content

Commit 7619d2d

Browse files
committed
Strict error check using isError, fix regexes
1 parent 2710ccc commit 7619d2d

File tree

5 files changed

+69
-16
lines changed

5 files changed

+69
-16
lines changed

src/raven.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
var TraceKit = require('../vendor/TraceKit/tracekit');
55
var RavenConfigError = require('./configError');
6+
var utils = require('./utils');
7+
8+
var isError = utils.isError,
9+
isObject = utils.isObject;
10+
611
var stringify = require('json-stringify-safe');
712

813
var wrapConsoleMethod = require('./console').wrapMethod;
@@ -1655,25 +1660,12 @@ function isString(what) {
16551660
return objectPrototype.toString.call(what) === '[object String]';
16561661
}
16571662

1658-
function isObject(what) {
1659-
return typeof what === 'object' && what !== null;
1660-
}
16611663

16621664
function isEmptyObject(what) {
16631665
for (var _ in what) return false; // eslint-disable-line guard-for-in, no-unused-vars
16641666
return true;
16651667
}
16661668

1667-
// Sorta yanked from https://github.com/joyent/node/blob/aa3b4b4/lib/util.js#L560
1668-
// with some tiny modifications
1669-
function isError(what) {
1670-
var toString = objectPrototype.toString.call(what);
1671-
return isObject(what) &&
1672-
toString === '[object Error]' ||
1673-
toString === '[object Exception]' || // Firefox NS_ERROR_FAILURE Exceptions
1674-
what instanceof Error;
1675-
}
1676-
16771669
function each(obj, callback) {
16781670
var i, j;
16791671

src/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
function isObject(what) {
4+
return typeof what === 'object' && what !== null;
5+
}
6+
7+
// Sorta yanked from https://github.com/joyent/node/blob/aa3b4b4/lib/util.js#L560
8+
// with some tiny modifications
9+
function isError(what) {
10+
var toString = {}.toString.call(what);
11+
return isObject(what) &&
12+
toString === '[object Error]' ||
13+
toString === '[object Exception]' || // Firefox NS_ERROR_FAILURE Exceptions
14+
what instanceof Error;
15+
}
16+
17+
module.exports = {
18+
isObject: isObject,
19+
isError: isError
20+
};

test/integration/test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,39 @@ describe('integration', function () {
275275
var ravenData = iframe.contentWindow.ravenData[0];
276276
assert.match(ravenData.exception.values[0].value, /stringError$/);
277277
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, 1); // always 1 because thrown strings can't provide > 1 frame
278-
assert.match(ravenData.exception.values[0].stacktrace.frames[0].filename, /\/test\/integration\/throw-string\.js/)
278+
279+
// some browsers extract proper url, line, and column for thrown strings
280+
// but not all - falls back to frame url
281+
assert.match(ravenData.exception.values[0].stacktrace.frames[0].filename, /\/test\/integration\//);
282+
assert.match(ravenData.exception.values[0].stacktrace.frames[0]['function'], /\?|global code/);
283+
}
284+
);
285+
});
286+
287+
it('should catch thrown objects', function (done) {
288+
var iframe = this.iframe;
289+
290+
iframeExecute(iframe, done,
291+
function () {
292+
// intentionally loading this error via a script file to make
293+
// sure it is 1) not caught by instrumentation 2) doesn't trigger
294+
// "Script error"
295+
var script = document.createElement('script');
296+
script.src = 'throw-object.js';
297+
script.onload = function () {
298+
done();
299+
};
300+
document.head.appendChild(script);
301+
},
302+
function () {
303+
var ravenData = iframe.contentWindow.ravenData[0];
304+
assert.equal(ravenData.exception.values[0].type, undefined);
305+
assert.equal(ravenData.exception.values[0].value, '[object Object]');
306+
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, 1); // always 1 because thrown objects can't provide > 1 frame
307+
308+
// some browsers extract proper url, line, and column for thrown objects
309+
// but not all - falls back to frame url
310+
assert.match(ravenData.exception.values[0].stacktrace.frames[0].filename, /\/test\/integration\//);
279311
assert.match(ravenData.exception.values[0].stacktrace.frames[0]['function'], /\?|global code/);
280312
}
281313
);

test/integration/throw-object.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function throwStringError() {
2+
// never do this; just making sure Raven.js handles this case
3+
// gracefully
4+
throw {error: 'stuff is broken'};
5+
}
6+
7+
throwStringError();

vendor/TraceKit/tracekit.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var utils = require('../../src/utils');
4+
35
/*
46
TraceKit - Cross brower stack traces
57
@@ -26,7 +28,7 @@ var _slice = [].slice;
2628
var UNKNOWN_FUNCTION = '?';
2729

2830
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types
29-
var ERROR_TYPES_RE = /^(?:Uncaught (?:exception: )?)?((?:Eval|Internal|Range|Reference|Syntax|Type|URI|)Error): ?(.*)$/;
31+
var ERROR_TYPES_RE = /^(?:[Uu]ncaught (?:exception: )?)?(?:((?:Eval|Internal|Range|Reference|Syntax|Type|URI|)Error): )?(.*)$/;
3032

3133
function getLocationHref() {
3234
if (typeof document === 'undefined' || typeof document.location === 'undefined')
@@ -153,7 +155,7 @@ TraceKit.report = (function reportModuleWrapper() {
153155
if (lastExceptionStack) {
154156
TraceKit.computeStackTrace.augmentStackTraceWithInitialElement(lastExceptionStack, url, lineNo, message);
155157
processLastException();
156-
} else if (ex && {}.toString.call(ex) !== '[object String]') {
158+
} else if (ex && utils.isError(ex)) {
157159
// non-string `ex` arg; attempt to extract stack trace
158160

159161
// New chrome and blink send along a real error object

0 commit comments

Comments
 (0)