Bläddra i källkod

exception handling

Jachym Cepicky 14 år sedan
förälder
incheckning
78035e58ed
3 ändrade filer med 50 tillägg och 15 borttagningar
  1. 19 10
      OWS.py
  2. 24 0
      OWSExceptions.py
  3. 7 5
      owsproxy.cgi

+ 19 - 10
OWS.py

@@ -13,7 +13,7 @@ import mapscript
 from string import Template
 from osgeo import osr
 from osgeo import ogr
-
+import OWSExceptions
 
 class OWS:
 
@@ -133,7 +133,12 @@ class OWS:
         if request.getValueByName("fes"):
             self.setFilter(mapobj,request)
 
-        mapobj.OWSDispatch(request)
+        res = mapobj.OWSDispatch(request)
+        if mapscript.MS_DONE == res:
+            raise OWSExceptions.NoValidRequest("No valid OWS Request")
+        elif mapscript.MS_FAILURE == res:
+            pass
+            #raise OWSExceptions.RequestFailed("Request failed")
 
     def getMapObj(self,mapfilename=None):
 
@@ -273,13 +278,17 @@ def getService():
     params = urlparse.parse_qs(qstring)
 
     
-    owsUrl = urllib.unquote(params["owsUrl"][0])
-
-    if params["owsService"][0].lower() == "wfs":
-        from wfs import WFS
-        return WFS(owsUrl,qstring)
-    elif params["owsService"][0].lower() == "wcs":
-        from wcs import WCS
-        return WCS(owsUrl,qstring)
+    if "owsUrl" in params.keys() and\
+        "owsService" in params.keys():
+        owsUrl = urllib.unquote(params["owsUrl"][0])
+
+        if params["owsService"][0].lower() == "wfs":
+            from wfs import WFS
+            return WFS(owsUrl,qstring)
+        elif params["owsService"][0].lower() == "wcs":
+            from wcs import WCS
+            return WCS(owsUrl,qstring)
+    else:
+        raise OWSExceptions.MissingParameterValue("""owsUrl or owsService""")
     
 

+ 24 - 0
OWSExceptions.py

@@ -0,0 +1,24 @@
+class OWSException(Exception):
+    pass
+
+class WMSException(OWSException):
+    def toXml(self):
+        print  """Content-type: text/xml\n"""
+
+        print """<?xml version="1.0" ?>
+        <ServiceExceptionReport version="1.3.0" xmlns="http://www.opengis.net/ogc"
+            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+            xsi:schemaLocation="http://www.opengis.net/ogc http://schemas.opengis.net/wms/1.3.0/exceptions_1_3_0.xsd">
+        <ServiceException code="%s" locator="%s"><![CDATA[
+        %s
+        ]]></ServiceException>
+        </ServiceExceptionReport>""" % \
+                (self.__class__.__name__,self.message,self.args)
+
+class MissingParameterValue(WMSException):
+    pass
+
+class RequestFailed(WMSException):
+    pass
+class NoValidRequest(WMSException):
+    pass

+ 7 - 5
owsproxy.cgi

@@ -1,11 +1,13 @@
 #!/usr/bin/python
 
-import sys
-sys.path.append("/home/jachym/usr/src/hsrs/hslayers/trunk/source/scripts/hslframework/owsviewer/")
-
 import OWS
 import logging
+import OWSExceptions
+
 logging.basicConfig(level=logging.DEBUG)
 
-service = OWS.getService()
-service.performRequest()
+try:
+    service = OWS.getService()
+    service.performRequest()
+except OWSExceptions.OWSException,e:
+    e.toXml()