Fix handling of non-existent ID in getTx

This commit is contained in:
Simon Warta 2020-02-12 16:08:08 +01:00
parent fd725998c6
commit 3e5bd7c3c7
2 changed files with 23 additions and 8 deletions

View File

@ -10,6 +10,7 @@ import {
PubkeyBytes,
SendTransaction,
TokenTicker,
TransactionId,
TransactionState,
} from "@iov/bcp";
import { Random, Secp256k1 } from "@iov/crypto";
@ -264,6 +265,21 @@ describe("CosmWasmConnection", () => {
});
});
describe("getTx", () => {
it("throws for non-existent transaction", async () => {
pendingWithoutCosmos();
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultConfig);
const nonExistentId = "0000000000000000000000000000000000000000000000000000000000000000" as TransactionId;
await connection.getTx(nonExistentId).then(
() => fail("this must not succeed"),
error => expect(error).toMatch(/transaction does not exist/i),
);
connection.disconnect();
});
});
describe("integration tests", () => {
it("can post and get a transaction", async () => {
pendingWithoutCosmos();

View File

@ -231,15 +231,14 @@ export class CosmWasmConnection implements BlockchainConnection {
public async getTx(
id: TransactionId,
): Promise<ConfirmedAndSignedTransaction<UnsignedTransaction> | FailedTransaction> {
try {
// tslint:disable-next-line: deprecation
const response = await this.restClient.txsById(id);
return this.parseAndPopulateTxResponseSigned(response);
} catch (error) {
if (error.response.status === 404) {
const results = await this.cosmWasmClient.searchTx({ id: id });
switch (results.length) {
case 0:
throw new Error("Transaction does not exist");
}
throw error;
case 1:
return this.parseAndPopulateTxResponseSigned(results[0]);
default:
throw new Error("Got unexpected amount of search results");
}
}