|
214 | 214 | #undef SetPort |
215 | 215 | %} |
216 | 216 |
|
217 | | -// ============== |
218 | | -// ERROR HANDLING |
219 | | -// ============== |
220 | | -// Converts C++ exceptions to Go errors using panic/recovery mechanism. |
221 | | -// All functions now return an error in addition to their return type instead of panicking on exceptions. |
222 | | - |
223 | | -// Ensure necessary imports for error handling code |
224 | | -%insert(go_imports) %{ |
225 | | -import "errors" |
226 | | -import "fmt" |
227 | | -%} |
228 | | - |
229 | | -// Handle exceptions by triggering recoverable panic containing the exception message |
| 217 | +%include "exception.i" |
230 | 218 | %exception { |
231 | 219 | try { |
232 | | - $action |
233 | | - } catch (const std::exception &e) { |
| 220 | + $action; |
| 221 | + } catch (std::exception &e) { |
234 | 222 | _swig_gopanic(e.what()); |
235 | | - } catch (...) { |
236 | | - _swig_gopanic("unknown exception occurred"); |
237 | 223 | } |
238 | 224 | } |
239 | 225 |
|
240 | | -// Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps |
241 | | -%define ERROR_HANDLING_TYPEMAP(TYPE) |
242 | | -%typemap(gotype, out) TYPE "$gotype, error" |
243 | | -%typemap(cgoout, out) TYPE %{ |
244 | | - var swig_r $gotypes |
245 | | - var swig_err error |
246 | | - |
247 | | - func() { |
248 | | - defer func() { |
249 | | - if r := recover(); r != nil { |
250 | | - swig_err = errors.New(fmt.Sprintf("%v", r)) |
251 | | - } |
252 | | - }() |
253 | | - swig_r = $cgocall |
254 | | - }() |
255 | | - |
256 | | - return swig_r, swig_err |
257 | | -%} |
258 | | -%enddef |
259 | | - |
260 | | -// Apply gotype and cgoout typemaps to functions that return: |
261 | | - |
262 | | -/*// Primitives |
263 | | -ERROR_HANDLING_TYPEMAP(bool) |
264 | | -ERROR_HANDLING_TYPEMAP(char) |
265 | | -ERROR_HANDLING_TYPEMAP(double) |
266 | | -ERROR_HANDLING_TYPEMAP(int) |
267 | | -ERROR_HANDLING_TYPEMAP(ptrdiff_t) |
268 | | -ERROR_HANDLING_TYPEMAP(size_t)*/ |
269 | | - |
270 | | -/*// Generate gotype and cgoout typemaps for void separately |
271 | | -%typemap(gotype, out) void "error" |
272 | | -%typemap(cgoout, out) void %{ |
273 | | - var swig_err error |
274 | | -
|
275 | | - func() { |
276 | | - defer func() { |
277 | | - if r := recover(); r != nil { |
278 | | - swig_err = errors.New(fmt.Sprintf("%v", r)) |
279 | | - } |
280 | | - }() |
281 | | - $cgocall |
282 | | - }() |
283 | | -
|
284 | | - return swig_err |
285 | | -%}*/ |
286 | | - |
287 | | -// Handle edge case: SDF::Obj returns nil when internal pointer is invalid |
288 | 226 | %typemap(goout) pdftron::SDF::Obj |
289 | 227 | %{ |
290 | 228 | // Without the brackets, swig attempts to turn $1 into a c++ dereference.. seems like a bug |
291 | 229 | if ($1).GetMp_obj().Swigcptr() != 0 { |
292 | | - return $1, swig_err |
| 230 | + $result = $1 |
| 231 | + return $result |
293 | 232 | } |
294 | 233 |
|
295 | | - return nil, swig_err |
| 234 | + $result = nil |
296 | 235 | %} |
297 | 236 |
|
298 | | -// ================== |
299 | | -// END ERROR HANDLING |
300 | | -// ================== |
301 | | - |
302 | 237 | /** |
303 | 238 | * Provides mapping for C++ vectors. |
304 | 239 | * For example, vector<double> will be called as VectorDouble in GoLang. |
|
0 commit comments