Skip to content

feat: set python exception if an error happens in a js callback #83

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
39 changes: 32 additions & 7 deletions src/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,38 @@ export class Callback {
args: Deno.PointerValue,
kwargs: Deno.PointerValue,
) => {
return PyObject.from(callback(
kwargs === null ? {} : Object.fromEntries(
new PyObject(kwargs).asDict()
.entries(),
),
...(args === null ? [] : new PyObject(args).valueOf()),
)).handle;
let result: PythonConvertible;
// Prepare arguments for the JS callback
try {
// Prepare arguments for the JS callback
const jsKwargs = kwargs === null
? {}
: Object.fromEntries(new PyObject(kwargs).asDict().entries());
const jsArgs = args === null ? [] : new PyObject(args).valueOf();

// Call the actual JS function
result = callback(jsKwargs, ...jsArgs);

// Convert the JS return value back to a Python object
return PyObject.from(result).handle;
} catch (e) {
// An error occurred in the JS callback.
// We need to set a Python exception and return NULL.

// Prepare the error message for Python
const errorMessage = e instanceof Error
? `${e.name}: ${e.message}` // Include JS error type and message
: String(e); // Fallback for non-Error throws
const cErrorMessage = cstr(`JS Callback Error: ${errorMessage}`);

const errorTypeHandle =
python.builtins.RuntimeError[ProxiedPyObject].handle;

// Set the Python exception (type and message)
py.PyErr_SetString(errorTypeHandle, cErrorMessage);

return null;
}
},
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export const SYMBOLS = {
result: "void",
},

PyErr_SetString: {
parameters: ["pointer", "buffer"], // type, message
result: "void",
},

PyDict_New: {
parameters: [],
result: "pointer",
Expand Down
24 changes: 24 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,27 @@ Deno.test("callbacks have signature", async (t) => {
fn.destroy();
});
});

Deno.test("js exception inside python callback returns python exception", () => {
const pyCallback = python.callback(() => {
throw new Error("This is an intentional error from JS!");
});

const pyModule = python.runModule(
`
def call_the_callback(cb):
result = cb()
return result
`,
"test_module",
);

try {
pyModule.call_the_callback(pyCallback);
} catch (e) {
// deno-lint-ignore no-explicit-any
assertEquals((e as any).name, "PythonError");
} finally {
pyCallback.destroy();
}
});