Skip to content

Commit

Permalink
Display Exception message when raised in python plugin ogcapi handler
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed Dec 3, 2020
1 parent c71a2ca commit dca78ee
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 14 deletions.
38 changes: 24 additions & 14 deletions python/server/server.sip.in
Expand Up @@ -16,28 +16,38 @@ ${DEFAULTDOCSTRINGSIGNATURE}


%VirtualErrorHandler serverapi_badrequest_exception_handler
PyObject *exception, *value, *traceback;
PyErr_Fetch(&exception, &value, &traceback);
PyObject *type, *exception, *traceback, *pyStrException;

PyErr_Fetch(&type, &exception, &traceback);
pyStrException = PyObject_Str(exception);
Py_DECREF(pyStrException);

SIP_RELEASE_GIL( sipGILState );
QString strVal = "API bad request error";
if ( value && PyUnicode_Check(value) )

QString strException = "API bad request error";
if ( pyStrException && PyUnicode_Check(pyStrException) )
{
Py_ssize_t size;
strVal = QString::fromUtf8( PyUnicode_AsUTF8AndSize(value, &size) );
strException = QString::fromUtf8( PyUnicode_AsUTF8(pyStrException) );
}
throw QgsServerApiBadRequestException( strVal );

throw QgsServerApiBadRequestException( strException );
%End


%VirtualErrorHandler server_exception_handler
PyObject *exception, *value, *traceback;
PyErr_Fetch(&exception, &value, &traceback);
PyObject *type, *exception, *traceback, *pyStrException;

PyErr_Fetch(&type, &exception, &traceback);
pyStrException = PyObject_Str(exception);
Py_DECREF(pyStrException);

SIP_RELEASE_GIL( sipGILState );
QString strVal = "Server internal error";
if ( value && PyUnicode_Check(value) )

QString strException = "Server internal error";
if ( pyStrException && PyUnicode_Check(pyStrException) )
{
Py_ssize_t size;
strVal = QString::fromUtf8( PyUnicode_AsUTF8AndSize(value, &size) );
strException = QString::fromUtf8( PyUnicode_AsUTF8(pyStrException) );
}
throw QgsServerException( strVal );

throw QgsServerException( strException );
%End
66 changes: 66 additions & 0 deletions tests/src/python/test_qgsserver_api.py
Expand Up @@ -1721,6 +1721,42 @@ def templatePath(self, context):
return self.templatePathOverride


class HandlerException(QgsServerOgcApiHandler):

def __init__(self):
super().__init__()
self.__exception = None

def setException(self, exception):
self.__exception = exception

def path(self):
return QtCore.QRegularExpression("/handlerexception")

def operationId(self):
return "handlerException"

def summary(self):
return "Trigger an exception"

def description(self):
return "Trigger an exception"

def linkTitle(self):
return "Trigger an exception Title"

def linkType(self):
return QgsServerOgcApi.data

def handleRequest(self, context):
"""Triggers an exception"""
raise self.__exception

def parameters(self, context):
return [
QgsServerQueryStringParameter('value1', True, QgsServerQueryStringParameter.Type.Double, 'a double value')]


class QgsServerOgcAPITest(QgsServerAPITestBase):
""" QGIS OGC API server tests"""

Expand Down Expand Up @@ -1900,6 +1936,36 @@ def testOgcApiHandlerContentType(self):
'http://localhost:8000/project/7ecb/wfs3/collections/zg.grundnutzung.html')
self.assertEqual(h3.contentTypeFromRequest(req), QgsServerOgcApi.HTML)

def testOgcApiHandlerException(self):
"""Test OGC API Handler exception"""

project = QgsProject()
project.read(unitTestDataPath('qgis_server') + '/test_project_api.qgs')
request = QgsBufferServerRequest('')
response = QgsBufferServerResponse()

ctx = QgsServerApiContext(
'/services/apiexception', request, response, project, self.server.serverInterface())
h = HandlerException()

api = QgsServerOgcApi(self.server.serverInterface(),
'/apiexception', 'apiexception', 'an api with exception', '1.2')
api.registerHandler(h)

h.setException(Exception("UTF-8 Exception 1 $ù~à^£"))
ctx.request().setUrl(QtCore.QUrl('http://www.qgis.org/handlerexception'))
with self.assertRaises(QgsServerApiBadRequestException) as ex:
api.executeRequest(ctx)
self.assertEqual(
str(ex.exception), "UTF-8 Exception 1 $ù~à^£")

h.setException(QgsServerApiBadRequestException("UTF-8 Exception 2 $ù~à^£"))
ctx.request().setUrl(QtCore.QUrl('http://www.qgis.org/handlerexception'))
with self.assertRaises(QgsServerApiBadRequestException) as ex:
api.executeRequest(ctx)
self.assertEqual(
str(ex.exception), "UTF-8 Exception 2 $ù~à^£")


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

0 comments on commit dca78ee

Please sign in to comment.