Skip to content

Commit 1ccbaf6

Browse files
Alan CohenChris Clark
Alan Cohen
authored and
Chris Clark
committedAug 21, 2015
Fix: Emit error events and return error on pathfind
1 parent ba6c703 commit 1ccbaf6

21 files changed

+129
-66
lines changed
 

‎src/api/common/errors.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ ApiError.prototype = new RippleError();
7777
ApiError.prototype.name = 'ApiError';
7878

7979
module.exports = {
80-
ValidationError: ValidationError,
81-
NetworkError: NetworkError,
82-
TransactionError: TransactionError,
83-
RippledNetworkError: RippledNetworkError,
84-
NotFoundError: NotFoundError,
85-
MissingLedgerHistoryError: MissingLedgerHistoryError,
86-
TimeOutError: TimeOutError,
87-
ApiError: ApiError,
88-
RippleError: RippleError
80+
ValidationError,
81+
NetworkError,
82+
TransactionError,
83+
RippledNetworkError,
84+
NotFoundError,
85+
MissingLedgerHistoryError,
86+
TimeOutError,
87+
ApiError,
88+
RippleError
8989
};

‎src/api/common/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
generateAddress: utils.generateAddress,
1313
composeAsync: utils.composeAsync,
1414
wrapCatch: utils.wrapCatch,
15+
convertErrors: utils.convertErrors,
1516
convertExceptions: utils.convertExceptions,
1617
convertKeysFromSnakeCaseToCamelCase:
1718
utils.convertKeysFromSnakeCaseToCamelCase,

‎src/api/common/utils.js

+11
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ function composeAsync(wrapper: Wrapper, callback: Callback): Callback {
6666
};
6767
}
6868

69+
function convertErrors(callback: () => void): () => void {
70+
return function(error, data) {
71+
if (error && !(error instanceof errors.RippleError)) {
72+
callback(new errors.RippleError(error));
73+
} else {
74+
callback(error, data);
75+
}
76+
};
77+
}
78+
6979
function convertExceptions<T>(f: () => T): () => T {
7080
return function() {
7181
try {
@@ -105,6 +115,7 @@ module.exports = {
105115
composeAsync,
106116
wrapCatch,
107117
convertExceptions,
118+
convertErrors,
108119
convertKeysFromSnakeCaseToCamelCase,
109120
promisify
110121
};

‎src/api/ledger/accountinfo.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const utils = require('./utils');
55
const removeUndefined = require('./parse/utils').removeUndefined;
66
const validate = utils.common.validate;
77
const composeAsync = utils.common.composeAsync;
8+
const convertErrors = utils.common.convertErrors;
89

910
type AccountData = {
1011
Sequence: number,
@@ -66,10 +67,10 @@ function getAccountInfoAsync(account: string, options: AccountInfoOptions,
6667
};
6768

6869
this.remote.requestAccountInfo(request,
69-
composeAsync(formatAccountInfo, callback));
70+
composeAsync(formatAccountInfo, convertErrors(callback)));
7071
}
7172

72-
function getAccountInfo(account: string, options: AccountInfoOptions={}
73+
function getAccountInfo(account: string, options: AccountInfoOptions = {}
7374
): Promise<AccountInfoResponse> {
7475
return utils.promisify(getAccountInfoAsync).call(this, account, options);
7576
}

‎src/api/ledger/balances.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const utils = require('./utils');
66
const getTrustlines = require('./trustlines');
77
const validate = utils.common.validate;
88
const composeAsync = utils.common.composeAsync;
9+
const convertErrors = utils.common.convertErrors;
910

1011
function getTrustlineBalanceAmount(trustline) {
1112
return {
@@ -39,10 +40,10 @@ function getBalancesAsync(account, options, callback) {
3940
async.parallel({
4041
xrp: _.partial(utils.getXRPBalance, this.remote, account, ledgerVersion),
4142
trustlines: _.partial(getTrustlinesAsync.bind(this), account, options)
42-
}, composeAsync(formatBalances, callback));
43+
}, composeAsync(formatBalances, convertErrors(callback)));
4344
}
4445

45-
function getBalances(account: string, options={}) {
46+
function getBalances(account: string, options = {}) {
4647
return utils.promisify(getBalancesAsync).call(this, account, options);
4748
}
4849

‎src/api/ledger/ledger.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const utils = require('./utils');
44
const validate = utils.common.validate;
55
const composeAsync = utils.common.composeAsync;
6+
const convertErrors = utils.common.convertErrors;
67
const parseLedger = require('./parse/ledger');
78

89
function getLedgerAsync(options, callback) {
@@ -16,10 +17,11 @@ function getLedgerAsync(options, callback) {
1617
};
1718

1819
this.remote.requestLedger(request,
19-
composeAsync(response => parseLedger(response.ledger), callback));
20+
composeAsync(response => parseLedger(response.ledger),
21+
convertErrors(callback)));
2022
}
2123

22-
function getLedger(options={}) {
24+
function getLedger(options = {}) {
2325
return utils.promisify(getLedgerAsync).call(this, options);
2426
}
2527

‎src/api/ledger/orderbook.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
const _ = require('lodash');
44
const async = require('async');
55
const utils = require('./utils');
6-
const parseOrderbookOrder = require('./parse/orderbook-order');
76
const validate = utils.common.validate;
87
const composeAsync = utils.common.composeAsync;
8+
const convertErrors = utils.common.convertErrors;
9+
const parseOrderbookOrder = require('./parse/orderbook-order');
910

1011
// account is to specify a "perspective", which affects which unfunded offers
1112
// are returned
@@ -18,7 +19,7 @@ function getBookOffers(remote, account, ledgerVersion, limit,
1819
ledger: ledgerVersion || 'validated',
1920
limit: limit,
2021
taker: account
21-
}), composeAsync(data => data.offers, callback));
22+
}), composeAsync(data => data.offers, convertErrors(callback)));
2223
}
2324

