Skip to content

Commit

Permalink
add explicit try catch on method decorators, fix some missing unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Oct 28, 2024
1 parent 573609d commit 66745fd
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 183 deletions.
Binary file modified canister_templates/stable.wasm
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
benchmarking::record_benchmark, error::handle_promise_error, quickjs_with_ctx,
benchmarking::record_benchmark, error::quickjs_call_with_error_handling, quickjs_with_ctx,
WASM_DATA_REF_CELL,
};

Expand All @@ -26,11 +26,7 @@ pub extern "C" fn execute_method_js(function_index: i32, pass_arg_data: i32) {
vec![]
};

let promise: rquickjs::Promise = method_callback
.call((candid_args,))
.map_err(|e| format!("Failed to execute method callback: {}", e))?;

handle_promise_error(ctx.clone(), promise)?;
quickjs_call_with_error_handling(ctx.clone(), method_callback, (candid_args,))?;

Ok(())
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::quickjs_with_ctx;
use crate::{error::quickjs_call_with_error_handling, quickjs_with_ctx};
use rquickjs::{Ctx, Exception, Function, IntoJs, TypedArray};

pub fn get_function(ctx: Ctx) -> Function {
Expand All @@ -21,7 +21,8 @@ pub fn get_function(ctx: Ctx) -> Function {
resolve_or_reject(ctx.clone(), &call_result, &promise_id);

Ok(())
});
})
.unwrap();
});

rquickjs::Undefined
Expand All @@ -38,9 +39,7 @@ fn resolve_or_reject<'a>(
let (should_resolve, js_value) = prepare_js_value(ctx.clone(), &call_result);
let callback = get_callback(ctx.clone(), &promise_id, should_resolve);

callback
.call::<_, rquickjs::Undefined>((js_value,))
.unwrap();
quickjs_call_with_error_handling(ctx.clone(), callback, (js_value,)).unwrap();
}

fn prepare_js_value<'a>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rquickjs::{Ctx, Function};
use slotmap::Key;

use crate::quickjs_with_ctx;
use crate::{error::quickjs_call_with_error_handling, quickjs_with_ctx};

pub fn get_function(ctx: Ctx) -> Function {
Function::new(ctx, |delay: String, callback_id: String| {
Expand All @@ -16,14 +16,16 @@ pub fn get_function(ctx: Ctx) -> Function {
.unwrap();
let timer_callback: Function = timer_callbacks.get(callback_id.as_str()).unwrap();

let result = timer_callback.call::<_, rquickjs::Value>(()).unwrap();
let result =
quickjs_call_with_error_handling(ctx.clone(), timer_callback, ()).unwrap();

if result.is_exception() {
panic!("Timer callback threw an exception");
}

Ok(())
});
})
.unwrap();
};

let timer_id: ic_cdk_timers::TimerId = ic_cdk_timers::set_timer(delay_duration, closure);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rquickjs::{Ctx, Function};
use slotmap::Key;

use crate::quickjs_with_ctx;
use crate::{error::quickjs_call_with_error_handling, quickjs_with_ctx};

pub fn get_function(ctx: Ctx) -> Function {
Function::new(ctx, |interval: String, callback_id: String| {
Expand All @@ -16,14 +16,16 @@ pub fn get_function(ctx: Ctx) -> Function {
.unwrap();
let timer_callback: Function = timer_callbacks.get(callback_id.as_str()).unwrap();

let result = timer_callback.call::<_, rquickjs::Value>(()).unwrap();
let result =
quickjs_call_with_error_handling(ctx.clone(), timer_callback, ()).unwrap();

if result.is_exception() {
panic!("Timer interval callback threw an exception");
}

Ok(())
});
})
.unwrap();
};

let timer_id: ic_cdk_timers::TimerId =
Expand Down
21 changes: 13 additions & 8 deletions src/lib/stable/canister_methods/heartbeat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde';
import { trap } from '../ic_apis';

export function heartbeat<This, Args extends any[], Return>(
originalMethod: (this: This, ...args: Args) => Return,
Expand All @@ -14,13 +15,17 @@ export function heartbeat<This, Args extends any[], Return>(
};

globalThis._azleCallbacks[indexString] = async (): Promise<void> => {
await executeAndReplyWithCandidSerde(
'heartbeat',
[],
originalMethod.bind(globalThis._azleCanisterClassInstance),
[],
undefined,
false
);
try {
await executeAndReplyWithCandidSerde(
'heartbeat',
[],
originalMethod.bind(globalThis._azleCanisterClassInstance),
[],
undefined,
false
);
} catch (error: any) {
trap(error.toString());
}
};
}
21 changes: 13 additions & 8 deletions src/lib/stable/canister_methods/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IDL } from '@dfinity/candid';

import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde';
import { trap } from '../ic_apis';

