Skip to content

Commit

Permalink
add nicer error messages to IDL encode and decode
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Oct 30, 2024
1 parent 416fac5 commit b1e76ef
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
Binary file modified canister_templates/stable.wasm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export async function execute(
);
const message = new TextDecoder('utf8').decode(memory);

throw message;
throw new Error(message);
}
}
// env: {
Expand Down
24 changes: 23 additions & 1 deletion src/lib/stable/execute_with_candid_serde.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function decodeArgs(
mode === 'query' ||
mode === 'update'
) {
return IDL.decode(paramIdlTypes, args[0]);
return idlDecode(paramIdlTypes, args[0]);
} else {
return [];
}
Expand All @@ -60,3 +60,25 @@ function encodeResultAndReply(

reply({ data: unencodedResult, idlType: returnIdlType });
}

export function idlEncode(
argTypes: Array<IDL.Type<any>>,
args: any[]
): Uint8Array {
try {
return new Uint8Array(IDL.encode(argTypes, args));
} catch (error) {
throw new Error(`Failed to encode Candid arguments: ${error}`);
}
}

export function idlDecode(
retTypes: IDL.Type[],
bytes: ArrayBuffer
): JsonValue[] {
try {
return IDL.decode(retTypes, bytes);
} catch (error) {
throw new Error(`Failed to decode Candid bytes: ${error}`);
}
}
10 changes: 5 additions & 5 deletions src/lib/stable/ic_apis/call.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IDL } from '@dfinity/candid';
import { Principal } from '@dfinity/principal';
import { v4 } from 'uuid'; // TODO is uuid experimental?
import { v4 } from 'uuid';

import { idlDecode, idlEncode } from '../execute_with_candid_serde';

export async function call<Args extends any[] | undefined, Return = any>(
canisterId: Principal | string,
Expand Down Expand Up @@ -40,7 +42,7 @@ export async function call<Args extends any[] | undefined, Return = any>(
} else {
const idlType =
returnTypeIdl === undefined ? [] : [returnTypeIdl];
resolve(IDL.decode(idlType, result)[0] as Return);
resolve(idlDecode(idlType, result)[0] as Return);
}

delete globalThis._azleResolveIds[globalResolveId];
Expand All @@ -64,9 +66,7 @@ export async function call<Args extends any[] | undefined, Return = any>(
: canisterId;
const canisterIdBytes = canisterIdPrincipal.toUint8Array();
const argsRaw =
raw === undefined
? new Uint8Array(IDL.encode(paramIdlTypes, args))
: raw;
raw === undefined ? idlEncode(paramIdlTypes, args) : raw;
const paymentString = payment.toString();

// TODO consider finally, what if deletion goes wrong
Expand Down
7 changes: 3 additions & 4 deletions src/lib/stable/ic_apis/notify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IDL } from '@dfinity/candid';
import { Principal } from '@dfinity/principal';

import { idlEncode } from '../execute_with_candid_serde';

/**
* Performs a cross-canister call without awaiting the result
* @param canisterId The ID of the canister to notify
Expand Down Expand Up @@ -35,10 +37,7 @@ export function notify(
? Principal.fromText(canisterId)
: canisterId;
const canisterIdBytes = canisterIdPrincipal.toUint8Array();
const argsRaw =
raw === undefined
? new Uint8Array(IDL.encode(paramIdlTypes, args))
: raw;
const argsRaw = raw === undefined ? idlEncode(paramIdlTypes, args) : raw;
const paymentString = payment.toString();

if (globalThis._azleIcExperimental !== undefined) {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/stable/ic_apis/reply.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { IDL } from '@dfinity/candid';

import { idlEncode } from '../execute_with_candid_serde';

type ReplyInput<T> =
| {
data: T;
Expand Down Expand Up @@ -37,10 +39,8 @@ export function reply<T>(input: ReplyInput<T>): void {

return globalThis._azleIcExperimental !== undefined
? globalThis._azleIcExperimental.replyRaw(
// @ts-ignore IDL.encode types are defined incorrectly https://github.com/demergent-labs/azle/issues/2061
IDL.encode(idlType, data).buffer
idlEncode(idlType, data).buffer
)
: // @ts-ignore IDL.encode types are defined incorrectly https://github.com/demergent-labs/azle/issues/2061
globalThis._azleIcStable.replyRaw(IDL.encode(idlType, data));
: globalThis._azleIcStable.replyRaw(idlEncode(idlType, data));
}
}

0 comments on commit b1e76ef

Please sign in to comment.