Skip to content

Commit

Permalink
add panic hooks to init and post upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Jul 11, 2024
1 parent 5415d8f commit 5bc965e
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 62 deletions.
33 changes: 8 additions & 25 deletions examples/stable_memory/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/stable_memory/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ export function getTests(stableMemoryCanister: ActorSubclass<_SERVICE>): Test {
const expectedErrorMessage = new RegExp(
`Call was rejected:\\s*Request ID: [a-f0-9]{64}\\s*Reject code: 5\\s*Reject text: ${rejectionMessage.source}`
);
('OutOfMemoryCool');
await expect(stableMemoryCanister.stableGrow(1)).rejects.toThrow(
expectedErrorMessage
);
Expand Down
9 changes: 0 additions & 9 deletions src/compiler/compile_rust_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ export async function compileRustCode(
compilerInfo: CompilerInfo,
canisterConfig: CanisterConfig
): Promise<void> {
if (nativeCompilation === true) {
compileRustCodeNatively(
`.azle/${canisterName}/${canisterName}.wasm`,
canisterName,
stdio
);
return;
}

if (process.env.AZLE_GENERATE_STATIC_CANISTER_TEMPLATE === 'true') {
compileRustCodeNatively(
STATIC_CANISTER_TEMPLATE_PATH,
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async function azle(): Promise<void> {
return;
}

// TODO can we just get names once?
const {
stdioType,
wasmedgeQuickJsPath,
Expand Down Expand Up @@ -72,6 +73,7 @@ async function azle(): Promise<void> {
)
);

// TODO this might should only happen if we compile
await prepareRustStagingArea(
canisterConfig,
canisterPath,
Expand Down Expand Up @@ -105,6 +107,7 @@ async function azle(): Promise<void> {
canisterMethods.post_upgrade?.index ?? -1
);

// TODO probably not required anymore
// This is for the dfx.json candid property
await writeFile(candidPath, candid);

Expand Down
44 changes: 44 additions & 0 deletions src/compiler/rust/canister_methods/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ pub fn canister_methods(_: TokenStream) -> TokenStream {
#[no_mangle]
#[allow(unused)]
pub fn init(function_index: i32, pass_arg_data: i32) {
std::panic::set_hook(Box::new(|panic_info| {
let msg = if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
*s
} else if let Some(s) = panic_info.payload().downcast_ref::<String>() {
s.as_str()
} else {
"Unknown panic message"
};

let location = if let Some(location) = panic_info.location() {
format!(" at {}:{}", location.file(), location.line())
} else {
" (unknown location)".to_string()
};

let message = &format!("Panic occurred: {}{}", msg, location);

ic_cdk::println!("{}", message);

ic_cdk::trap(message);
}));

let wasm_data = get_wasm_data();

let env_vars: Vec<(&str, &str)> = wasm_data.env_vars.iter().map(|(key, value)| (key.as_str(), value.as_str())).collect();
Expand Down Expand Up @@ -94,6 +116,28 @@ pub fn canister_methods(_: TokenStream) -> TokenStream {
#[no_mangle]
#[allow(unused)]
pub fn post_upgrade(function_index: i32, pass_arg_data: i32) {
std::panic::set_hook(Box::new(|panic_info| {
let msg = if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
*s
} else if let Some(s) = panic_info.payload().downcast_ref::<String>() {
s.as_str()
} else {
"Unknown panic message"
};

let location = if let Some(location) = panic_info.location() {
format!(" at {}:{}", location.file(), location.line())
} else {
" (unknown location)".to_string()
};

let message = &format!("Panic occurred: {}{}", msg, location);

ic_cdk::println!("{}", message);

ic_cdk::trap(message);
}));

let wasm_data = get_wasm_data();

let env_vars: Vec<(&str, &str)> = wasm_data.env_vars.iter().map(|(key, value)| (key.as_str(), value.as_str())).collect();
Expand Down
Binary file modified static_canister_template.wasm
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5bc965e

Please sign in to comment.