Skip to content

Commit 9c8bb49

Browse files
committed
Fix bad message pulled from thrown strings
Fixes #871
1 parent b8d9670 commit 9c8bb49

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

test/integration/test.js

+51
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,57 @@ describe('integration', function () {
256256
);
257257
});
258258

259+
it('should catch thrown strings', function (done) {
260+
var iframe = this.iframe;
261+
262+
iframeExecute(iframe, done,
263+
function () {
264+
// intentionally loading this error via a script file to make
265+
// sure it is 1) not caught by instrumentation 2) doesn't trigger
266+
// "Script error"
267+
var script = document.createElement('script');
268+
script.src = 'throw-string.js';
269+
script.onload = function () {
270+
done();
271+
};
272+
document.head.appendChild(script);
273+
},
274+
function () {
275+
var ravenData = iframe.contentWindow.ravenData[0];
276+
assert.match(ravenData.exception.values[0].value, /stringError$/);
277+
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/)
279+
assert.match(ravenData.exception.values[0].stacktrace.frames[0]['function'], /\?|global code/);
280+
}
281+
);
282+
});
283+
284+
it('should catch thrown errors', function (done) {
285+
var iframe = this.iframe;
286+
287+
iframeExecute(iframe, done,
288+
function () {
289+
// intentionally loading this error via a script file to make
290+
// sure it is 1) not caught by instrumentation 2) doesn't trigger
291+
// "Script error"
292+
var script = document.createElement('script');
293+
script.src = 'throw-error.js';
294+
script.onload = function () {
295+
done();
296+
};
297+
document.head.appendChild(script);
298+
},
299+
function () {
300+
var ravenData = iframe.contentWindow.ravenData[0];
301+
assert.match(ravenData.exception.values[0].type, /^Error/);
302+
assert.match(ravenData.exception.values[0].value, /realError$/);
303+
assert.isAbove(ravenData.exception.values[0].stacktrace.frames.length, 0); // 1 or 2 depending on platform
304+
assert.match(ravenData.exception.values[0].stacktrace.frames[0].filename, /\/test\/integration\/throw-error\.js/)
305+
assert.match(ravenData.exception.values[0].stacktrace.frames[0]['function'], /\?|global code/);
306+
}
307+
);
308+
});
309+
259310
it('should NOT catch an exception already caught via Raven.wrap', function (done) {
260311
var iframe = this.iframe;
261312

test/integration/throw-error.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function throwRealError() {
2+
throw new Error('realError');
3+
}
4+
5+
throwRealError();

test/integration/throw-string.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function throwStringError() {
2+
throw 'stringError';
3+
}
4+
5+
throwStringError();

vendor/TraceKit/tracekit.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var _slice = [].slice;
2626
var UNKNOWN_FUNCTION = '?';
2727

2828
// 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): ?(.*)$/;
29+
var ERROR_TYPES_RE = /^(?:Uncaught (?:exception: )?)?((?:Eval|Internal|Range|Reference|Syntax|Type|URI|)Error): ?(.*)$/;
3030

3131
function getLocationHref() {
3232
if (typeof document === 'undefined' || typeof document.location === 'undefined')
@@ -152,7 +152,11 @@ TraceKit.report = (function reportModuleWrapper() {
152152
if (lastExceptionStack) {
153153
TraceKit.computeStackTrace.augmentStackTraceWithInitialElement(lastExceptionStack, url, lineNo, message);
154154
processLastException();
155-
} else if (ex) {
155+
} else if (ex && typeof ex === 'object') {
156+
// intentionally a "weak" object check here - want to accept
157+
// Error-like objects just in case (previously *any* truthy ex value)
158+
// triggered this branch
159+
156160
// New chrome and blink send along a real error object
157161
// Let's just report that like a normal error.
158162
// See: https://mikewest.org/2013/08/debugging-runtime-errors-with-window-onerror
@@ -595,7 +599,6 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
595599
throw e;
596600
}
597601
}
598-
599602
return {
600603
'name': ex.name,
601604
'message': ex.message,

0 commit comments

Comments
 (0)