Skip to content

Commit e4de4cf

Browse files
committed
modify the deal way when get error responce from node-server
1 parent 3968cfc commit e4de4cf

5 files changed

Lines changed: 97 additions & 103 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chainsql",
3-
"version": "0.6.53",
3+
"version": "0.6.54",
44
"description": "An database driver for chainsql ",
55
"main": "src/index.js",
66
"scripts": {

src/index.js

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -549,18 +549,13 @@ function handleCommit(ChainSQL, object, resolve, reject) {
549549
delete cache[i].TableName;
550550
delete cache[i].confidential;
551551
payment.Statements.push(cache[i]);
552-
};
552+
}
553553

554554
//clear transactin status
555555
ChainSQL.transaction = false;
556556
ChainSQL.cache = [];
557557

558558
getTxJson(ChainSQL, payment).then(function (data) {
559-
if (data.status == 'error') {
560-
ChainSQL.transaction = false;
561-
throw new Error('getTxJson error');
562-
}
563-
564559
var payment = data.tx_json;
565560
payment.Statements = convertStringToHex(JSON.stringify(payment.Statements));
566561
ChainSQL.api.prepareTx(payment).then(function (data) {
@@ -573,6 +568,12 @@ function handleCommit(ChainSQL, object, resolve, reject) {
573568
}).catch(function (error) {
574569
cb(error, null);
575570
});
571+
}).catch(function (error) {
572+
ChainSQL.transaction = false;
573+
if (error.error_message)
574+
throw new Error(error.error_message);
575+
else
576+
throw new Error('getTxJson error');
576577
});
577578
});
578579
}
@@ -718,16 +719,15 @@ function prepareTable(ChainSQL, payment, object, resolve, reject) {
718719
ChainSQL.api.prepareTable(payment).then(function (tx_json) {
719720
// console.log(tx_json);
720721
getTxJson(ChainSQL, JSON.parse(tx_json.txJSON)).then(function (data) {
721-
if (data.status == 'error') {
722-
if (data.error_message)
723-
errFunc(new Error(data.error_message));
724-
else
725-
errFunc(new Error('getTxJson error'));
726-
}
727722
data.tx_json.Fee = util.calcFee(data.tx_json);
728723
var payment = data.tx_json;
729724
let signedRet = ChainSQL.api.sign(JSON.stringify(data.tx_json), ChainSQL.connect.secret);
730725
handleSignedTx(ChainSQL, signedRet, object, resolve, reject);
726+
}).catch(function (error) {
727+
if (error.error_message)
728+
errFunc(new Error(error.error_message));
729+
else
730+
errFunc(new Error('getTxJson error'));
731731
});
732732
}).catch(function (error) {
733733
errFunc(error);
@@ -794,53 +794,42 @@ ChainsqlAPI.prototype.getAccountTables = function(address, bGetDetailInfo=false)
794794
account: address,
795795
detail: bGetDetailInfo
796796
}).then(function(data){
797-
dealWithRet(data,resolve,reject);
797+
resolve(data);
798798
}).catch(function(err){
799799
reject(err);
800-
})
801-
})
802-
}
800+
});
801+
});
802+
};
803803

804804
ChainsqlAPI.prototype.getTableAuth = function(owner,tableName,accounts){
805805
var connection = this.api ? this.api.connection : this.connect.api.connection;
806806
var req = {
807807
command: 'table_auth',
808808
owner: owner,
809809
tablename:tableName
810-
}
810+
};
811811
if(accounts && accounts.length > 0){
812812
req.accounts = accounts;
813813
}
814814
return new Promise(function(resolve, reject){
815815
connection.request(req).then(function(data){
816-
dealWithRet(data,resolve,reject);
816+
resolve(data);
817817
}).catch(function(err){
818818
reject(err);
819-
})
820-
})
821-
}
819+
});
820+
});
821+
};
822822

