diff --git a/lib/http-api.js b/lib/http-api.js index 9468d8520..04f2a9bbe 100644 --- a/lib/http-api.js +++ b/lib/http-api.js @@ -378,23 +378,36 @@ module.exports.MatrixHttpApi.prototype = { } } var defer = q.defer(); - this.opts.request( - { - uri: uri, - method: method, - withCredentials: false, - qs: queryParams, - body: data, - json: true, - _matrix_opts: this.opts - }, - requestCallback(defer, callback, this.opts.onlyData) - ); + try { + this.opts.request( + { + uri: uri, + method: method, + withCredentials: false, + qs: queryParams, + body: data, + json: true, + _matrix_opts: this.opts + }, + requestCallback(defer, callback, this.opts.onlyData) + ); + } + catch (ex) { + defer.reject(ex); + if (callback) callback(ex); + } return defer.promise; } }; - +/** + * Returns a callback that can be invoked by an HTTP request on completion, + * that will either resolve or reject the given defer as well as invoke the + * given userDefinedCallback (if any). + * + * If onlyData is true, the defer/callback is invoked with the body of the + * response, otherwise the result code. + */ var requestCallback = function(defer, userDefinedCallback, onlyData) { userDefinedCallback = userDefinedCallback || function() {};