Merge pull request #914 from cosmos/improve-smart-query-error
Fix response error handling for smart queries
This commit is contained in:
commit
e2f67cc25e
@ -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
|
||||
|
||||
@ -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";
|
||||
|
||||
@ -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}`);
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user