Skip to content

Commit

Permalink
Return promise error if submit result is an immediate failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Clark committed Sep 16, 2015
1 parent b43c4a7 commit 80494ad
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/api/transaction/submit.js
@@ -1,10 +1,27 @@
/* @flow */
'use strict';
const _ = require('lodash');
const utils = require('./utils');
const validate = utils.common.validate;
const Request = utils.common.core.Request;
const convertErrors = utils.common.convertErrors;

function isImmediateRejection(engineResult) {
return _.startsWith(engineResult, 'tel')
|| _.startsWith(engineResult, 'tem')
|| _.startsWith(engineResult, 'tej');
}

function convertSubmitErrors(callback) {
return function(error, data) {
if (isImmediateRejection(data.engineResult)) {
callback(new utils.common.errors.RippleError('Submit failed'), data);
} else {
callback(error, data);
}
};
}

function submitAsync(txBlob: string, callback: (err: any, data: any) => void
): void {
validate.blob(txBlob);
Expand All @@ -13,7 +30,7 @@ function submitAsync(txBlob: string, callback: (err: any, data: any) => void
request.request(null,
utils.common.composeAsync(
data => utils.common.convertKeysFromSnakeCaseToCamelCase(data),
convertErrors(callback)));
convertSubmitErrors(convertErrors(callback))));
}

function submit(txBlob: string) {
Expand Down
8 changes: 8 additions & 0 deletions test/api-test.js
Expand Up @@ -155,6 +155,14 @@ describe('RippleAPI', function() {
_.partial(checkResult, responses.submit, 'submit'));
});

it('submit - failure', function() {
return this.api.submit('BAD').then(() => {
assert(false, 'Should throw RippleError');
}).catch(error => {
assert(error instanceof this.api.errors.RippleError);
});
});

it('getBalances', function() {
return this.api.getBalances(address).then(
_.partial(checkResult, responses.getBalances, 'getBalances'));
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/api/rippled/index.js
@@ -1,7 +1,10 @@
'use strict';

module.exports = {
submit: require('./submit'),
submit: {
success: require('./submit'),
failure: require('./submit-failed')
},
ledger: require('./ledger'),
ledgerNotFound: require('./ledger-not-found'),
ledgerWithoutCloseTime: require('./ledger-without-close-time'),
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/api/rippled/submit-failed.json
@@ -0,0 +1,13 @@
{
"id": 0,
"status": "success",
"type": "response",
"result": {
"success": false,
"engine_result": "temBAD_FEE",
"engine_result_code": 1,
"engine_result_message": "",
"tx_blob": "12000322000000002400000017201B0086955468400000000000000C732102F89EAEC7667B30F33D0687BBA86C3FE2A08CCA40A9186C5BDE2DAA6FA97A37D87446304402207660BDEF67105CE1EBA9AD35DC7156BAB43FF1D47633199EE257D70B6B9AAFBF02207F5517BC8AEF2ADC1325897ECDBA8C673838048BCA62F4E98B252F19BE88796D770A726970706C652E636F6D81144FBFF73DA4ECF9B701940F27341FA8020C313443",
"tx_json": {}
}
}
6 changes: 5 additions & 1 deletion test/mock-rippled.js
Expand Up @@ -205,7 +205,11 @@ module.exports = function(port) {

mock.on('request_submit', function(request, conn) {
assert.strictEqual(request.command, 'submit');
conn.send(createResponse(request, fixtures.submit));
if (request.tx_blob === 'BAD') {
conn.send(createResponse(request, fixtures.submit.failure));
} else {
conn.send(createResponse(request, fixtures.submit.success));
}
});

mock.on('request_account_lines', function(request, conn) {
Expand Down

0 comments on commit 80494ad

Please sign in to comment.