823-
function dealWithRet(data,resolve,reject){
824-
if(data.status == 'success'){
825-
resolve(data);
826-
}else{
827-
reject(data);
828-
}
829-
}
830823
ChainsqlAPI.prototype.getTableNameInDB = function(owner,tableName){
831824
var that = this;
832825
return new Promise(function(resolve, reject){
833826
util.getTableName(that,owner,tableName).then(function(data){
834-
if(data.status == 'success'){
835-
resolve(data.nameInDB);
836-
}else{
837-
reject(data);
838-
}
827+
resolve(data.nameInDB);
839828
}).catch(function(err){
840829
reject(err);
841-
})
842-
})
843-
}
830+
});
831+
});
832+
};
844833

845834
ChainsqlAPI.prototype.getBySqlAdmin = function(sql){
846835
var connection = this.api ? this.api.connection : this.connect.api.connection;
@@ -849,19 +838,19 @@ ChainsqlAPI.prototype.getBySqlAdmin = function(sql){
849838
command: 'r_get_sql_admin',
850839
sql:sql
851840
}).then(function(data){
852-
dealWithRet(data,resolve,reject);
841+
resolve(data);
853842
}).catch(function(err){
854843
reject(err);
855-
})
856-
})
857-
}
844+
});
845+
});
846+
};
858847

859848
ChainsqlAPI.prototype.getBySqlUser = function(sql){
860849
var connect = this.connect;
861850
var json = {
862851
Account:connect.address,
863852
Sql:sql
864-
}
853+
};
865854
return new Promise(function(resolve, reject){
866855
util.getValidatedLedgerIndex(connect).then(function(ledgerVersion){
867856
json.LedgerIndex = ledgerVersion;
@@ -875,14 +864,12 @@ ChainsqlAPI.prototype.getBySqlUser = function(sql){
875864
tx_json:json
876865
})
877866
}).then(function(data){
878-
dealWithRet(data,resolve,reject);
867+
resolve(data);
879868
}).catch(function(err) {
880869
reject(err);
881870
});
882-
})
883-
884-
885-
}
871+
});
872+
};
886873

