Skip to content

Commit

Permalink
add better error handling to heartbeat, inspect_message, and pre_upgr…
Browse files Browse the repository at this point in the history
…ade for the class-based syntax
  • Loading branch information
lastmjs committed Jul 11, 2024
1 parent 3e555d8 commit 5415d8f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
12 changes: 10 additions & 2 deletions src/lib/stable/heartbeat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { handleUncaughtError } from './error';

export function heartbeat<T>(
_target: object,
target: object,
propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<T>
): TypedPropertyDescriptor<T> | void {
Expand All @@ -11,7 +13,13 @@ export function heartbeat<T>(
};

globalThis._azleCanisterMethods.callbacks[index.toString()] =
descriptor.value as any;
async (): Promise<void> => {
try {
await (descriptor.value as any).bind(target)();
} catch (error) {
handleUncaughtError(error);
}
};

return descriptor;
}
Expand Down
13 changes: 10 additions & 3 deletions src/lib/stable/inspect_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
// TODO but it will break all of your other methods
// TODO so do we just leave params out?

import { handleUncaughtError } from './error';

export function inspectMessage<T>(
_target: object,
target: object,
propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<T>
): TypedPropertyDescriptor<T> | void {
Expand All @@ -16,8 +18,13 @@ export function inspectMessage<T>(
index
};

globalThis._azleCanisterMethods.callbacks[index.toString()] =
descriptor.value as any;
globalThis._azleCanisterMethods.callbacks[index.toString()] = (): void => {
try {
(descriptor.value as any).bind(target)();
} catch (error) {
handleUncaughtError(error);
}
};

return descriptor;
}
13 changes: 10 additions & 3 deletions src/lib/stable/pre_upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { handleUncaughtError } from './error';

export function preUpgrade<T>(
_target: object,
target: object,
propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<T>
): TypedPropertyDescriptor<T> | void {
Expand All @@ -10,8 +12,13 @@ export function preUpgrade<T>(
index
};

globalThis._azleCanisterMethods.callbacks[index.toString()] =
descriptor.value as any;
globalThis._azleCanisterMethods.callbacks[index.toString()] = (): void => {
try {
(descriptor.value as any).bind(target)();
} catch (error) {
handleUncaughtError(error);
}
};

return descriptor;
}

0 comments on commit 5415d8f

Please sign in to comment.