You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
query.compute.queryContract throws errors in some cases and returns error strings in other cases (like when a query does not exist), which is confusing and problematic, especially because strings are valid responses from queries. thus the caller cannot verify if a string is an error or a successful response from a contract.
i suspect the error strings should actually be thrown as errors instead, unless i'm misunderstanding the purpose here.
in src/query/compute.ts:
/** * Query a Secret Contract. * May return a string on error. */asyncqueryContract<Textendsobject,Rextendsany>({contract_address: contractAddress,code_hash: codeHash,
query,}: QueryContractRequest<T>,headers?: HeadersInit,): Promise<R>{if(!codeHash){console.warn(getMissingCodeHashWarning("queryContract()"));({code_hash: codeHash}=awaitthis.codeHashByContractAddress({contract_address: contractAddress,}));}codeHash=codeHash!.replace("0x","").toLowerCase();constencryptedQuery=awaitthis.encryption!.encrypt(codeHash,query);constnonce=encryptedQuery.slice(0,32);try{const{data: encryptedResult}=awaitQuery.QuerySecretContract({contract_address: contractAddress,query: encryptedQuery,},{
headers,pathPrefix: this.url,},);constdecryptedBase64Result=awaitthis.encryption!.decrypt(fromBase64(encryptedResultasunknownasstring)!,nonce,);// Don't try to parse JSON in case the result is empty.// This seems kinda stupid but if the contract for some reason returns `Binary::default()`// the JSON parsing will fail (empty bytes)if(!decryptedBase64Result?.length){return{}asR;}returnJSON.parse(fromUtf8(fromBase64(fromUtf8(decryptedBase64Result))));}catch(err){try{consterrorMessageRgx=/encrypted: (.+?): (?:instantiate|execute|query|reply to|migrate) contract failed/g;constrgxMatches=errorMessageRgx.exec(err.message);if(rgxMatches==null||rgxMatches?.length!=2){throwerr;}constencryptedError=fromBase64(rgxMatches[1]);constdecryptedBase64Error=awaitthis.encryption!.decrypt(encryptedError,nonce,);try{//@ts-ignore// return the error stringreturnfromUtf8(fromBase64(fromUtf8(decryptedBase64Error)));}catch(parseError){if(parseError.message==="Invalid base64 string format"){//@ts-ignore// return the error stringreturnfromUtf8(decryptedBase64Error);}else{throwerr;}}}catch(decryptionError){throwerr;}}}
i feel like the return statements in the main catch statement should probably be throw new Error(...) instead.
The text was updated successfully, but these errors were encountered:
query.compute.queryContract
throws errors in some cases and returns error strings in other cases (like when a query does not exist), which is confusing and problematic, especially because strings are valid responses from queries. thus the caller cannot verify if a string is an error or a successful response from a contract.i suspect the error strings should actually be thrown as errors instead, unless i'm misunderstanding the purpose here.
in
src/query/compute.ts
:i feel like the
return
statements in the main catch statement should probably bethrow new Error(...)
instead.The text was updated successfully, but these errors were encountered: