LSP: Introduces lspAssert(condition, ErrorCode, message)

This commit is contained in:
Christian Parpart 2022-01-05 11:30:04 +01:00
parent c16867cb83
commit 1bd0f9570f
2 changed files with 49 additions and 40 deletions

View File

@ -275,19 +275,19 @@ bool LanguageServer::run()
void LanguageServer::requireServerInitialized() void LanguageServer::requireServerInitialized()
{ {
if (m_state != State::Initialized) lspAssert(
BOOST_THROW_EXCEPTION( m_state == State::Initialized,
RequestError(ErrorCode::ServerNotInitialized) << ErrorCode::ServerNotInitialized,
errinfo_comment("Server is not properly initialized.") "Server is not properly initialized."
); );
} }
void LanguageServer::handleInitialize(MessageID _id, Json::Value const& _args) void LanguageServer::handleInitialize(MessageID _id, Json::Value const& _args)
{ {
if (m_state != State::Started) lspAssert(
BOOST_THROW_EXCEPTION( m_state == State::Started,
RequestError(ErrorCode::RequestFailed) << ErrorCode::RequestFailed,
errinfo_comment("Initialize called at the wrong time.") "Initialize called at the wrong time."
); );
m_state = State::Initialized; m_state = State::Initialized;
@ -298,10 +298,10 @@ void LanguageServer::handleInitialize(MessageID _id, Json::Value const& _args)
if (Json::Value uri = _args["rootUri"]) if (Json::Value uri = _args["rootUri"])
{ {
rootPath = uri.asString(); rootPath = uri.asString();
if (!boost::starts_with(rootPath, "file://")) lspAssert(
BOOST_THROW_EXCEPTION( boost::starts_with(rootPath, "file://"),
RequestError(ErrorCode::InvalidParams) << ErrorCode::InvalidParams,
errinfo_comment("rootUri only supports file URI scheme.") "rootUri only supports file URI scheme."
); );
rootPath = rootPath.substr(7); rootPath = rootPath.substr(7);
@ -334,10 +334,10 @@ void LanguageServer::handleTextDocumentDidOpen(Json::Value const& _args)
{ {
requireServerInitialized(); requireServerInitialized();
if (!_args["textDocument"]) lspAssert(
BOOST_THROW_EXCEPTION( _args["textDocument"],
RequestError(ErrorCode::RequestFailed) << ErrorCode::RequestFailed,
errinfo_comment("Text document parameter missing.") "Text document parameter missing."
); );
string text = _args["textDocument"]["text"].asString(); string text = _args["textDocument"]["text"].asString();
@ -355,27 +355,27 @@ void LanguageServer::handleTextDocumentDidChange(Json::Value const& _args)
for (Json::Value jsonContentChange: _args["contentChanges"]) for (Json::Value jsonContentChange: _args["contentChanges"])
{ {
if (!jsonContentChange.isObject()) lspAssert(
BOOST_THROW_EXCEPTION( jsonContentChange.isObject(),
RequestError(ErrorCode::RequestFailed) << ErrorCode::RequestFailed,
errinfo_comment("Invalid content reference.") "Invalid content reference."
); );
string const sourceUnitName = m_fileRepository.clientPathToSourceUnitName(uri); string const sourceUnitName = m_fileRepository.clientPathToSourceUnitName(uri);
if (!m_fileRepository.sourceUnits().count(sourceUnitName)) lspAssert(
BOOST_THROW_EXCEPTION( m_fileRepository.sourceUnits().count(sourceUnitName),
RequestError(ErrorCode::RequestFailed) << ErrorCode::RequestFailed,
errinfo_comment("Unknown file: " + uri) "Unknown file: " + uri
); );
string text = jsonContentChange["text"].asString(); string text = jsonContentChange["text"].asString();
if (jsonContentChange["range"].isObject()) // otherwise full content update if (jsonContentChange["range"].isObject()) // otherwise full content update
{ {
optional<SourceLocation> change = parseRange(sourceUnitName, jsonContentChange["range"]); optional<SourceLocation> change = parseRange(sourceUnitName, jsonContentChange["range"]);
if (!change || !change->hasText()) lspAssert(
BOOST_THROW_EXCEPTION( change && change->hasText(),
RequestError(ErrorCode::RequestFailed) << ErrorCode::RequestFailed,
errinfo_comment("Invalid source range: " + jsonCompactPrint(jsonContentChange["range"])) "Invalid source range: " + jsonCompactPrint(jsonContentChange["range"])
); );
string buffer = m_fileRepository.sourceUnits().at(sourceUnitName); string buffer = m_fileRepository.sourceUnits().at(sourceUnitName);
@ -392,10 +392,10 @@ void LanguageServer::handleTextDocumentDidClose(Json::Value const& _args)
{ {
requireServerInitialized(); requireServerInitialized();
if (!_args["textDocument"]) lspAssert(
BOOST_THROW_EXCEPTION( _args["textDocument"],
RequestError(ErrorCode::RequestFailed) << ErrorCode::RequestFailed,
errinfo_comment("Text document parameter missing.") "Text document parameter missing."
); );
string uri = _args["textDocument"]["uri"].asString(); string uri = _args["textDocument"]["uri"].asString();

View File

@ -64,6 +64,15 @@ private:
ErrorCode m_code; ErrorCode m_code;
}; };
#define lspAssert(condition, errorCode, errorMessage) \
if (!(condition)) \
{ \
BOOST_THROW_EXCEPTION( \
RequestError(errorCode) << \
errinfo_comment(errorMessage) \
); \
}
/** /**
* Transport layer API * Transport layer API
* *