Merge pull request #914 from cosmos/improve-smart-query-error

Fix response error handling for smart queries
This commit is contained in:
Simon Warta 2021-10-28 17:05:55 +02:00 committed by GitHub
commit e2f67cc25e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 4 deletions

View File

@ -6,6 +6,10 @@ and this project adheres to
## [Unreleased]
### Fixed
- @cosmjs/cosmwasm-stargate: Fix response error handling for smart queries.
## [0.26.3] - 2021-10-25
### Added

View File

@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { sha256 } from "@cosmjs/crypto";
import { fromAscii, fromHex, toAscii, toHex } from "@cosmjs/encoding";
import { DirectSecp256k1HdWallet, OfflineDirectSigner, Registry } from "@cosmjs/proto-signing";

View File

@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { fromUtf8, toAscii } from "@cosmjs/encoding";
import { createPagination, createProtobufRpcClient, QueryClient } from "@cosmjs/stargate";
import {
@ -121,10 +120,16 @@ export function setupWasmExtension(base: QueryClient): WasmExtension {
const request = { address: address, queryData: toAscii(JSON.stringify(query)) };
const { data } = await queryService.SmartContractState(request);
// By convention, smart queries must return a valid JSON document (see https://github.com/CosmWasm/cosmwasm/issues/144)
let responseText: string;
try {
return JSON.parse(fromUtf8(data));
responseText = fromUtf8(data);
} catch (error) {
throw new Error("Contract did not return valid JSON data");
throw new Error(`Could not UTF-8 decode smart query response from contract: ${error}`);
}
try {
return JSON.parse(responseText);
} catch (error) {
throw new Error(`Could not JSON parse smart query response from contract: ${error}`);
}
},
},