8000 feat!: better python errors by eliassjogreen · Pull Request #42 · denosaurs/deno_python · GitHub
[go: up one dir, main page]

Skip to content

feat!: better python errors #42

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. 8000

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2023
Merged
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
33 changes: 21 additions & 12 deletions src/python.ts
8000
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,9 @@ export class PyObject {

const positionalCount = positional.length - namedCount;
if (positionalCount < 0) {
throw new PythonError("Not enough arguments");
throw new TypeError(
`${this.toString()} requires at least ${namedCount} arguments, but only ${positional.length} were passed`,
);
}

const args = py.PyTuple_New(positionalCount);
Expand Down Expand Up @@ -784,8 +786,21 @@ export class PyObject {
export class PythonError extends Error {
name = "PythonError";

constructor(public message: string) {
constructor(
public type: PyObject,
public value: PyObject,
public traceback: PyObject,
) {
let message = (value ?? type).toString() ?? "Unknown error";
let stack: string | undefined;
if (!traceback.isNone) {
const tb = python.import("traceback");
stack = (tb.format_tb(traceback).valueOf() as string[]).join("");
message += stack;
}

super(message);
this.stack = stack;
}
}

Expand All @@ -809,13 +824,7 @@ export function maybeThrowError() {
value = new PyObject(Deno.UnsafePointer.create(pointers[1])),
traceback = new PyObject(Deno.UnsafePointer.create(pointers[2]));

let errorMessage = (value ?? type).toString() ?? "Unknown error";
if (!traceback.isNone) {
const tb = python.import("traceback");
errorMessage += `\nTraceback:\n${tb.format_tb(traceback)}`;
}

throw new PythonError(errorMessage);
throw new PythonError(type, value, traceback);
}

/**
Expand Down Expand Up @@ -881,7 +890,7 @@ export class Python {
*/
run(code: string) {
if (py.PyRun_SimpleString(cstr(code)) !== 0) {
throw new PythonError("Failed to run code");
throw new EvalError("Failed to run python code");
}
}

Expand All @@ -898,7 +907,7 @@ export class Python {
.handle,
);
if (module === null) {
throw new PythonError("Failed to run module");
throw new EvalError("Failed to run python module");
}
return new PyObject(module)?.proxy;
}
Expand All @@ -910,7 +919,7 @@ export class Python {
const mod = py.PyImport_ImportModule(cstr(name));
if (mod === null) {
maybeThrowError();
throw new PythonError(`Failed to import module ${name}`);
throw new TypeError(`Failed to import module ${name}`);
}
return new PyObject(mod);
}
Expand Down
0