2425
function isSameIssue(a, b) {
@@ -77,7 +78,7 @@ function getOrderbookAsync(account, orderbook, options, callback) {
7778
callback));
7879
}
7980

80-
function getOrderbook(account: string, orderbook: Object, options={}) {
81+
function getOrderbook(account: string, orderbook: Object, options = {}) {
8182
return utils.promisify(getOrderbookAsync).call(this,
8283
account, orderbook, options);
8384
}

‎src/api/ledger/orders.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ const _ = require('lodash');
44
const utils = require('./utils');
55
const validate = utils.common.validate;
66
const composeAsync = utils.common.composeAsync;
7+
const convertErrors = utils.common.convertErrors;
78
const parseAccountOrder = require('./parse/account-order');
89

9-
function requestAccountOffers(remote, address, ledgerVersion, options,
10-
marker, limit, callback
10+
function requestAccountOffers(remote, address, ledgerVersion, marker, limit,
11+
callback
1112
) {
1213
remote.requestAccountOffers({
1314
account: address,
@@ -18,7 +19,7 @@ function requestAccountOffers(remote, address, ledgerVersion, options,
1819
composeAsync((data) => ({
1920
marker: data.marker,
2021
results: data.offers.map(_.partial(parseAccountOrder, address))
21-
}), callback));
22+
}), convertErrors(callback)));
2223
}
2324

2425
function getOrdersAsync(account, options, callback) {
@@ -28,13 +29,13 @@ function getOrdersAsync(account, options, callback) {
2829
const ledgerVersion = options.ledgerVersion
2930
|| this.remote.getLedgerSequence();
3031
const getter = _.partial(requestAccountOffers, this.remote, account,
31-
ledgerVersion, options);
32+
ledgerVersion);
3233
utils.getRecursive(getter, options.limit,
3334
composeAsync((orders) => _.sortBy(orders,
3435
(order) => order.properties.sequence), callback));
3536
}
3637

