Skip to content

Fix bad message pulled from thrown strings and objects #872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

var TraceKit = require('../vendor/TraceKit/tracekit');
var RavenConfigError = require('./configError');
var utils = require('./utils');

var isError = utils.isError,
isObject = utils.isObject;

var stringify = require('json-stringify-safe');

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

function isObject(what) {
return typeof what === 'object' && what !== null;
}

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

// Sorta yanked from https://github.com/joyent/node/blob/aa3b4b4/lib/util.js#L560
// with some tiny modifications
function isError(what) {
var toString = objectPrototype.toString.call(what);
return isObject(what) &&
toString === '[object Error]' ||
toString === '[object Exception]' || // Firefox NS_ERROR_FAILURE Exceptions
what instanceof Error;
}

function each(obj, callback) {
var i, j;

Expand Down
20 changes: 20 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

function isObject(what) {
return typeof what === 'object' && what !== null;
}

// Sorta yanked from https://github.com/joyent/node/blob/aa3b4b4/lib/util.js#L560
// with some tiny modifications
function isError(what) {
var toString = {}.toString.call(what);
return isObject(what) &&
toString === '[object Error]' ||
toString === '[object Exception]' || // Firefox NS_ERROR_FAILURE Exceptions
what instanceof Error;
}

module.exports = {
isObject: isObject,
isError: isError
};
83 changes: 83 additions & 0 deletions test/integration/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,89 @@ describe('integration', function () {
);
});

it('should catch thrown strings', function (done) {
var iframe = this.iframe;

iframeExecute(iframe, done,
function () {
// intentionally loading this error via a script file to make
// sure it is 1) not caught by instrumentation 2) doesn't trigger
// "Script error"
var script = document.createElement('script');
script.src = 'throw-string.js';
script.onload = function () {
done();
};
document.head.appendChild(script);
},
function () {
var ravenData = iframe.contentWindow.ravenData[0];
assert.match(ravenData.exception.values[0].value, /stringError$/);
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, 1); // always 1 because thrown strings can't provide > 1 frame

// some browsers extract proper url, line, and column for thrown strings
// but not all - falls back to frame url
assert.match(ravenData.exception.values[0].stacktrace.frames[0].filename, /\/test\/integration\//);
assert.match(ravenData.exception.values[0].stacktrace.frames[0]['function'], /\?|global code/);
}
);
});

it('should catch thrown objects', function (done) {
var iframe = this.iframe;

iframeExecute(iframe, done,
function () {
// intentionally loading this error via a script file to make
// sure it is 1) not caught by instrumentation 2) doesn't trigger
// "Script error"
var script = document.createElement('script');
script.src = 'throw-object.js';
script.onload = function () {
done();
};
document.head.appendChild(script);
},
function () {
var ravenData = iframe.contentWindow.ravenData[0];
assert.equal(ravenData.exception.values[0].type, undefined);
assert.equal(ravenData.exception.values[0].value, '[object Object]');
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, 1); // always 1 because thrown objects can't provide > 1 frame

// some browsers extract proper url, line, and column for thrown objects
// but not all - falls back to frame url
assert.match(ravenData.exception.values[0].stacktrace.frames[0].filename, /\/test\/integration\//);
assert.match(ravenData.exception.values[0].stacktrace.frames[0]['function'], /\?|global code/);
}
);
});

it('should catch thrown errors', function (done) {
var iframe = this.iframe;

iframeExecute(iframe, done,
function () {
// intentionally loading this error via a script file to make
// sure it is 1) not caught by instrumentation 2) doesn't trigger
// "Script error"
var script = document.createElement('script');
script.src = 'throw-error.js';
script.onload = function () {
done();
};
document.head.appendChild(script);
},
function () {
var ravenData = iframe.contentWindow.ravenData[0];
assert.match(ravenData.exception.values[0].type, /^Error/);
assert.match(ravenData.exception.values[0].value, /realError$/);
assert.isAbove(ravenData.exception.values[0].stacktrace.frames.length, 0); // 1 or 2 depending on platform
assert.match(ravenData.exception.values[0].stacktrace.frames[0].filename, /\/test\/integration\/throw-error\.js/)
assert.match(ravenData.exception.values[0].stacktrace.frames[0]['function'], /\?|global code/);
}
);
});

it('should NOT catch an exception already caught via Raven.wrap', function (done) {
var iframe = this.iframe;

Expand Down
5 changes: 5 additions & 0 deletions test/integration/throw-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function throwRealError() {
throw new Error('realError');
}

throwRealError();
7 changes: 7 additions & 0 deletions test/integration/throw-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function throwStringError() {
// never do this; just making sure Raven.js handles this case
// gracefully
throw {error: 'stuff is broken'};
}

throwStringError();
5 changes: 5 additions & 0 deletions test/integration/throw-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function throwStringError() {
throw 'stringError';
}

throwStringError();
10 changes: 7 additions & 3 deletions vendor/TraceKit/tracekit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var utils = require('../../src/utils');

/*
TraceKit - Cross brower stack traces

Expand All @@ -26,7 +28,7 @@ var _slice = [].slice;
var UNKNOWN_FUNCTION = '?';

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

function getLocationHref() {
if (typeof document === 'undefined' || typeof document.location === 'undefined')
Expand All @@ -35,6 +37,7 @@ function getLocationHref() {
return document.location.href;
}


/**
* TraceKit.report: cross-browser processing of unhandled exceptions
*
Expand Down Expand Up @@ -152,7 +155,9 @@ TraceKit.report = (function reportModuleWrapper() {
if (lastExceptionStack) {
TraceKit.computeStackTrace.augmentStackTraceWithInitialElement(lastExceptionStack, url, lineNo, message);
processLastException();
} else if (ex) {
} else if (ex && utils.isError(ex)) {
// non-string `ex` arg; attempt to extract stack trace

// New chrome and blink send along a real error object
// Let's just report that like a normal error.
// See: https://mikewest.org/2013/08/debugging-runtime-errors-with-window-onerror
Expand Down Expand Up @@ -595,7 +600,6 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
throw e;
}
}

return {
'name': ex.name,
'message': ex.message,
Expand Down