Skip to content

Commit 63c34e3

Browse files
committedMay 26, 2020
[WFS provider] Parse WMS service exceptions (fixes #29866)
1 parent 24ec1d9 commit 63c34e3

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed
 

‎src/providers/wfs/qgswfscapabilities.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,28 @@ void QgsWfsCapabilities::capabilitiesReplyFinished()
176176
// handle exceptions
177177
if ( doc.tagName() == QLatin1String( "ExceptionReport" ) )
178178
{
179-
QDomNode ex = doc.firstChild();
179+
QDomNode ex = doc.firstChild(); // Get Exception element
180180
QString exc = ex.toElement().attribute( QStringLiteral( "exceptionCode" ), QStringLiteral( "Exception" ) );
181-
QDomElement ext = ex.firstChild().toElement();
181+
QDomElement ext = ex.firstChild().toElement(); // Get ExceptionText element
182182
mErrorCode = QgsWfsRequest::ServerExceptionError;
183183
mErrorMessage = exc + ": " + ext.firstChild().nodeValue();
184184
emit gotCapabilities();
185185
return;
186186
}
187187

188+
// handle WMS exceptions as well (to be nice with users...)
189+
// See https://github.com/qgis/QGIS/issues/29866
190+
if ( doc.tagName() == QLatin1String( "ServiceExceptionReport" ) )
191+
{
192+
QDomNode ex = doc.firstChild(); // Get ServiceExceptionReport element
193+
QString exc = ex.toElement().attribute( QStringLiteral( "code" ), QStringLiteral( "Exception" ) );
194+
QDomElement ext = ex.toElement();
195+
mErrorCode = QgsWfsRequest::ServerExceptionError;
196+
mErrorMessage = exc + ": " + ext.firstChild().nodeValue().trimmed();
197+
emit gotCapabilities();
198+
return;
199+
}
200+
188201
mCaps.clear();
189202

190203
//test wfs version

‎tests/src/python/test_provider_wfs.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4650,6 +4650,58 @@ def testWFS20CaseInsensitiveKVP(self):
46504650
assert QgsGeometry.compare(vl_extent.asPolygon()[0], reference.asPolygon()[0],
46514651
0.00001), 'Expected {}, got {}'.format(reference.asWkt(), vl_extent.asWkt())
46524652

4653+
def testGetCapabilitiesReturnWFSException(self):
4654+
"""Test parsing of WFS exception
4655+
"""
4656+
endpoint = self.__class__.basetestpath + '/fake_qgis_http_endpoint_testGetCapabilitiesReturnWFSException'
4657+
4658+
get_cap_response = """<?xml version="1.0" encoding="UTF-8"?>
4659+
<ExceptionReport xmlns="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4660+
xsi:schemaLocation="http://schemas.opengis.net/ows/1.1.0/owsExceptionReport.xsd" version="1.0.0" language="en">
4661+
<Exception exceptionCode="foo" locator="service">
4662+
<ExceptionText>bar</ExceptionText>
4663+
</Exception>
4664+
</ExceptionReport>
4665+
""".encode('UTF-8')
4666+
4667+
with open(sanitize(endpoint,
4668+
'?SERVICE=WFS&REQUEST=GetCapabilities&VERSION=1.0.0'),
4669+
'wb') as f:
4670+
f.write(get_cap_response)
4671+
4672+
with MessageLogger('WFS') as logger:
4673+
vl = QgsVectorLayer("url='http://" + endpoint + "' typename='my:typename' version='1.0.0'", 'test', 'WFS')
4674+
self.assertFalse(vl.isValid())
4675+
4676+
self.assertEqual(len(logger.messages()), 1, logger.messages())
4677+
self.assertTrue("foo: bar" in logger.messages()[0].decode('UTF-8'), logger.messages())
4678+
4679+
def testGetCapabilitiesReturnWMSException(self):
4680+
"""Test fix for https://github.com/qgis/QGIS/issues/29866
4681+
"""
4682+
endpoint = self.__class__.basetestpath + '/fake_qgis_http_endpoint_testGetCapabilitiesReturnWMSxception'
4683+
4684+
get_cap_response = """<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
4685+
<!DOCTYPE ServiceExceptionReport SYSTEM "http://schemas.opengis.net/wms/1.1.1/exception_1_1_1.dtd">
4686+
<ServiceExceptionReport version="1.1.1">
4687+
<ServiceException code="InvalidFormat">
4688+
Can't recognize service requested.
4689+
</ServiceException>
4690+
</ServiceExceptionReport>
4691+
""".encode('UTF-8')
4692+
4693+
with open(sanitize(endpoint,
4694+
'?SERVICE=WFS&REQUEST=GetCapabilities&VERSION=1.0.0'),
4695+
'wb') as f:
4696+
f.write(get_cap_response)
4697+
4698+
with MessageLogger('WFS') as logger:
4699+
vl = QgsVectorLayer("url='http://" + endpoint + "' typename='my:typename' version='1.0.0'", 'test', 'WFS')
4700+
self.assertFalse(vl.isValid())
4701+
4702+
self.assertEqual(len(logger.messages()), 1, logger.messages())
4703+
self.assertTrue("InvalidFormat: Can't recognize service requested." in logger.messages()[0].decode('UTF-8'), logger.messages())
4704+
46534705

46544706
if __name__ == '__main__':
46554707
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.