37-
function getOrders(account: string, options={}) {
38+
function getOrders(account: string, options = {}) {
3839
return utils.promisify(getOrdersAsync).call(this, account, options);
3940
}
4041

‎src/api/ledger/pathfind.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const validate = utils.common.validate;
88
const parsePathfind = require('./parse/pathfind');
99
const NotFoundError = utils.common.errors.NotFoundError;
1010
const composeAsync = utils.common.composeAsync;
11+
const convertErrors = utils.common.convertErrors;
1112

1213
type PathFindParams = {
1314
src_currencies?: Array<string>, src_account: string, dst_amount: string,
@@ -47,7 +48,7 @@ function requestPathFind(remote, pathfind: PathFind, callback) {
4748
}
4849

4950
remote.createPathFind(params,
50-
composeAsync(_.partial(addParams, params), callback));
51+
composeAsync(_.partial(addParams, params), convertErrors(callback)));
5152
}
5253

5354
function addDirectXrpPath(paths, xrpBalance) {

‎src/api/ledger/settings.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const validate = utils.common.validate;
66
const parseFields = require('./parse/fields');
77
const composeAsync = utils.common.composeAsync;
88
const AccountFlags = utils.common.constants.AccountFlags;
9+
const convertErrors = utils.common.convertErrors;
910

1011
function parseFlags(value) {
1112
const settings = {};
@@ -34,10 +35,10 @@ function getSettingsAsync(account, options, callback) {
3435
};
3536

3637
this.remote.requestAccountInfo(request,
37-
composeAsync(formatSettings, callback));
38+
composeAsync(formatSettings, convertErrors(callback)));
3839
}
3940

