Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual error testing #2253

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn execute_method_js_with_result(
let record_benchmarks = WASM_DATA_REF_CELL
.with(|wasm_data_ref_cell| wasm_data_ref_cell.borrow().clone())
.as_ref()
.ok_or("could not convert wasm_data_ref_cell to ref")?
.ok_or("Could not convert wasm_data_ref_cell to ref")?
.record_benchmarks;

if record_benchmarks {
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));
bdemann marked this conversation as resolved.
Show resolved Hide resolved
} 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));
}
}