From d3062622893ed79dad4c4333f56da874f6e180e5 Mon Sep 17 00:00:00 2001 From: Shane Tomlinson <stomlinson@mozilla.com> Date: Wed, 25 Apr 2012 10:33:33 +0100 Subject: [PATCH] Adding a catch all case when path specific code is started. * The catch all will print a console log message if console.log is available. * Adding helpers.log which prints to the console if console.log is available. --- resources/static/pages/start.js | 6 ++ resources/static/shared/helpers.js | 18 +++++- resources/static/test/cases/shared/helpers.js | 61 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/resources/static/pages/start.js b/resources/static/pages/start.js index f2adcff07..34e4c2dec 100644 --- a/resources/static/pages/start.js +++ b/resources/static/pages/start.js @@ -11,6 +11,7 @@ $(function() { */ var bid = BrowserID, + helpers = bid.Helpers, pageHelpers = bid.PageHelpers, user = bid.User, dom = bid.DOM, @@ -79,6 +80,11 @@ $(function() { else if(token && path === "/verify_email_address") { bid.verifyEmailAddress(token); } + else { + // Instead of throwing a hard error here, adding a message to the console + // to let developers know something is up. + helpers.log("unknown path"); + } user.checkAuthentication(function(authenticated) { if (authenticated) { diff --git a/resources/static/shared/helpers.js b/resources/static/shared/helpers.js index 8b2d99794..7f536f6b6 100644 --- a/resources/static/shared/helpers.js +++ b/resources/static/shared/helpers.js @@ -67,6 +67,15 @@ } } + function log(msg) { + try { + window.console.log(msg); + } catch(e) { + // Catch all if console is not available or if it for some reason blows + // up. Do nothing. + } + } + extend(helpers, { /** * Extend an object with the properties of another object. Overwrites @@ -116,7 +125,14 @@ * parameter is a function. * @param {variant} [params] - parameters to pass to callback. */ - complete: complete + complete: complete, + + /** + * If the console is available, log a message to it. + * @method log + * @param {string} msg + */ + log: log }); diff --git a/resources/static/test/cases/shared/helpers.js b/resources/static/test/cases/shared/helpers.js index 7e0a171de..8ebdd3f7d 100644 --- a/resources/static/test/cases/shared/helpers.js +++ b/resources/static/test/cases/shared/helpers.js @@ -103,4 +103,65 @@ equal(url, "https://browserid.org?email=testuser%40testuser.com&status=complete", "correct URL with GET parameters"); }); + + test("simulate log on browser without console - no exception thrown", function() { + var err, + nativeConsole = window.console; + + // Simulate browser without window.console. + window.console = undefined; + try { + helpers.log("test message"); + } + catch(e) { + err = e; + } + + equal(typeof err, "undefined", "no exception thrown"); + + window.console = nativeConsole; + }); + + test("simulate log on browser without console.log - no exception thrown", function() { + var err, + nativeConsole = window.console; + + // Simulate browser with console, but without console.log. + window.console = {}; + try { + helpers.log("test message"); + } + catch(e) { + err = e; + } + + equal(typeof err, "undefined", "no exception thrown"); + + window.console = nativeConsole; + }); + + test("simulate log on browser with console.log - prints message", function() { + var err, + loggedMessage, + nativeConsole = window.console; + + // Simulate browser with console and console.log + window.console = { + log: function(msg) { + loggedMessage = msg; + } + }; + + try { + helpers.log("test message"); + } + catch(e) { + err = e; + } + + equal(typeof err, "undefined", "no exception thrown"); + equal(loggedMessage, "test message", "correct message logged"); + + window.console = nativeConsole; + }); }()); -- GitLab