@@ -734,7 +734,9 @@ export class PyObject {
734734
735735 const positionalCount = positional . length - namedCount ;
736736 if ( positionalCount < 0 ) {
737- throw new PythonError ( "Not enough arguments" ) ;
737+ throw new TypeError (
738+ `${ this . toString ( ) } requires at least ${ namedCount } arguments, but only ${ positional . length } were passed` ,
739+ ) ;
738740 }
739741
740742 const args = py . PyTuple_New ( positionalCount ) ;
@@ -787,8 +789,21 @@ export class PyObject {
787789export class PythonError extends Error {
788790 name = "PythonError" ;
789791
790- constructor ( public message : string ) {
792+ constructor (
793+ public type : PyObject ,
794+ public value : PyObject ,
795+ public traceback : PyObject ,
796+ ) {
797+ let message = ( value ?? type ) . toString ( ) ?? "Unknown error" ;
798+ let stack : string | undefined ;
799+ if ( ! traceback . isNone ) {
800+ const tb = python . import ( "traceback" ) ;
801+ stack = ( tb . format_tb ( traceback ) . valueOf ( ) as string [ ] ) . join ( "" ) ;
802+ message += stack ;
803+ }
804+
791805 super ( message ) ;
806+ this . stack = stack ;
792807 }
793808}
794809
@@ -812,13 +827,7 @@ export function maybeThrowError() {
812827 value = new PyObject ( Deno . UnsafePointer . create ( pointers [ 1 ] ) ) ,
813828 traceback = new PyObject ( Deno . UnsafePointer . create ( pointers [ 2 ] ) ) ;
814829
815- let errorMessage = ( value ?? type ) . toString ( ) ?? "Unknown error" ;
816- if ( ! traceback . isNone ) {
817- const tb = python . import ( "traceback" ) ;
818- errorMessage += `\nTraceback:\n${ tb . format_tb ( traceback ) } ` ;
819- }
820-
821- throw new PythonError ( errorMessage ) ;
830+ throw new PythonError ( type , value , traceback ) ;
822831}
823832
824833/**
@@ -884,7 +893,7 @@ export class Python {
884893 */
885894 run ( code : string ) {
886895 if ( py . PyRun_SimpleString ( cstr ( code ) ) !== 0 ) {
887- throw new PythonError ( "Failed to run code" ) ;
896+ throw new EvalError ( "Failed to run python code" ) ;
888897 }
889898 }
890899
@@ -901,7 +910,7 @@ export class Python {
901910 . handle ,
902911 ) ;
903912 if ( module === null ) {
904- throw new PythonError ( "Failed to run module" ) ;
913+ throw new EvalError ( "Failed to run python module" ) ;
905914 }
906915 return new PyObject ( module ) ?. proxy ;
907916 }
@@ -913,7 +922,7 @@ export class Python {
913922 const mod = py . PyImport_ImportModule ( cstr ( name ) ) ;
914923 if ( mod === null ) {
915924 maybeThrowError ( ) ;
916- throw new PythonError ( `Failed to import module ${ name } ` ) ;
925+ throw new TypeError ( `Failed to import module ${ name } ` ) ;
917926 }
918927 return new PyObject ( mod ) ;
919928 }
0 commit comments