Fix response error handling for smart queries

This commit is contained in:
Simon Warta 2021-10-28 16:29:41 +02:00
parent a4d4d4382f
commit e754fad581
2 changed files with 12 additions and 2 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

@ -120,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}`);
}
},
},