Skip to content
Draft
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1c584ec
Adding better error handling in Go through panic/recovery method with…
andrewmoreno-apryse Sep 12, 2025
3211337
Moved error handling typemaps to after director classes
andrewmoreno-apryse Sep 15, 2025
45398c6
Added out to typemaps
andrewmoreno-apryse Sep 15, 2025
941a2fe
Excluded director classes from error handling typemaps
andrewmoreno-apryse Sep 15, 2025
2b81070
Moved director exclusions to above error handling typemaps
andrewmoreno-apryse Sep 15, 2025
3394e18
Disabled exception handling on director classes
andrewmoreno-apryse Sep 15, 2025
e91bf8f
Consolidated error handling code
andrewmoreno-apryse Sep 15, 2025
00b4d29
Added additional director excludes for SwigDirector_Callback and Swig…
andrewmoreno-apryse Sep 15, 2025
4f3484b
Excluded SwigDirectors from exception handling
andrewmoreno-apryse Sep 15, 2025
17c3127
Added more exclusions for directors in a vain hope that it suddenly w…
andrewmoreno-apryse Sep 15, 2025
647c20b
Fixed syntax errors after being led astray by ChatGPT
andrewmoreno-apryse Sep 15, 2025
59d82ec
Tried excluding SwigDirector_ classes in a different way
andrewmoreno-apryse Sep 15, 2025
dd0f26d
Idk at this point whats even happening
andrewmoreno-apryse Sep 15, 2025
1f16248
Removed all director exclusions, SWIGTYPE typemaps, and void typemap …
andrewmoreno-apryse Sep 16, 2025
db9e880
Comment out primitive typemaps for testing
andrewmoreno-apryse Sep 16, 2025
d138748
Reset to before error handling changes for testing
andrewmoreno-apryse Sep 16, 2025
11f3d20
Added error handling imports
andrewmoreno-apryse Sep 16, 2025
a4b3478
Added macro for generating gotype/cgoout typemaps for exception handl…
andrewmoreno-apryse Sep 16, 2025
70aa1d8
Applied macro to all functions in our API, and excluded director classes
andrewmoreno-apryse Sep 16, 2025
fb5e552
Added void typemap and added out to macro
andrewmoreno-apryse Sep 16, 2025
8bb0f44
removed newline from import to try and fix bug
andrewmoreno-apryse Sep 16, 2025
fcffb14
Changed sytnax on go_import to try and remove blank import line
andrewmoreno-apryse Sep 16, 2025
3eec07d
Fix typo in go_imports
andrewmoreno-apryse Sep 16, 2025
32aa20c
Change cgoout typemaps to goout, as cgoout does not exist
andrewmoreno-apryse Sep 16, 2025
5e4acb6
Removed pointers and references from error handling typemap macros to…
andrewmoreno-apryse Sep 16, 2025
f7e4a08
Removed non-primitives from error handling typemap macros to see if t…
andrewmoreno-apryse Sep 16, 2025
82f3bc5
Removed director typemap exclusions as they aren't needed anymore
andrewmoreno-apryse Sep 17, 2025
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
62 changes: 62 additions & 0 deletions PDFTronGo/pdftron.i
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@
#undef SetPort
%}

// ==============
// ERROR HANDLING
// ==============
// Converts C++ exceptions to Go errors using panic/recovery mechanism.
// All functions now return an error in addition to their return type instead of panicking on exceptions.

// Ensure necessary imports for error handling code
%go_import("errors", "fmt")

// Handle exceptions by triggering recoverable panic containing the exception message
%include "exception.i"
%exception {
try {
Expand All @@ -223,6 +233,54 @@
}
}

// Macro for generating gotype (adding error to return) and goout (adding panic recovery to return errors) typemaps
%define EXCEPTION_HANDLING_TYPEMAP(TYPE)
%typemap(gotype) TYPE "$gotype, error"
%typemap(goout) TYPE %{
var swig_r $gotypes
var swig_err error

func() {
defer func() {
if r := recover(); r != nil {
swig_err = errors.New(fmt.Sprintf("%v", r))
}
}()
swig_r = $cgocall
}()

return swig_r, swig_err
%}
%enddef

// Apply gotype and goout typemaps to functions that return:

// Primitives
EXCEPTION_HANDLING_TYPEMAP(bool)
EXCEPTION_HANDLING_TYPEMAP(char)
EXCEPTION_HANDLING_TYPEMAP(double)
EXCEPTION_HANDLING_TYPEMAP(int)
EXCEPTION_HANDLING_TYPEMAP(ptrdiff_t)
EXCEPTION_HANDLING_TYPEMAP(size_t)

// Generate gotype and goout typemaps for void separately
%typemap(gotype) void "error"
%typemap(goout) void %{
var swig_err error

func() {
defer func() {
if r := recover(); r != nil {
swig_err = errors.New(fmt.Sprintf("%v", r))
}
}()
$cgocall
}()

return swig_err
%}

// Handle edge case: SDF::Obj returns nil when internal pointer is invalid
%typemap(goout) pdftron::SDF::Obj
%{
// Without the brackets, swig attempts to turn $1 into a c++ dereference.. seems like a bug
Expand All @@ -234,6 +292,10 @@
$result = nil
%}

// ==================
// END ERROR HANDLING
// ==================

/**
* Provides mapping for C++ vectors.
* For example, vector<double> will be called as VectorDouble in GoLang.
Expand Down