Skip to content

Commit

Permalink
Add stack-based messages to all assertions for IE
Browse files Browse the repository at this point in the history
IE11 does not show stack traces on failed assertions in the console.
So if there is no message associated with a failed assertion, create
one based on the stack trace.  This should help debug errors on IE.

Note that in Shaka 2, we already require messages for all assertions,
so this is just a stop-gap measure to fix lingering bugs in Shaka 1.

Issue #251

Change-Id: Ib42c693278485fa2d3bad7e34f3c73d15d23be27
  • Loading branch information
joeyparrish committed Feb 1, 2016
1 parent 9c817ce commit b47a03a
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/debug/asserts.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ goog.provide('shaka.asserts');
goog.define('shaka.asserts.ENABLE_ASSERTS', goog.DEBUG);


/** @type {function()|function(*, string=)} */
/** @type {function(*, string=)} */
shaka.asserts.assert = function() {};


Expand All @@ -48,11 +48,6 @@ shaka.asserts.patchAssert_ = function() {

if (!assert) {
console.assert = function() {};
} else if (!assert.bind) {
// IE 9 does not provide a .bind for the built-in console functions.
console.assert = function() {
assert.apply(console, arguments);
};
}
};

Expand All @@ -61,8 +56,21 @@ shaka.asserts.patchAssert_ = function() {
if (shaka.asserts.ENABLE_ASSERTS) {
shaka.asserts.patchAssert_();

shaka.asserts.assert =
console.assert.bind(console);
shaka.asserts.assert = function(test, opt_message) {
var message = opt_message;
if (!message) {
// IE11 does not display any stack information on assertions. So if
// there is no message, create one from the stack.
try {
throw new Error();
} catch (error) {
// Line 0 is "Error", line 1 is this location, line 2 is the immediate
// caller of shaka.asserts.assert().
message = error.stack.split('\n')[2];
}
}
console.assert(test, message);
};

shaka.asserts.notImplemented =
console.assert.bind(console, 0 == 1, 'Not implemented.');
Expand Down

0 comments on commit b47a03a

Please sign in to comment.