40-
function getSettings(account: string, options={}) {
41+
function getSettings(account: string, options = {}) {
4142
return utils.promisify(getSettingsAsync).call(this, account, options);
4243
}
4344

‎src/api/ledger/transaction.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const utils = require('./utils');
66
const parseTransaction = require('./parse/transaction');
77
const validate = utils.common.validate;
88
const errors = utils.common.errors;
9+
const convertErrors = utils.common.convertErrors;
910
const RippleError = require('../../core/rippleerror').RippleError;
1011

1112
import type {Remote} from '../../core/remote';
@@ -69,9 +70,9 @@ function getTransactionAsync(identifier: string, options: TransactionOptions,
6970
} else if (!error && tx && !isTransactionInRange(tx, options)) {
7071
callback(new errors.NotFoundError('Transaction not found'));
7172
} else if (error) {
72-
callback(error);
73+
convertErrors(callback)(error);
7374
} else if (!tx) {
74-
callback(new Error('Internal error'));
75+
callback(new errors.ApiError('Internal error'));
7576
} else {
7677
callback(error, parseTransaction(tx));
7778
}
@@ -85,7 +86,7 @@ function getTransactionAsync(identifier: string, options: TransactionOptions,
8586
}
8687

8788
function getTransaction(identifier: string,
88-
options: TransactionOptions={}
89+
options: TransactionOptions = {}
8990
): Promise<GetTransactionResponse> {
9091
return utils.promisify(getTransactionAsync).call(this, identifier, options);
9192
}

‎src/api/ledger/transactions.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const parseTransaction = require('./parse/transaction');
77
const getTransaction = require('./transaction');
88
const validate = utils.common.validate;
99
const composeAsync = utils.common.composeAsync;
10+
const convertErrors = utils.common.convertErrors;
1011

1112
function parseAccountTxTransaction(tx) {
1213
// rippled uses a different response format for 'account_tx' than 'tx'
@@ -55,6 +56,17 @@ function orderFilter(options, tx) {
5556
utils.compareTransactions(tx, options.startTx) < 0);
5657
}
5758

59+
function formatPartialResponse(address, options, data) {
60+
return {
61+
marker: data.marker,
62+
results: data.transactions
63+
.filter((tx) => tx.validated)
64+
.map(parseAccountTxTransaction)
65+
.filter(_.partial(transactionFilter, address, options))
66+
.filter(_.partial(orderFilter, options))
67+
};
68+
}
69+
5870
function getAccountTx(remote, address, options, marker, limit, callback) {
5971
const params = {
6072
account: address,
@@ -66,16 +78,9 @@ function getAccountTx(remote, address, options, marker, limit, callback) {
6678
marker: marker
6779
};
6880

69-
remote.requestAccountTx(params, (error, data) => {
70-
return error ? callback(error) : callback(null, {
71-
marker: data.marker,
72-
results: data.transactions
73-
.filter((tx) => tx.validated)
74-
.map(parseAccountTxTransaction)
75-
.filter(_.partial(transactionFilter, address, options))
76-
.filter(_.partial(orderFilter, options))
77-
});
78-
});
81+
remote.requestAccountTx(params,
82+
composeAsync(_.partial(formatPartialResponse, address, options),
83+
convertErrors(callback)));
7984
}
8085

8186
function checkForLedgerGaps(remote, options, transactions) {
@@ -131,7 +136,7 @@ function getTransactionsAsync(account, options, callback) {
131136
}
132137
}
133138

134-
function getTransactions(account: string, options={}) {
139+
function getTransactions(account: string, options = {}) {
135140
return utils.promisify(getTransactionsAsync).call(this, account, options);
136141
}
137142

‎src/api/ledger/trustlines.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@
33
const _ = require('lodash');
44
const utils = require('./utils');
55
const validate = utils.common.validate;
6+
const composeAsync = utils.common.composeAsync;
7+
const convertErrors = utils.common.convertErrors;
68
const parseAccountTrustline = require('./parse/account-trustline');
79

810
function currencyFilter(currency, trustline) {
911
return currency === null || trustline.specification.currency === currency;
1012
}
1113

14+
function formatResponse(options, data) {
15+
return {
16+
marker: data.marker,
17+
results: data.lines.map(parseAccountTrustline)
18+
.filter(_.partial(currencyFilter, options.currency || null))
19+
};
20+
}
21+
1222
function getAccountLines(remote, address, ledgerVersion, options, marker, limit,
1323
callback
1424
) {
@@ -20,14 +30,9 @@ function getAccountLines(remote, address, ledgerVersion, options, marker, limit,
2030
peer: options.counterparty
2131
};
2232

23-
remote.requestAccountLines(requestOptions, (error, data) => {
24-
return error ? callback(error) :
25-
callback(null, {
26-
marker: data.marker,
27-
results: data.lines.map(parseAccountTrustline)
28-
.filter(_.partial(currencyFilter, options.currency || null))
29-
});
30-
});
33+
remote.requestAccountLines(requestOptions,
34+
composeAsync(_.partial(formatResponse, options),
35+
convertErrors(callback)));
3136
}
3237

3338
function getTrustlinesAsync(account: string, options: {currency: string,
@@ -44,7 +49,7 @@ function getTrustlinesAsync(account: string, options: {currency: string,
4449
utils.getRecursive(getter, options.limit, callback);
4550
}
4651

47-
function getTrustlines(account: string, options={}) {
52+
function getTrustlines(account: string, options = {}) {
4853
return utils.promisify(getTrustlinesAsync).call(this, account, options);
4954
}
5055

‎src/api/transaction/submit.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
const utils = require('./utils');
44
const validate = utils.common.validate;
55
const Request = utils.common.core.Request;
6+
const convertErrors = utils.common.convertErrors;
67

7-
function submitAsync(txBlob: string, callback: (err: any, data: any) => void):
8-
void {
8+
function submitAsync(txBlob: string, callback: (err: any, data: any) => void
9+
): void {
910
validate.blob(txBlob);
1011
const request = new Request(this.remote, 'submit');
1112
request.message.tx_blob = txBlob;
1213
request.request(null,
1314
utils.common.composeAsync(
1415
data => utils.common.convertKeysFromSnakeCaseToCamelCase(data),
15-
callback));
16+
convertErrors(callback)));
1617
}
1718

1819
function submit(txBlob: string) {

‎src/core/remote.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -1816,10 +1816,7 @@ Remote.prototype.createPathFind = function(options, callback) {
18161816
callback(null, data);
18171817
}
18181818
});
1819-
pathFind.on('error', (error) => {
1820-
pathFind.close();
1821-
callback(error);
1822-
});
1819+
pathFind.on('error', callback);
18231820
}
18241821

18251822
this._cur_path_find = pathFind;

0 commit comments

Comments
 (0)
Please sign in to comment.