qxtwebjsonrpcservice: add a throwRPCError function to allow bailing in a slot

This commit is contained in:
Arvid E. Picciani 2011-09-16 15:18:18 +02:00 committed by Arvid Ephraim Picciani
parent 9aeffd1077
commit 590e000c00
3 changed files with 36 additions and 1 deletions

View File

@ -104,6 +104,7 @@ void QxtWebJsonRPCService::Private::readFinished()
void QxtWebJsonRPCService::Private::handle(QxtWebContent *c)
{
QxtWebRequestEvent *event = content.take(c);
currentRequest = 0;
c->ignoreRemainingContent();
QString data = QString::fromUtf8(c->readAll());
@ -143,6 +144,9 @@ void QxtWebJsonRPCService::Private::handle(QxtWebContent *c)
}
QVariant returnValue;
currentRequest = event;
currentRequestId = rid;
requestCanceled = false;
Method method = methods.value(action.toUtf8() + QByteArray::number(args.count()));
bool ok = false;
@ -364,6 +368,9 @@ void QxtWebJsonRPCService::Private::handle(QxtWebContent *c)
p->postEvent(err);
return;
}
if (requestCanceled) {
return;
}
QVariantMap res;
res.insert("result", returnValue);
@ -390,6 +397,29 @@ QxtWebJsonRPCService::~QxtWebJsonRPCService()
delete d;
}
/*!
* respond to the current request with an error.
*
* The return value of the current slot is NOT used.
* Instead null is returned, adhering to the jsonrpc specificaiton.
*
* Calling this function from somewhere else then a handler slot is undefined behaviour.
*/
void QxtWebJsonRPCService::throwRPCError(QVariant error)
{
d->requestCanceled= true;
QxtWebRequestEvent *event = d->currentRequest;
QVariantMap res;
res.insert("result", QVariant());
res.insert("error", error);
res.insert("id", d->currentRequestId);
QxtWebPageEvent *err = new QxtWebPageEvent(event->sessionID, event->requestID,
QxtJSON::stringify(res).toUtf8() + "\r\n");
postEvent(err);
}
/*!
Returns the current absolute url of this service depending on the request \a event.
*/

View File

@ -35,7 +35,6 @@
#include "qxtabstractwebservice.h"
#include <QUrl>
class QXT_WEB_EXPORT QxtWebJsonRPCService : public QxtAbstractWebService
{
Q_OBJECT
@ -44,6 +43,8 @@ public:
virtual ~QxtWebJsonRPCService();
protected:
void throwRPCError(QVariant error);
QUrl self(QxtWebRequestEvent* event);
virtual void pageRequestedEvent(QxtWebRequestEvent* event);
virtual void functionInvokedEvent(QxtWebRequestEvent* event);

View File

@ -54,6 +54,10 @@ public:
};
QMap<QByteArray, Method> methods;
QxtWebRequestEvent *currentRequest;
QVariant currentRequestId;
bool requestCanceled;
public slots:
void readFinished();
void handle(QxtWebContent *);