New HTTP sync API

This commit is contained in:
Avital Oliver
2013-04-19 14:05:17 -07:00
parent c9bacb897e
commit 03085e8842
2 changed files with 17 additions and 18 deletions

View File

@@ -2826,12 +2826,13 @@ standard `x-www-form-urlencoded` content type, unless the `content`
or `data` option is used to specify a body, in which case the
parameters will be appended to the URL instead.
The callback receives two arguments, `error` and `result`. The `error`
argument will contain an Error if the request fails in any way,
including a network error, time-out, or an HTTP status code in the 400
or 500 range. The result object is always
defined. When run in synchronous mode, the `result` is returned from the
function, and the `error` value is a stored as a property in `result`.
The callback receives two arguments, `error` and `result`. The
`error` argument will contain an Error if the request fails in any
way, including a network error, time-out, or an HTTP status code in
the 400 or 500 range. In case of a 4xx/5xx HTTP status code, the
`response` property on `error` matches the contents the result object
would have had otherwise. When run in synchronous mode, either
`result` is returned from the function, or `error` is thrown.
Contents of the result object:
@@ -2853,11 +2854,6 @@ Contents of the result object:
<span class="type">Object</span></dt>
<dd>A dictionary of HTTP headers from the response.</dd>
<dt><span class="name">error</span>
<span class="type">Error</span></dt>
<dd>Error object if the request failed. Matches the <code>error</code> callback parameter.</dd>
</dl>
Example server method:

View File

@@ -65,7 +65,10 @@ Meteor.http.call = function(method, url, options, callback) {
// Sync mode
fut = new Future;
callback = function(error, result) {
fut.ret(result);
if (error)
fut.throw(error);
else
fut.ret(result);
};
} else {
// Async mode
@@ -75,13 +78,13 @@ Meteor.http.call = function(method, url, options, callback) {
});
}
// wrap callback to always return a result object, and always
// have an 'error' property in result
// wrap callback to add a 'response' property on an error, in case
// we have both (http 4xx/5xx error, which has a response payload)
callback = (function(callback) {
return function(error, result) {
result = result || {};
result.error = error;
callback(error, result);
return function(error, response) {
if (error && response)
error.response = response;
callback(error, response);
};
})(callback);