Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
feat: handle errors in getTransaction endpoints
Browse files Browse the repository at this point in the history
Co-authored-by: Konstantin Shuplenkov <konstantin.shuplenkov@dash.org>
Co-authored-by: Ivan Shumkov <ivan@shumkov.ru>
  • Loading branch information
3 people committed Feb 21, 2020
1 parent 434d91b commit e0d36ae
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 5 deletions.
11 changes: 10 additions & 1 deletion lib/grpcServer/handlers/core/getTransactionHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ function getTransactionHandlerFactory(insightAPI) {
throw new InvalidArgumentGrpcError('id is not specified');
}

const serializedTransaction = await insightAPI.getRawTransactionById(id);
let serializedTransaction;
try {
serializedTransaction = await insightAPI.getRawTransactionById(id);
} catch (e) {
if (e.statusCode === 404) {
throw new InvalidArgumentGrpcError('Transaction not found');
}

throw e;
}

const transaction = new Transaction(serializedTransaction);

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
"devDependencies": {
"@dashevo/dp-services-ctl": "~0.11.0-dev.1",
"@dashevo/dapi-client": "~0.9.0-dev.5",
"@dashevo/dapi-client": "~0.9.0-dev.6",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const startDapi = require('@dashevo/dp-services-ctl/lib/services/startDapi');

const {
Transaction,
PrivateKey,
} = require('@dashevo/dashcore-lib');

describe('getTransactionHandlerFactor', function main() {
this.timeout(260000);

let removeDapi;
let dapiClient;
let transaction;
let transactionId;

beforeEach(async () => {
const {
dapiCore,
dashCore,
remove,
} = await startDapi();

removeDapi = remove;

dapiClient = dapiCore.getApi();

const coreAPI = dashCore.getApi();

await coreAPI.generate(1000);

const { result: fromAddress } = await coreAPI.getNewAddress();
const { result: privateKeyString } = await coreAPI.dumpPrivKey(fromAddress);
const { result: toAddress } = await coreAPI.getNewAddress();

const privateKey = new PrivateKey(privateKeyString);
await coreAPI.generate(500);
await coreAPI.sendToAddress(fromAddress, 10);
await coreAPI.generate(10);

const { items: unspent } = await dapiClient.getUTXO(fromAddress);

const amount = 10000;

transaction = new Transaction();

transaction.from(unspent)
.to(toAddress, amount)
.change(fromAddress)
.fee(668)
.sign(privateKey);

({ result: transactionId } = await coreAPI.sendRawTransaction(transaction.serialize()));
});

afterEach(async () => {
await removeDapi();
});

it('should respond with a transaction by it\'s ID', async () => {
const result = await dapiClient.getTransaction(transactionId);
const receivedTx = new Transaction(Buffer.from(result));
expect(receivedTx.toString('hex')).to.deep.equal(transaction.serialize());
});

it('should respond with an invalid argument error if no transaction were found', async () => {
const nonExistentId = Buffer.alloc(32).toString('hex');
try {
await dapiClient.getTransaction(nonExistentId);
expect.fail('Error was not thrown');
} catch (e) {
expect(e.message).to.equal('3 INVALID_ARGUMENT: Invalid argument: Transaction not found');
}
});
});

0 comments on commit e0d36ae

Please sign in to comment.