Skip to content
Permalink
Browse files

Display Exception message when raised in python plugin ogcapi handler

(cherry picked from commit 5568c6e)
  • Loading branch information
troopa81 authored and nyalldawson committed Dec 21, 2020
1 parent c522a04 commit 5577ae5e12daedc963f0749ac657ee09681135a8
Showing with 90 additions and 14 deletions.
  1. +24 −14 python/server/server.sip.in
  2. +66 −0 tests/src/python/test_qgsserver_api.py
@@ -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
@@ -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"""

@@ -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 5577ae5

Please sign in to comment.
You can’t perform that action at this time.