LSP: add a convenient Error type to the Response

Change-Id: I6059a8891ae3df76a1184001afe9e0d802d649d2
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2018-11-22 09:37:55 +01:00
parent 328289484a
commit a9ffd81f94

View File

@@ -196,7 +196,7 @@ public:
#undef CASE_ERRORCODES #undef CASE_ERRORCODES
}; };
template <typename Result, typename Error> template <typename Result, typename ErrorDataType>
class Response : public JsonRpcMessage class Response : public JsonRpcMessage
{ {
public: public:
@@ -217,13 +217,14 @@ public:
void setResult(const Result &result) { m_jsonObject.insert(resultKey, result); } void setResult(const Result &result) { m_jsonObject.insert(resultKey, result); }
void clearResult() { m_jsonObject.remove(resultKey); } void clearResult() { m_jsonObject.remove(resultKey); }
Utils::optional<ResponseError<Error>> error() const using Error = ResponseError<ErrorDataType>;
Utils::optional<Error> error() const
{ {
const QJsonValue &val = m_jsonObject.value(errorKey); const QJsonValue &val = m_jsonObject.value(errorKey);
return val.isUndefined() ? Utils::nullopt return val.isUndefined() ? Utils::nullopt
: Utils::make_optional(fromJsonValue<ResponseError<Error>>(val)); : Utils::make_optional(fromJsonValue<Error>(val));
} }
void setError(const ResponseError<Error> &error) void setError(const Error &error)
{ m_jsonObject.insert(errorKey, QJsonValue(error)); } { m_jsonObject.insert(errorKey, QJsonValue(error)); }
void clearError() { m_jsonObject.remove(errorKey); } void clearError() { m_jsonObject.remove(errorKey); }
@@ -231,7 +232,7 @@ public:
{ return JsonRpcMessage::isValid(errorMessage) && id().isValid(); } { return JsonRpcMessage::isValid(errorMessage) && id().isValid(); }
}; };
template <typename Result, typename Error, typename Params> template <typename Result, typename ErrorDataType, typename Params>
class Request : public Notification<Params> class Request : public Notification<Params>
{ {
public: public:
@@ -247,7 +248,7 @@ public:
void setId(const MessageId &id) void setId(const MessageId &id)
{ JsonRpcMessage::m_jsonObject.insert(idKey, id.toJson()); } { JsonRpcMessage::m_jsonObject.insert(idKey, id.toJson()); }
using Response = LanguageServerProtocol::Response<Result, Error>; using Response = LanguageServerProtocol::Response<Result, ErrorDataType>;
using ResponseCallback = std::function<void(Response)>; using ResponseCallback = std::function<void(Response)>;
void setResponseCallback(const ResponseCallback &callback) void setResponseCallback(const ResponseCallback &callback)
{ m_callBack = callback; } { m_callBack = callback; }
@@ -263,9 +264,9 @@ public:
JsonRpcMessageHandler::toJsonObject(content, codec, parseError); JsonRpcMessageHandler::toJsonObject(content, codec, parseError);
Response response(object); Response response(object);
if (object.isEmpty()) { if (object.isEmpty()) {
ResponseError<Error> error; ResponseError<ErrorDataType> error;
error.setMessage(parseError); error.setMessage(parseError);
response.setError(ResponseError<Error>()); response.setError(error);
} }
callback(Response(object)); callback(Response(object));
}); });