887874
ChainsqlAPI.prototype.submit = function (cb) {
888875
var that = this;

src/table.js

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,6 @@ function prepareTable(ChainSQL, payment, object, resolve, reject) {
372372

373373
connect.api.prepareTable(payment).then(function(tx_json) {
374374
getTxJson(ChainSQL, JSON.parse(tx_json.txJSON)).then(function(data) {
375-
if (data.status == 'error') {
376-
throw new Error('getTxJson error');
377-
}
378375
data.tx_json.Fee = util.calcFee(data.tx_json);
379376
//var payment = data.tx_json;
380377
var signedRet = connect.api.sign(JSON.stringify(data.tx_json), ChainSQL.connect.secret);
@@ -434,6 +431,8 @@ function prepareTable(ChainSQL, payment, object, resolve, reject) {
434431
}).catch(function (error) {
435432
throw new Error(error);
436433
});
434+
}).catch(function(error) {
435+
cb(error, null);
437436
});
438437
}).catch(function (error) {
439438
cb(error, null);
@@ -446,53 +445,48 @@ function handleGetRecord(ChainSQL, object, resolve, reject) {
446445

447446
var isFunction = false;
448447
if ((typeof object) === 'function')
449-
isFunction = true
448+
isFunction = true;
450449

451450
var cb = function(error, data) {
452451
if (isFunction) {
453-
object(error, data)
452+
object(error, data);
454453
} else {
455454
if (error) {
456455
reject(error);
457456
} else {
458457
resolve(data);
459458
}
460459
}
461-
}
460+
};
462461

463462
var connect = ChainSQL.connect;
464-
//console.log('select \n\t', JSON.stringify(ChainSQL.query));
465-
var json = {
466-
Account:connect.address,
467-
Owner: connect.scope,
468-
Tables: [{
469-
Table: {
470-
TableName: ChainSQL.tab
471-
}
472-
}],
473-
Raw: JSON.stringify(ChainSQL.query)
474-
}
475-
util.getValidatedLedgerIndex(connect).then(function(ledgerVersion){
476-
json.LedgerIndex = ledgerVersion;
477-
return util.signData(JSON.stringify(json), ChainSQL.connect.secret);
478-
}).then(function(signed){
479-
return connect.api.connection.request({
480-
command: 'r_get',
481-
publicKey:signed.publicKey,
482-
signature:signed.signature,
483-
signingData:JSON.stringify(json),
484-
tx_json:json
485-
})
486-
}).then(function(data) {
487-
if (data.status != 'success'){
488-
cb(data, null);
489-
}else{
490-
cb(null, data);
491-
}
492-
}).catch(function(err) {
463+
//console.log('select \n\t', JSON.stringify(ChainSQL.query));
464+
var json = {
465+
Account: connect.address,
466+
Owner: connect.scope,
467+
Tables: [{
468+
Table: {
469+
TableName: ChainSQL.tab
470+
}
471+
}],
472+
Raw: JSON.stringify(ChainSQL.query)
473+
};
474+
util.getValidatedLedgerIndex(connect).then(function (ledgerVersion) {
475+
json.LedgerIndex = ledgerVersion;
476+
return util.signData(JSON.stringify(json), ChainSQL.connect.secret);
477+
}).then(function (signed) {
478+
return connect.api.connection.request({
479+
command: 'r_get',
480+
publicKey: signed.publicKey,
481+
signature: signed.signature,
482+
signingData: JSON.stringify(json),
483+
tx_json: json
484+
});
485+
}).then(function (data) {
486+
cb(null, data);
487+
}).catch(function (err) {
493488
cb(err, null);
494-
})
495-
489+
});
496490
}
497491

498492
Table.prototype.submit = function(cb) {

src/util.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,22 @@ function decodeToken(that, token) {
5050

5151

5252
function getTxJson(that, tx_json) {
53-
var connection = that.api;
54-
if (!connection) {
55-
connection = that.connect.api.connection;
56-
} else {
57-
connection = that.api.connection;
58-
}
59-
return connection.request({
60-
command: 't_prepare',
61-
tx_json: tx_json
62-
}).then(function(data) {
63-
return data;
64-
})
53+
var connection = that.api;
54+
if (!connection) {
55+
connection = that.connect.api.connection;
56+
} else {
57+
connection = that.api.connection;
58+
}
59+
return new Promise(function (resolve, reject) {
60+
connection.request({
61+
command: 't_prepare',
62+
tx_json: tx_json
63+
}).then(function (data) {
64+
resolve(data);
65+
}).catch(function (err) {
66+
reject(err);
67+
});
68+
});
6569
}
6670

6771
function signData(message,secret){
@@ -92,13 +96,13 @@ function getUserToken(connection,owner,user,name) {
9296
}
9397

9498
}).then(function(data) {
95-
if(data.status == 'error') throw new Error(data.error_message);
9699
var json = {};
97100
json[owner + name] = data.token;
98101
return json;
99102
}).catch(function(err){
100-
console.error(err);
101-
return {'status':'error'};
103+
// console.error(err);
104+
// return {'status':'error'};
105+
throw new Error(err);
102106
})
103107
}
104108

test/test.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,12 @@ var testCreateTable1 = async function() {
209209
confidential: false
210210
}
211211
// 创建表
212-
let rs = await c.createTable(sTableName2, raw, option).submit({expect:'db_success'});
213-
console.log("testCreateTable1" , rs)
212+
try {
213+
let rs = await c.createTable(sTableName2, raw, option).submit({expect:'db_success'});
214+
console.log("testCreateTable1" , rs)
215+
} catch (error) {
216+
console.log(error);
217+
}
214218
};
215219

216220
//重复插入的情况下报异常
@@ -453,8 +457,13 @@ function callback(err,data){
453457

454458
async function testAccountTables()
455459
{
456-
let retRequest = await c.getAccountTables("z4ypskpHPpMDtHsZvFHg8eDEdTjQrYYYV6",true)
457-
console.log(retRequest)
460+
try {
461+
let retRequest = await c.getAccountTables("zn4X2eXBBaWtkJkxvXLBczQ1mRdpUCfXH3",true);
462+
console.log(retRequest);
463+
} catch (error) {
464+
console.error(error);
465+
}
466+
458467
}
459468

460469
async function testTableAuth(){

0 commit comments

Comments
 (0)