Renaming lspAssert to lspRequire plus adding some documentation to make it more clear what this function is supposed to be used for.

This commit is contained in:
Christian Parpart 2022-09-19 14:47:28 +02:00
parent 3ddf5db755
commit d07c596f42
3 changed files with 18 additions and 11 deletions

View File

@ -88,7 +88,7 @@ string FileRepository::sourceUnitNameToUri(string const& _sourceUnitName) const
string FileRepository::uriToSourceUnitName(string const& _path) const string FileRepository::uriToSourceUnitName(string const& _path) const
{ {
lspAssert(boost::algorithm::starts_with(_path, "file://"), ErrorCode::InternalError, "URI must start with file://"); lspRequire(boost::algorithm::starts_with(_path, "file://"), ErrorCode::InternalError, "URI must start with file://");
return stripFileUriSchemePrefix(_path); return stripFileUriSchemePrefix(_path);
} }

View File

@ -183,7 +183,7 @@ void LanguageServer::changeConfiguration(Json::Value const& _settings)
else if (text == "directly-opened-and-on-import") else if (text == "directly-opened-and-on-import")
m_fileLoadStrategy = FileLoadStrategy::DirectlyOpenedAndOnImported; m_fileLoadStrategy = FileLoadStrategy::DirectlyOpenedAndOnImported;
else else
lspAssert(false, ErrorCode::InvalidParams, "Invalid file load strategy: " + text); lspRequire(false, ErrorCode::InvalidParams, "Invalid file load strategy: " + text);
} }
m_settingsObject = _settings; m_settingsObject = _settings;
@ -366,7 +366,7 @@ bool LanguageServer::run()
void LanguageServer::requireServerInitialized() void LanguageServer::requireServerInitialized()
{ {
lspAssert( lspRequire(
m_state == State::Initialized, m_state == State::Initialized,
ErrorCode::ServerNotInitialized, ErrorCode::ServerNotInitialized,
"Server is not properly initialized." "Server is not properly initialized."
@ -375,7 +375,7 @@ void LanguageServer::requireServerInitialized()
void LanguageServer::handleInitialize(MessageID _id, Json::Value const& _args) void LanguageServer::handleInitialize(MessageID _id, Json::Value const& _args)
{ {
lspAssert( lspRequire(
m_state == State::Started, m_state == State::Started,
ErrorCode::RequestFailed, ErrorCode::RequestFailed,
"Initialize called at the wrong time." "Initialize called at the wrong time."
@ -389,7 +389,7 @@ 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();
lspAssert( lspRequire(
boost::starts_with(rootPath, "file://"), boost::starts_with(rootPath, "file://"),
ErrorCode::InvalidParams, ErrorCode::InvalidParams,
"rootUri only supports file URI scheme." "rootUri only supports file URI scheme."
@ -471,7 +471,7 @@ void LanguageServer::handleTextDocumentDidOpen(Json::Value const& _args)
{ {
requireServerInitialized(); requireServerInitialized();
lspAssert( lspRequire(
_args["textDocument"], _args["textDocument"],
ErrorCode::RequestFailed, ErrorCode::RequestFailed,
"Text document parameter missing." "Text document parameter missing."
@ -492,14 +492,14 @@ void LanguageServer::handleTextDocumentDidChange(Json::Value const& _args)
for (Json::Value jsonContentChange: _args["contentChanges"]) for (Json::Value jsonContentChange: _args["contentChanges"])
{ {
lspAssert( lspRequire(
jsonContentChange.isObject(), jsonContentChange.isObject(),
ErrorCode::RequestFailed, ErrorCode::RequestFailed,
"Invalid content reference." "Invalid content reference."
); );
string const sourceUnitName = m_fileRepository.uriToSourceUnitName(uri); string const sourceUnitName = m_fileRepository.uriToSourceUnitName(uri);
lspAssert( lspRequire(
m_fileRepository.sourceUnits().count(sourceUnitName), m_fileRepository.sourceUnits().count(sourceUnitName),
ErrorCode::RequestFailed, ErrorCode::RequestFailed,
"Unknown file: " + uri "Unknown file: " + uri
@ -509,7 +509,7 @@ void LanguageServer::handleTextDocumentDidChange(Json::Value const& _args)
if (jsonContentChange["range"].isObject()) // otherwise full content update if (jsonContentChange["range"].isObject()) // otherwise full content update
{ {
optional<SourceLocation> change = parseRange(m_fileRepository, sourceUnitName, jsonContentChange["range"]); optional<SourceLocation> change = parseRange(m_fileRepository, sourceUnitName, jsonContentChange["range"]);
lspAssert( lspRequire(
change && change->hasText(), change && change->hasText(),
ErrorCode::RequestFailed, ErrorCode::RequestFailed,
"Invalid source range: " + util::jsonCompactPrint(jsonContentChange["range"]) "Invalid source range: " + util::jsonCompactPrint(jsonContentChange["range"])
@ -529,7 +529,7 @@ void LanguageServer::handleTextDocumentDidClose(Json::Value const& _args)
{ {
requireServerInitialized(); requireServerInitialized();
lspAssert( lspRequire(
_args["textDocument"], _args["textDocument"],
ErrorCode::RequestFailed, ErrorCode::RequestFailed,
"Text document parameter missing." "Text document parameter missing."

View File

@ -71,7 +71,14 @@ private:
ErrorCode m_code; ErrorCode m_code;
}; };
#define lspAssert(condition, errorCode, errorMessage) \ /**
* Ensures precondition check is valid.
* This is supposed to be a recoverable error, that means, if the condition fails to be valid,
* an exception is being raised to be thrown out of the current request handlers
* of the current LSP's client RPC call and this will cause the current request to fail
* with the given error code - but subsequent calls shall be able to continue.
*/
#define lspRequire(condition, errorCode, errorMessage) \
if (!(condition)) \ if (!(condition)) \
{ \ { \
BOOST_THROW_EXCEPTION( \ BOOST_THROW_EXCEPTION( \