Browse Source

Prevents breakages of our functionality caused by side-effect callstats.io API failures.

j8
Lyubomir Marinov 10 years ago
parent
commit
9b28e64e5d
1 changed files with 78 additions and 52 deletions
  1. 78
    52
      modules/statistics/CallStats.js

+ 78
- 52
modules/statistics/CallStats.js View File

@@ -29,44 +29,70 @@ function initCallback (err, msg) {
29 29
 
30 30
 var callStatsIntegrationEnabled = config.callStatsID && config.callStatsSecret;
31 31
 
32
+/**
33
+ * Returns a function which invokes f in a try/catch block, logs any exception
34
+ * to the console, and then swallows it.
35
+ *
36
+ * @param f the function to invoke in a try/catch block
37
+ * @return a function which invokes f in a try/catch block, logs any exception
38
+ * to the console, and then swallows it
39
+ */
40
+function _try_catch (f) {
41
+    return function () {
42
+        try {
43
+            f.apply(this, arguments);
44
+        } catch (e) {
45
+            console.error(e);
46
+        }
47
+    };
48
+}
49
+
32 50
 var CallStats = {
33
-    init: function (jingleSession) {
51
+    init: _try_catch(function (jingleSession) {
34 52
         if(!this.isEnabled() || callStats !== null) {
35 53
             return;
36 54
         }
37 55
 
38
-        callStats = new callstats($, io, jsSHA);
39
-
40
-        this.session = jingleSession;
41
-        this.peerconnection = jingleSession.peerconnection.peerconnection;
56
+        try {
57
+            callStats = new callstats($, io, jsSHA);
42 58
 
43
-        this.userID = Settings.getCallStatsUserName();
59
+            this.session = jingleSession;
60
+            this.peerconnection = jingleSession.peerconnection.peerconnection;
61
+            this.userID = Settings.getCallStatsUserName();
44 62
 
45
-        var location = window.location;
46
-        this.confID = location.hostname + location.pathname;
63
+            var location = window.location;
47 64
 
48
-        //userID is generated or given by the origin server
49
-        callStats.initialize(config.callStatsID,
50
-            config.callStatsSecret,
51
-            this.userID,
52
-            initCallback);
65
+            this.confID = location.hostname + location.pathname;
53 66
 
54
-        var usage = callStats.fabricUsage.multiplex;
67
+            callStats.initialize(
68
+                    config.callStatsID, config.callStatsSecret,
69
+                    this.userID /* generated or given by the origin server */,
70
+                    initCallback);
55 71
 
56
-        callStats.addNewFabric(this.peerconnection,
57
-            Strophe.getResourceFromJid(jingleSession.peerjid),
58
-            usage,
59
-            this.confID,
60
-            this.pcCallback.bind(this));
72
+            var usage = callStats.fabricUsage.multiplex;
61 73
 
62
-        // notify callstats about failures if there were any
63
-        if (pendingErrors.length) {
74
+            callStats.addNewFabric(
75
+                    this.peerconnection,
76
+                    Strophe.getResourceFromJid(jingleSession.peerjid),
77
+                    usage,
78
+                    this.confID,
79
+                    this.pcCallback.bind(this));
80
+        } catch (e) {
81
+            // The callstats.io API failed to initialize (e.g. because its
82
+            // download failed to succeed in general or on time). Further
83
+            // attempts to utilize it cannot possibly succeed.
84
+            callStats = null;
85
+            console.error(e);
86
+        }
87
+        // Notify callstats about pre-init failures if there were any.
88
+        if (callStats && pendingErrors.length) {
64 89
             pendingErrors.forEach(function (error) {
65 90
                 this._reportError(error.type, error.error, error.pc);
66 91
             }, this);
67 92
             pendingErrors.length = 0;
68 93
         }
69
-    },
94
+    }),
95
+
70 96
     /**
71 97
      * Returns true if the callstats integration is enabled, otherwise returns
72 98
      * false.
@@ -77,15 +103,17 @@ var CallStats = {
77 103
     isEnabled: function() {
78 104
         return callStatsIntegrationEnabled;
79 105
     },
80
-    pcCallback: function (err, msg) {
106
+
107
+    pcCallback: _try_catch(function (err, msg) {
81 108
         if (!callStats) {
82 109
             return;
83 110
         }
84 111
         console.log("Monitoring status: "+ err + " msg: " + msg);
85 112
         callStats.sendFabricEvent(this.peerconnection,
86 113
             callStats.fabricEvent.fabricSetup, this.confID);
87
-    },
88
-    sendMuteEvent: function (mute, type) {
114
+    }),
115
+
116
+    sendMuteEvent: _try_catch(function (mute, type) {
89 117
         if (!callStats) {
90 118
             return;
91 119
         }
@@ -99,30 +127,32 @@ var CallStats = {
99 127
                 callStats.fabricEvent.audioUnmute);
100 128
         }
101 129
         callStats.sendFabricEvent(this.peerconnection, event, this.confID);
102
-    },
103
-    sendTerminateEvent: function () {
130
+    }),
131
+
132
+    sendTerminateEvent: _try_catch(function () {
104 133
         if(!callStats) {
105 134
             return;
106 135
         }
107 136
         callStats.sendFabricEvent(this.peerconnection,
108 137
             callStats.fabricEvent.fabricTerminated, this.confID);
109
-    },
110
-    sendSetupFailedEvent: function () {
138
+    }),
139
+
140
+    sendSetupFailedEvent: _try_catch(function () {
111 141
         if(!callStats) {
112 142
             return;
113 143
         }
114 144
         callStats.sendFabricEvent(this.peerconnection,
115 145
             callStats.fabricEvent.fabricSetupFailed, this.confID);
116
-    },
146
+    }),
117 147
 
118
-   /**
148
+    /**
119 149
      * Sends the given feedback through CallStats.
120 150
      *
121 151
      * @param overallFeedback an integer between 1 and 5 indicating the
122 152
      * user feedback
123 153
      * @param detailedFeedback detailed feedback from the user. Not yet used
124 154
      */
125
-    sendFeedback: function(overallFeedback, detailedFeedback) {
155
+    sendFeedback: _try_catch(function(overallFeedback, detailedFeedback) {
126 156
         if(!callStats) {
127 157
             return;
128 158
         }
@@ -132,9 +162,9 @@ var CallStats = {
132 162
 
133 163
         var feedbackJSON = JSON.parse(feedbackString);
134 164
 
135
-        callStats.sendUserFeedback(
136
-            this.confID, feedbackJSON);
137
-    },
165
+        callStats.sendUserFeedback(this.confID, feedbackJSON);
166
+    }),
167
+
138 168
     /**
139 169
      * Reports an error to callstats.
140 170
      *
@@ -147,11 +177,7 @@ var CallStats = {
147 177
         if (callStats) {
148 178
             callStats.reportError(pc, this.confID, type, e);
149 179
         } else if (this.isEnabled()) {
150
-            pendingErrors.push({
151
-                type: type,
152
-                error: e,
153
-                pc: pc
154
-            });
180
+            pendingErrors.push({ type: type, error: e, pc: pc });
155 181
         }
156 182
         // else just ignore it
157 183
     },
@@ -161,9 +187,9 @@ var CallStats = {
161 187
      *
162 188
      * @param {Error} e error to send
163 189
      */
164
-    sendGetUserMediaFailed: function (e) {
190
+    sendGetUserMediaFailed: _try_catch(function (e) {
165 191
         this._reportError(wrtcFuncNames.getUserMedia, e, null);
166
-    },
192
+    }),
167 193
 
168 194
     /**
169 195
      * Notifies CallStats that peer connection failed to create offer.
@@ -171,9 +197,9 @@ var CallStats = {
171 197
      * @param {Error} e error to send
172 198
      * @param {RTCPeerConnection} pc connection on which failure occured.
173 199
      */
174
-    sendCreateOfferFailed: function (e, pc) {
200
+    sendCreateOfferFailed: _try_catch(function (e, pc) {
175 201
         this._reportError(wrtcFuncNames.createOffer, e, pc);
176
-    },
202
+    }),
177 203
 
178 204
     /**
179 205
      * Notifies CallStats that peer connection failed to create answer.
@@ -181,9 +207,9 @@ var CallStats = {
181 207
      * @param {Error} e error to send
182 208
      * @param {RTCPeerConnection} pc connection on which failure occured.
183 209
      */
184
-    sendCreateAnswerFailed: function (e, pc) {
210
+    sendCreateAnswerFailed: _try_catch(function (e, pc) {
185 211
         this._reportError(wrtcFuncNames.createAnswer, e, pc);
186
-    },
212
+    }),
187 213
 
188 214
     /**
189 215
      * Notifies CallStats that peer connection failed to set local description.
@@ -191,9 +217,9 @@ var CallStats = {
191 217
      * @param {Error} e error to send
192 218
      * @param {RTCPeerConnection} pc connection on which failure occured.
193 219
      */
194
-    sendSetLocalDescFailed: function (e, pc) {
220
+    sendSetLocalDescFailed: _try_catch(function (e, pc) {
195 221
         this._reportError(wrtcFuncNames.setLocalDescription, e, pc);
196
-    },
222
+    }),
197 223
 
198 224
     /**
199 225
      * Notifies CallStats that peer connection failed to set remote description.
@@ -201,9 +227,9 @@ var CallStats = {
201 227
      * @param {Error} e error to send
202 228
      * @param {RTCPeerConnection} pc connection on which failure occured.
203 229
      */
204
-    sendSetRemoteDescFailed: function (e, pc) {
230
+    sendSetRemoteDescFailed: _try_catch(function (e, pc) {
205 231
         this._reportError(wrtcFuncNames.setRemoteDescription, e, pc);
206
-    },
232
+    }),
207 233
 
208 234
     /**
209 235
      * Notifies CallStats that peer connection failed to add ICE candidate.
@@ -211,8 +237,8 @@ var CallStats = {
211 237
      * @param {Error} e error to send
212 238
      * @param {RTCPeerConnection} pc connection on which failure occured.
213 239
      */
214
-    sendAddIceCandidateFailed: function (e, pc) {
240
+    sendAddIceCandidateFailed: _try_catch(function (e, pc) {
215 241
         this._reportError(wrtcFuncNames.addIceCandidate, e, pc);
216
-    }
242
+    })
217 243
 };
218 244
 module.exports = CallStats;

Loading…
Cancel
Save