js: Polyfill console inside our own namespace
This commit is contained in:
parent
b62d4d5374
commit
7f570de239
@ -1,27 +0,0 @@
|
||||
/*jslint browser: true, plusplus: true */
|
||||
|
||||
/**
|
||||
* From
|
||||
* http://skratchdot.com/2012/05/prevent-console-calls-from-throwing-errors/
|
||||
*/
|
||||
|
||||
(function (window) {
|
||||
'use strict';
|
||||
|
||||
var i = 0,
|
||||
emptyFunction = function () {},
|
||||
functionNames = [
|
||||
'assert', 'clear', 'count', 'debug', 'dir',
|
||||
'dirxml', 'error', 'exception', 'group', 'groupCollapsed',
|
||||
'groupEnd', 'info', 'log', 'profile', 'profileEnd', 'table',
|
||||
'time', 'timeEnd', 'timeStamp', 'trace', 'warn'
|
||||
];
|
||||
|
||||
// Make sure window.console exists
|
||||
window.console = window.console || {};
|
||||
|
||||
// Make sure all functions exist
|
||||
for (i = 0; i < functionNames.length; i++) {
|
||||
window.console[functionNames[i]] = window.console[functionNames[i]] || emptyFunction;
|
||||
}
|
||||
}(window));
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
function Mopidy(settings) {
|
||||
this._settings = this._configure(settings || {});
|
||||
this._console = this._getConsole();
|
||||
|
||||
this._backoffDelay = this._settings.backoffDelayMin;
|
||||
this._pendingRequests = {};
|
||||
@ -29,6 +30,16 @@ Mopidy.prototype._configure = function (settings) {
|
||||
return settings;
|
||||
};
|
||||
|
||||
Mopidy.prototype._getConsole = function () {
|
||||
var console = window.console || {};
|
||||
|
||||
console.log = console.log || function () {};
|
||||
console.warn = console.warn || function () {};
|
||||
console.error = console.error || function () {};
|
||||
|
||||
return console;
|
||||
};
|
||||
|
||||
Mopidy.prototype._delegateEvents = function () {
|
||||
// Remove existing event handlers
|
||||
this.off("websocket:close");
|
||||
@ -109,7 +120,7 @@ Mopidy.prototype._resetBackoffDelay = function () {
|
||||
};
|
||||
|
||||
Mopidy.prototype._handleWebSocketError = function (error) {
|
||||
console.warn("WebSocket error:", error.stack || error);
|
||||
this._console.warn("WebSocket error:", error.stack || error);
|
||||
};
|
||||
|
||||
Mopidy.prototype._send = function (message) {
|
||||
@ -158,13 +169,13 @@ Mopidy.prototype._handleMessage = function (message) {
|
||||
} else if (data.hasOwnProperty("event")) {
|
||||
this._handleEvent(data);
|
||||
} else {
|
||||
console.warn(
|
||||
this._console.warn(
|
||||
"Unknown message type received. Message was: " +
|
||||
message.data);
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof SyntaxError) {
|
||||
console.warn(
|
||||
this._console.warn(
|
||||
"WebSocket message parsing failed. Message was: " +
|
||||
message.data);
|
||||
} else {
|
||||
@ -175,7 +186,7 @@ Mopidy.prototype._handleMessage = function (message) {
|
||||
|
||||
Mopidy.prototype._handleResponse = function (responseMessage) {
|
||||
if (!this._pendingRequests.hasOwnProperty(responseMessage.id)) {
|
||||
console.warn(
|
||||
this._console.warn(
|
||||
"Unexpected response received. Message was:", responseMessage);
|
||||
return;
|
||||
}
|
||||
@ -187,13 +198,13 @@ Mopidy.prototype._handleResponse = function (responseMessage) {
|
||||
resolver.resolve(responseMessage.result);
|
||||
} else if (responseMessage.hasOwnProperty("error")) {
|
||||
resolver.reject(responseMessage.error);
|
||||
console.warn("Server returned error:", responseMessage.error);
|
||||
this._console.warn("Server returned error:", responseMessage.error);
|
||||
} else {
|
||||
resolver.reject({
|
||||
message: "Response without 'result' or 'error' received",
|
||||
data: {response: responseMessage}
|
||||
});
|
||||
console.warn(
|
||||
this._console.warn(
|
||||
"Response without 'result' or 'error' received. Message was:",
|
||||
responseMessage);
|
||||
}
|
||||
|
||||
@ -266,7 +266,7 @@ buster.testCase("Mopidy", {
|
||||
},
|
||||
|
||||
"without stack logs the error to the console": function () {
|
||||
var stub = this.stub(console, "warn");
|
||||
var stub = this.stub(this.mopidy._console, "warn");
|
||||
var error = {};
|
||||
|
||||
this.mopidy._handleWebSocketError(error);
|
||||
@ -275,7 +275,7 @@ buster.testCase("Mopidy", {
|
||||
},
|
||||
|
||||
"with stack logs the error to the console": function () {
|
||||
var stub = this.stub(console, "warn");
|
||||
var stub = this.stub(this.mopidy._console, "warn");
|
||||
var error = {stack: "foo"};
|
||||
|
||||
this.mopidy._handleWebSocketError(error);
|
||||
@ -421,7 +421,7 @@ buster.testCase("Mopidy", {
|
||||
},
|
||||
|
||||
"logs unknown messages": function () {
|
||||
var stub = this.stub(console, "warn");
|
||||
var stub = this.stub(this.mopidy._console, "warn");
|
||||
var messageEvent = {data: JSON.stringify({foo: "bar"})};
|
||||
|
||||
this.mopidy._handleMessage(messageEvent);
|
||||
@ -432,7 +432,7 @@ buster.testCase("Mopidy", {
|
||||
},
|
||||
|
||||
"logs JSON parsing errors": function () {
|
||||
var stub = this.stub(console, "warn");
|
||||
var stub = this.stub(this.mopidy._console, "warn");
|
||||
var messageEvent = {data: "foobarbaz"};
|
||||
|
||||
this.mopidy._handleMessage(messageEvent);
|
||||
@ -445,7 +445,7 @@ buster.testCase("Mopidy", {
|
||||
|
||||
"._handleResponse": {
|
||||
"logs unexpected responses": function () {
|
||||
var stub = this.stub(console, "warn");
|
||||
var stub = this.stub(this.mopidy._console, "warn");
|
||||
var responseMessage = {
|
||||
jsonrpc: "2.0",
|
||||
id: 1337,
|
||||
@ -490,7 +490,7 @@ buster.testCase("Mopidy", {
|
||||
},
|
||||
|
||||
"rejects and logs requests which get errors back": function (done) {
|
||||
var stub = this.stub(console, "warn");
|
||||
var stub = this.stub(this.mopidy._console, "warn");
|
||||
var promise = this.mopidy._send({method: "bar"});
|
||||
var responseError = {message: "Error", data: {}};
|
||||
var responseMessage = {
|
||||
@ -511,7 +511,7 @@ buster.testCase("Mopidy", {
|
||||
},
|
||||
|
||||
"rejects and logs responses without result or error": function (done) {
|
||||
var stub = this.stub(console, "warn");
|
||||
var stub = this.stub(this.mopidy._console, "warn");
|
||||
var promise = this.mopidy._send({method: "bar"});
|
||||
var responseMessage = {
|
||||
jsonrpc: "2.0",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user