Changeset 521

Show
Ignore:
Timestamp:
10/18/06 16:34:28
Author:
seang
Message:

check for and handle service exceptions in responses under 32kb, larger ones are waived through

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • OWSLib/trunk/owslib/wfs.py

    r520 r521  
    2222 
    2323import cgi 
    24 from urllib import urlencode, urlopen 
     24from cStringIO import StringIO 
     25import sys 
     26from urllib import urlencode 
     27from urllib2 import urlopen 
    2528 
    2629from etree import etree 
     
    111114        else: 
    112115            u = urlopen(base_url + data) 
    113  
     116         
    114117        # check for service exceptions, rewrap, and return 
    115         return u 
    116  
    117          
     118        # We're going to assume that anything with a content-length > 32k 
     119        # is data. We'll check anything smaller. 
     120        if int(u.info()['Content-Length']) < 32000: 
     121            data = u.read() 
     122            tree = etree.fromstring(data) 
     123            if tree.tag == "{%s}ServiceExceptionReport" % OGC_NAMESPACE: 
     124                se = tree.find(nspath('ServiceException', OGC_NAMESPACE)) 
     125                raise ServiceException, str(se.text).strip() 
     126            else: 
     127                return StringIO(data) 
     128        else: 
     129            return u 
     130 
     131 
    118132class ServiceMetadata(object): 
    119133    """Abstraction for WFS metadata. 
  • OWSLib/trunk/tests/MapServerWFSFeature.txt

    r520 r521  
    77 
    88    >>> wfs = WebFeatureService('http://www.bsc-eoc.org/cgi-bin/bsc_ows.asp?', version='1.0.0') 
    9     >>> xml = wfs.getfeature(typename=['IBA'], maxfeatures=5).read(
    10     >>> xml.find('<wfs:FeatureCollection') > 0 
     9    >>> response = wfs.getfeature(typename=['IBA'], maxfeatures=5
     10    >>> response.read().find('<wfs:FeatureCollection') > 0 
    1111    True 
    1212 
     13Handle service exception 
     14 
     15    >>> response = wfs.getfeature(typename=['totally bogus'], maxfeatures=5) 
     16    Traceback (most recent call last): 
     17    ... 
     18    ServiceException: msWFSGetFeature(): WFS server error. TYPENAME 'totally bogus' doesn't exist in this server.  Please check the capabilities and reformulate your request. 
     19