export function init<This, Args extends any[], Return>(
paramIdlTypes: IDL.Type[]
Expand All @@ -25,14 +26,18 @@ export function init<This, Args extends any[], Return>(
globalThis._azleCallbacks[indexString] = async (
...args: any[]
): Promise<void> => {
await executeAndReplyWithCandidSerde(
'init',
args,
originalMethod.bind(globalThis._azleCanisterClassInstance),
paramIdlTypes,
undefined,
false
);
try {
await executeAndReplyWithCandidSerde(
'init',
args,
originalMethod.bind(globalThis._azleCanisterClassInstance),
paramIdlTypes,
undefined,
false
);
} catch (error: any) {
trap(error.toString());
}
};
};
}
21 changes: 13 additions & 8 deletions src/lib/stable/canister_methods/inspect_message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde';
import { trap } from '../ic_apis';

// TODO explain here in a jsdoc that the dev can get the raw args using argDataRaw
export function inspectMessage<This, Args extends any[], Return>(
Expand All @@ -15,13 +16,17 @@ export function inspectMessage<This, Args extends any[], Return>(
};

globalThis._azleCallbacks[indexString] = async (): Promise<void> => {
await executeAndReplyWithCandidSerde(
'inspectMessage',
[],
originalMethod.bind(globalThis._azleCanisterClassInstance),
[],
undefined,
false
);
try {
await executeAndReplyWithCandidSerde(
'inspectMessage',
[],
originalMethod.bind(globalThis._azleCanisterClassInstance),
[],
undefined,
false
);
} catch (error: any) {
trap(error.toString());
}
};
}
21 changes: 13 additions & 8 deletions src/lib/stable/canister_methods/post_upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IDL } from '@dfinity/candid';

import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde';
import { trap } from '../ic_apis';

export function postUpgrade<This, Args extends any[], Return>(
paramIdlTypes: IDL.Type[]
Expand All @@ -25,14 +26,18 @@ export function postUpgrade<This, Args extends any[], Return>(
globalThis._azleCallbacks[indexString] = async (
...args: any[]
): Promise<void> => {
await executeAndReplyWithCandidSerde(
'postUpgrade',
args,
originalMethod.bind(globalThis._azleCanisterClassInstance),
paramIdlTypes,
undefined,
false
);
try {
await executeAndReplyWithCandidSerde(
'postUpgrade',
args,
originalMethod.bind(globalThis._azleCanisterClassInstance),
paramIdlTypes,
undefined,
false
);
} catch (error: any) {
trap(error.toString());
}
};
};
}
21 changes: 13 additions & 8 deletions src/lib/stable/canister_methods/pre_upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde';
import { trap } from '../ic_apis';

export function preUpgrade<This, Args extends any[], Return>(
originalMethod: (this: This, ...args: Args) => Return,
Expand All @@ -14,13 +15,17 @@ export function preUpgrade<This, Args extends any[], Return>(
};

globalThis._azleCallbacks[indexString] = async (): Promise<void> => {
await executeAndReplyWithCandidSerde(
'preUpgrade',
[],
originalMethod.bind(globalThis._azleCanisterClassInstance),
[],
undefined,
false
);
try {
await executeAndReplyWithCandidSerde(
'preUpgrade',
[],
originalMethod.bind(globalThis._azleCanisterClassInstance),
[],
undefined,
false
);
} catch (error: any) {
trap(error.toString());
}
};
}
21 changes: 13 additions & 8 deletions src/lib/stable/canister_methods/query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IDL } from '@dfinity/candid';

import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde';
import { trap } from '../ic_apis';

export function query<This, Args extends any[], Return>(
paramIdlTypes: IDL.Type[],
Expand Down Expand Up @@ -34,14 +35,18 @@ export function query<This, Args extends any[], Return>(
globalThis._azleCallbacks[indexString] = async (
...args: any[]
): Promise<void> => {
await executeAndReplyWithCandidSerde(
'query',
args,
originalMethod.bind(globalThis._azleCanisterClassInstance),
paramIdlTypes,
returnIdlType,
options?.manual ?? false
);
try {
await executeAndReplyWithCandidSerde(
'query',
args,
originalMethod.bind(globalThis._azleCanisterClassInstance),
paramIdlTypes,
returnIdlType,
options?.manual ?? false
);
} catch (error: any) {
trap(error.toString());
}
};
};
}
21 changes: 13 additions & 8 deletions src/lib/stable/canister_methods/update.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IDL } from '@dfinity/candid';

import { executeAndReplyWithCandidSerde } from '../execute_with_candid_serde';
import { trap } from '../ic_apis';

export function update<This, Args extends any[], Return>(
paramIdlTypes: IDL.Type[],
Expand All @@ -26,14 +27,18 @@ export function update<This, Args extends any[], Return>(
globalThis._azleCallbacks[indexString] = async (
...args: any[]
): Promise<void> => {
await executeAndReplyWithCandidSerde(
'update',
args,
originalMethod.bind(globalThis._azleCanisterClassInstance),
paramIdlTypes,
returnIdlType,
options?.manual ?? false
);
try {
await executeAndReplyWithCandidSerde(
'update',
args,
originalMethod.bind(globalThis._azleCanisterClassInstance),
paramIdlTypes,
returnIdlType,
options?.manual ?? false
);
} catch (error: any) {
trap(error.toString());
}
};
};
}
Loading

0 comments on commit 66745fd

Please sign in to comment.