[mapserver-commits] r11185 - trunk/mapserver/mapscript/ruby

svn at osgeo.org svn at osgeo.org
Thu Mar 17 09:27:57 EDT 2011


Author: jimk
Date: 2011-03-17 06:27:56 -0700 (Thu, 17 Mar 2011)
New Revision: 11185

Modified:
   trunk/mapserver/mapscript/ruby/rbmodule.i
Log:
Improve Ruby exception handling. (#3749)


Modified: trunk/mapserver/mapscript/ruby/rbmodule.i
===================================================================
--- trunk/mapserver/mapscript/ruby/rbmodule.i	2011-03-16 20:43:14 UTC (rev 11184)
+++ trunk/mapserver/mapscript/ruby/rbmodule.i	2011-03-17 13:27:56 UTC (rev 11185)
@@ -23,6 +23,34 @@
     gdFree($1.data);
 }
 
+/**************************************************************************
+ * MapServer Errors and Ruby Exceptions
+ **************************************************************************
+ *
+ * Translation of errors reported via the ms_error structure into
+ * Ruby exceptions. Generally copied from Python MapScript by Jim Klassen.
+ * Follows original code by Chris Chamberlin <cdc at aracnet.com> as updated
+ * updated by Sean Gillies <sgillies at frii.com>. 
+ * Uses rb_raise, %exception and $action for SWIG 1.3, do the most
+ * obvious mapping of MapServer errors to Ruby exceptions and map all 
+ * others to a new 'MapServerError' exception which can be caught like 
+ * this:
+ * 
+ *   require 'mapscript'
+ *   empty_map = Mapscript.MapObj.new('')
+ *   begin:
+ *       img = empty_map.draw()
+ *   rescue Mapscript::MapServerError => msg
+ *       print "Caught MapServerError:", msg.to_s
+ *   end
+ *
+ *************************************************************************/
+
+%{
+    VALUE MSExc_MapServerError;
+    VALUE MSExc_MapServerChildError;
+%}
+
 /* Module initialization: call msSetup() and register msCleanup() */
 %init %{
 
@@ -32,5 +60,76 @@
         msSetError(MS_MISCERR, "Failed to set up threads and font cache",
                    "msSetup()");
     }
+
+    /* Is there a place to hook to call msCleanup()? */
+    VALUE exceptionClass = rb_eval_string("Exception");
+    VALUE mapscriptModule = rb_eval_string("Mapscript");
+    MSExc_MapServerError = rb_define_class_under(mapscriptModule, "MapserverError", exceptionClass);
+    MSExc_MapServerChildError = rb_define_class_under(mapscriptModule, "MapserverChildErrorError", exceptionClass);
 %}
 
+%{
+static void _raise_ms_exception( void );
+
+static void _raise_ms_exception() {
+    int errcode;
+    errorObj *ms_error;
+    char *errmsg;
+    ms_error = msGetErrorObj();
+    errcode = ms_error->code;
+    errmsg = msGetErrorString("\n");
+    
+    switch (errcode) {
+        case MS_IOERR:
+            rb_raise(rb_eIOError, errmsg);
+            break;
+        case MS_MEMERR:
+            rb_raise(rb_eNoMemError, errmsg);
+            break;
+        case MS_TYPEERR:
+            rb_raise(rb_eTypeError, errmsg);
+            break;
+        case MS_EOFERR:
+            rb_raise(rb_eEOFError, errmsg);
+            break;
+        case MS_CHILDERR:
+            rb_raise(MSExc_MapServerChildError, errmsg);
+            break;
+        default:
+            rb_raise(MSExc_MapServerError, errmsg);
+            break;
+    }
+
+    free(errmsg);
+}
+%}
+
+%exception {
+    msResetErrorList();
+    $action 
+    {
+        errorObj *ms_error = msGetErrorObj();
+
+        switch(ms_error->code) {
+            case MS_NOERR:
+                break;
+            case MS_NOTFOUND:
+                msResetErrorList();
+                break;
+            case -1:
+                break;
+            case MS_IOERR:
+                if (strncmp(ms_error->routine, "msSearchDiskTree()", 20) != 0) {
+                    _raise_ms_exception();
+                    msResetErrorList();
+                    return NULL;
+                }
+            default:
+                _raise_ms_exception();
+                msResetErrorList();
+                return NULL;
+        }
+
+    }
+}
+



More information about the mapserver-commits mailing list