Fix handling of non-existent ID in getTx
This commit is contained in:
parent
fd725998c6
commit
3e5bd7c3c7
@ -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();
|
||||
|
||||
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user