Skip to content

Commit 5b06ae0

Browse files
committed
Merge pull request web3#266 from ethereum/fixedBatch
Fixed batch error reporting
2 parents 203a83f + cf24469 commit 5b06ae0

File tree

9 files changed

+257
-28
lines changed

9 files changed

+257
-28
lines changed

dist/web3-light.js

+45-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web3-light.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web3.js

+45-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web3.js.map

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web3.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/web3/batch.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222

2323
var RequestManager = require('./requestmanager');
24+
var Jsonrpc = require('./jsonrpc');
25+
var errors = require('./errors');
2426

2527
var Batch = function () {
2628
this.requests = [];
@@ -47,11 +49,14 @@ Batch.prototype.execute = function () {
4749
results = results || [];
4850
requests.map(function (request, index) {
4951
return results[index] || {};
50-
}).map(function (result, index) {
51-
return requests[index].format ? requests[index].format(result.result) : result.result;
5252
}).forEach(function (result, index) {
5353
if (requests[index].callback) {
54-
requests[index].callback(err, result);
54+
55+
if (!Jsonrpc.getInstance().isValidResponse(result)) {
56+
return requests[index].callback(errors.InvalidResponse(result));
57+
}
58+
59+
requests[index].callback(null, (requests[index].format ? requests[index].format(result.result) : result.result));
5560
}
5661
});
5762
});

lib/web3/property.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323

2424
var RequestManager = require('./requestmanager');
25+
var utils = require('../utils/utils');
2526

2627
var Property = function (options) {
2728
this.name = options.name;
@@ -53,6 +54,19 @@ Property.prototype.formatOutput = function (result) {
5354
return this.outputFormatter && result !== null ? this.outputFormatter(result) : result;
5455
};
5556

57+
/**
58+
* Should be used to extract callback from array of arguments. Modifies input param
59+
*
60+
* @method extractCallback
61+
* @param {Array} arguments
62+
* @return {Function|Null} callback, if exists
63+
*/
64+
Property.prototype.extractCallback = function (args) {
65+
if (utils.isFunction(args[args.length - 1])) {
66+
return args.pop(); // modify the args array!
67+
}
68+
};
69+
5670
/**
5771
* Should attach function to method
5872
*
@@ -79,7 +93,10 @@ Property.prototype.attachToObject = function (obj) {
7993
return prefix + name.charAt(0).toUpperCase() + name.slice(1);
8094
};
8195

82-
obj[toAsyncName('get', name)] = this.getAsync.bind(this);
96+
var func = this.getAsync.bind(this);
97+
func.request = this.request.bind(this);
98+
99+
obj[toAsyncName('get', name)] = func;
83100
};
84101

85102
/**
@@ -112,5 +129,22 @@ Property.prototype.getAsync = function (callback) {
112129
});
113130
};
114131

132+
/**
133+
* Should be called to create pure JSONRPC request which can be used in batch request
134+
*
135+
* @method request
136+
* @param {...} params
137+
* @return {Object} jsonrpc request
138+
*/
139+
Property.prototype.request = function () {
140+
var payload = {
141+
method: this.getter,
142+
params: [],
143+
callback: this.extractCallback(Array.prototype.slice.call(arguments))
144+
};
145+
payload.format = this.formatOutput.bind(this);
146+
return payload;
147+
};
148+
115149
module.exports = Property;
116150

0 commit comments

Comments
 (0)