1 /* ----------------------------------------------------------------------------- 2 * error manipulation 3 * ----------------------------------------------------------------------------- */ 4 5 6 /* Define some additional error types */ 7 #define SWIG_ObjectPreviouslyDeletedError -100 8 9 10 /* Define custom exceptions for errors that do not map to existing Ruby 11 exceptions. Note this only works for C++ since a global cannot be 12 initialized by a function in C. For C, fallback to rb_eRuntimeError.*/ 13 14 SWIGINTERN VALUE 15 getNullReferenceError(void) { 16 static int init = 0; 17 static VALUE rb_eNullReferenceError ; 18 if (!init) { 19 init = 1; 20 rb_eNullReferenceError = rb_define_class("NullReferenceError", rb_eRuntimeError); 21 } 22 return rb_eNullReferenceError; 23 } 24 25 SWIGINTERN VALUE 26 getObjectPreviouslyDeletedError(void) { 27 static int init = 0; 28 static VALUE rb_eObjectPreviouslyDeleted ; 29 if (!init) { 30 init = 1; 31 rb_eObjectPreviouslyDeleted = rb_define_class("ObjectPreviouslyDeleted", rb_eRuntimeError); 32 } 33 return rb_eObjectPreviouslyDeleted; 34 } 35 36 37 SWIGINTERN VALUE 38 SWIG_Ruby_ErrorType(int SWIG_code) { 39 VALUE type; 40 switch (SWIG_code) { 41 case SWIG_MemoryError: 42 type = rb_eNoMemError; 43 break; 44 case SWIG_IOError: 45 type = rb_eIOError; 46 break; 47 case SWIG_RuntimeError: 48 type = rb_eRuntimeError; 49 break; 50 case SWIG_IndexError: 51 type = rb_eIndexError; 52 break; 53 case SWIG_TypeError: 54 type = rb_eTypeError; 55 break; 56 case SWIG_DivisionByZero: 57 type = rb_eZeroDivError; 58 break; 59 case SWIG_OverflowError: 60 type = rb_eRangeError; 61 break; 62 case SWIG_SyntaxError: 63 type = rb_eSyntaxError; 64 break; 65 case SWIG_ValueError: 66 type = rb_eArgError; 67 break; 68 case SWIG_SystemError: 69 type = rb_eFatal; 70 break; 71 case SWIG_AttributeError: 72 type = rb_eRuntimeError; 73 break; 74 case SWIG_NullReferenceError: 75 type = getNullReferenceError(); 76 break; 77 case SWIG_ObjectPreviouslyDeletedError: 78 type = getObjectPreviouslyDeletedError(); 79 break; 80 case SWIG_UnknownError: 81 type = rb_eRuntimeError; 82 break; 83 default: 84 type = rb_eRuntimeError; 85 } 86 return type; 87 } 88 89 90 /* This function is called when a user inputs a wrong argument to 91 a method. 92 */ 93 SWIGINTERN 94 const char* Ruby_Format_TypeError( const char* msg, 95 const char* type, 96 const char* name, 97 const int argn, 98 VALUE input ) 99 { 100 char buf[128]; 101 VALUE str; 102 VALUE asStr; 103 if ( msg && *msg ) 104 { 105 str = rb_str_new2(msg); 106 } 107 else 108 { 109 str = rb_str_new(NULL, 0); 110 } 111 112 str = rb_str_cat2( str, "Expected argument " ); 113 sprintf( buf, "%d of type ", argn-1 ); 114 str = rb_str_cat2( str, buf ); 115 str = rb_str_cat2( str, type ); 116 str = rb_str_cat2( str, ", but got " ); 117 str = rb_str_cat2( str, rb_obj_classname(input) ); 118 str = rb_str_cat2( str, " " ); 119 asStr = rb_inspect(input); 120 if ( RSTRING_LEN(asStr) > 30 ) 121 { 122 str = rb_str_cat( str, StringValuePtr(asStr), 30 ); 123 str = rb_str_cat2( str, "..." ); 124 } 125 else 126 { 127 str = rb_str_append( str, asStr ); 128 } 129 130 if ( name ) 131 { 132 str = rb_str_cat2( str, "\n\tin SWIG method '" ); 133 str = rb_str_cat2( str, name ); 134 str = rb_str_cat2( str, "'" ); 135 } 136 137 return StringValuePtr( str ); 138 } 139 140 /* This function is called when an overloaded method fails */ 141 SWIGINTERN 142 void Ruby_Format_OverloadedError( 143 const int argc, 144 const int maxargs, 145 const char* method, 146 const char* prototypes 147 ) 148 { 149 const char* msg = "Wrong # of arguments"; 150 if ( argc <= maxargs ) msg = "Wrong arguments"; 151 rb_raise(rb_eArgError,"%s for overloaded method '%s'.\n" 152 "Possible C/C++ prototypes are:\n%s", 153 msg, method, prototypes); 154 } 155