proper error handling proposal

This commit is contained in:
Marek Kotewicz 2014-11-07 14:03:46 +01:00
parent b0e11826e5
commit 0d6479e1d6
2 changed files with 18 additions and 13 deletions

View File

@ -40,7 +40,8 @@
return {
_id: object.id,
data: object.result
data: object.result,
error: object.error
};
};
@ -70,7 +71,7 @@
var self = this;
this.sendRequest(payload, function (request) {
var parsed = JSON.parse(request.responseText);
if (parsed.result instanceof Array ? parsed.result.length === 0 : !parsed.result) {
if (parsed.error || (parsed.result instanceof Array ? parsed.result.length === 0 : !parsed.result)) {
return;
}
self.handlers.forEach(function (handler) {

22
main.js
View File

@ -153,12 +153,12 @@
return {call: call, args: args};
}).then(function (request) {
return new Promise(function (resolve, reject) {
web3.provider.send(request, function (result) {
if (result || typeof result === "boolean") {
web3.provider.send(request, function (err, result) {
if (!err) {
resolve(result);
return;
}
reject(result);
reject(err);
});
});
}).catch(function(err) {
@ -173,8 +173,12 @@
var proto = {};
proto.get = function () {
return new Promise(function(resolve, reject) {
web3.provider.send({call: property.getter}, function(result) {
web3.provider.send({call: property.getter}, function(err, result) {
if (!err) {
resolve(result);
return
}
reject(err);
});
});
};
@ -182,12 +186,12 @@
proto.set = function (val) {
return flattenPromise([val]).then(function (args) {
return new Promise(function (resolve) {
web3.provider.send({call: property.setter, args: args}, function (result) {
if (result) {
web3.provider.send({call: property.setter, args: args}, function (err, result) {
if (!err) {
resolve(result);
} else {
reject(result);
return
}
reject(err);
});
});
}).catch(function (err) {
@ -438,7 +442,7 @@
if(data._id) {
var cb = web3._callbacks[data._id];
if (cb) {
cb.call(this, data.data)
cb.call(this, data.error, data.data)
delete web3._callbacks[data._id];
}
}