Adam Ahmed avatar Adam Ahmed committed b9fbf47

Stop crashing when getting unrecognized responses during booking.

Comments (0)

Files changed (1)

lib/gapps/datasource.js

 var gcal = require('./gcal');
 
 var path = require('path');
+var util = require('util');
 
 var Room = require('../models/room');
 var Event = require('../models/event');
     });
     return response;
 }
-function pollForConfirmation(ds, roomKey, eventId, cb) {
+function pollForConfirmation(ds, roomKey, eventId, cb, triesLeft) {
     ds.gcal.getEvent(roomKey, eventId, function(err, gevent) {
-        var response = roomResponse(gevent, roomKey);
-        if (response !== 'needsAction') {
+        var response;
+        if (!gevent.attendees) {
+            ds.emit('error', new Error('Unrecognized response for event request:\n' + util.inspect(gevent)));
+        } else {
+            response = roomResponse(gevent, roomKey);
+        }
+        if (response && response !== 'needsAction') {
             cb(null, gevent);
         } else {
-            pollForConfirmation(ds,roomKey, eventId, cb);
+            if (triesLeft === 0) {
+                var lastResponseStr = util.inspect(gevent);
+                return cb(new Error('Could not get a valid response. Last response was:\n' + lastResponseStr));
+            }
+            pollForConfirmation(ds,roomKey, eventId, cb, triesLeft != null ? triesLeft - 1 : 2);
         }
     });
 }
     if (~index) {
         this.gcal.book(roomKey, startDate, endDate, function(err, gevent) {
             if (err) {
+                self.emit('error', err);
                 return cb(err);
             }
             if (!gevent.start) {
+                self.emit('error', gevent);
                 return cb(gevent);
             }
 
             pollForConfirmation(self, roomKey, gevent.id, function(err, gevent) {
+                if (err) {
+                    self.emit('error', err);
+                    return cb(err);
+                }
+
                 var keep = gevent.status === 'confirmed' && gevent.attendees.some(function(attendee) {
                     // We need to check that the room accepted the event and filter it out otherwise.
                     return attendee.email === roomKey && attendee.responseStatus === 'accepted';
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.