php: SWIG_exception now maps code to exception class
This now determines the class of the exception object where a
suitable pre-defined PHP exception class exists - for example,
SWIG_TypeError -> PHP exception class TypeError.
Exception codes which don't naturally map to a pre-defined PHP
exception class are thrown as PHP class Exception (like all
PHP exceptions raised by SWIG_exception were before this change.)
diff --git a/Lib/exception.i b/Lib/exception.i
index 9bf3a19..020ee11 100644
--- a/Lib/exception.i
+++ b/Lib/exception.i
@@ -14,7 +14,18 @@
#ifdef SWIGPHP
%{
-#define SWIG_exception(code, msg) do { zend_throw_exception(NULL, (char*)msg, code); goto thrown; } while (0)
+#if PHP_MAJOR >= 8
+# define SWIG_HANDLE_VALUE_ERROR_FOR_PHP8 code == SWIG_ValueError ? zend_ce_value_error :
+#else
+# define SWIG_HANDLE_VALUE_ERROR_FOR_PHP8
+#endif
+#define SWIG_exception(code, msg) do { zend_throw_exception( \
+ code == SWIG_TypeError ? zend_ce_type_error : \
+ SWIG_HANDLE_VALUE_ERROR_FOR_PHP8 \
+ code == SWIG_DivisionByZero ? zend_ce_division_by_zero_error : \
+ code == SWIG_SyntaxError ? zend_ce_parse_error : \
+ code == SWIG_OverflowError ? zend_ce_arithmetic_error : \
+ NULL, msg, code); goto thrown; } while (0)
%}
#endif