1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-18 05:42:00 +03:00

turn HTTP exceptions into errbacks or rejected deferreds rather than bubbling them up and expecting the app to have try blocks eeeeeeeeeverywhere

This commit is contained in:
Matthew Hodgson
2015-08-31 18:26:27 +01:00
parent 6d6868df73
commit 3b9f1728c7

View File

@@ -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() {};