Skip to content

Commit

Permalink
Improve some edge cases in decoding of the error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
DOBEN committed Jun 21, 2024
1 parent d7f757f commit ef60b3c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
22 changes: 16 additions & 6 deletions front-end-tools/src/components/ReadComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ export default function ReadComponenet(props: ConnectionProps) {

promise
.then((res) => {
if (!res || res.tag === 'failure' || !res.returnValue) {
const er = parseError(res, contractName, contractIndex, entryPoint, schema);
setAddDisclaimer(er?.addDisclaimer);
setErrorContractInvoke(er?.errors);
if (res.tag === 'failure') {
const parsedError = parseError(res, contractName, contractIndex, entryPoint, schema);
setAddDisclaimer(parsedError?.addDisclaimer);
setErrorContractInvoke(parsedError?.errors);
} else {
const parsedValue = parseResult(res, contractName, contractIndex, entryPoint, schema);
setReturnValue(parsedValue);
Expand Down Expand Up @@ -554,11 +554,21 @@ export default function ReadComponenet(props: ConnectionProps) {
))}

<br />
<a href="https://developer.concordium.software/en/mainnet/smart-contracts/tutorials/piggy-bank/deploying.html#concordium-std-crate-errors">
<a
className="link"
target="_blank"
rel="noreferrer"
href="https://developer.concordium.software/en/mainnet/smart-contracts/tutorials/piggy-bank/deploying.html#concordium-std-crate-errors"
>
Developer documentation: Explanation of errors
</a>
<br />
<a href="https://docs.rs/concordium-std/latest/concordium_std/#signalling-errors">
<a
className="link"
target="_blank"
rel="noreferrer"
href="https://docs.rs/concordium-std/latest/concordium_std/#signalling-errors"
>
`Concordium-std` crate signalling errors
</a>
</Alert>
Expand Down
29 changes: 9 additions & 20 deletions front-end-tools/src/reading_from_blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ export function parseResult(
) {
const fullEntryPointName = `${contractName.value}.${entryPoint.value}`;

if (!res.returnValue) {
if (!res.returnValue || !res.returnValue?.buffer.length) {
throw new Error(
`RPC call 'invokeContract' on method '${fullEntryPointName}' of contract '${contractIndex}' returned no return_value`
`RPC call 'invokeContract' on method '${fullEntryPointName}' of contract '${contractIndex}' returned no return_value.`
);
}

Expand Down Expand Up @@ -279,33 +279,22 @@ export function parseError(
moduleSchema: string | undefined
) {
const fullEntryPointName = `${contractName.value}.${entryPoint.value}`;
const returnValue = { addDisclaimer: false, errors: [] as string[] };

if (!res || res.tag === 'failure') {
if (res.tag === 'failure') {
const [rejectReasonCode, humanReadableError] = decodeRejectReason(res, contractName, entryPoint, moduleSchema);

const errors = [];

if (humanReadableError) {
errors.push(`Prettified reject reason: ${humanReadableError}.`);
returnValue.errors.push(`Prettified reject reason: ${humanReadableError}.`);
returnValue.addDisclaimer = true;
}
if (rejectReasonCode) {
errors.push(`Reject reason code: ${rejectReasonCode}.`);
returnValue.errors.push(`Reject reason code: ${rejectReasonCode}.`);
}
errors.push(
returnValue.errors.push(
`RPC call 'invokeContract' on method '${fullEntryPointName}' of contract '${contractIndex}' failed.`
);

return { addDisclaimer: true, errors };
}

if (!res.returnValue) {
return {
addDisclaimer: false,
errors: [
`RPC call 'invokeContract' on method '${fullEntryPointName}' of contract '${contractIndex}' returned no return_value.`,
],
};
}

return undefined;
return returnValue;
}
17 changes: 8 additions & 9 deletions front-end-tools/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,19 @@ export function decodeRejectReason(
// to deserialize the error with the schema before falling back to decode the `concordium-std` crate errors manually.
// Only the `NotPayableError` is treated special since it has no matched error in the errorSchema.
if (moduleSchema !== undefined && failedResult.returnValue !== undefined) {
const decodedError = deserializeReceiveError(
ReturnValue.toBuffer(failedResult.returnValue),
toBuffer(moduleSchema, 'base64'),
contractName,
entryPoint
);

if (decodedError !== undefined) {
try {
const decodedError = deserializeReceiveError(
ReturnValue.toBuffer(failedResult.returnValue),
toBuffer(moduleSchema, 'base64'),
contractName,
entryPoint
);
// The object only includes one key. Its key is the human-readable error string.
const key = Object.keys(decodedError);

// Convert the human-readable error to a JSON string.
humanReadableError = JSON.stringify(key);
} else {
} catch (_error) {
// Falling back to decode the `concordium-std` crate errors manually
// Decode the `rejectReason` based on the error codes defined in `concordium-std` crate, and decode it into a human-readable string if possible.
humanReadableError = decodeConcordiumStdError(rejectReasonCode);
Expand Down

0 comments on commit ef60b3c

Please sign in to comment.