Skip to content

Commit d07c596

Browse files
Renaming lspAssert to lspRequire plus adding some documentation to make it more clear what this function is supposed to be used for.
1 parent 3ddf5db commit d07c596

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

libsolidity/lsp/FileRepository.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ string FileRepository::sourceUnitNameToUri(string const& _sourceUnitName) const
8888

8989
string FileRepository::uriToSourceUnitName(string const& _path) const
9090
{
91-
lspAssert(boost::algorithm::starts_with(_path, "file://"), ErrorCode::InternalError, "URI must start with file://");
91+
lspRequire(boost::algorithm::starts_with(_path, "file://"), ErrorCode::InternalError, "URI must start with file://");
9292
return stripFileUriSchemePrefix(_path);
9393
}
9494

libsolidity/lsp/LanguageServer.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void LanguageServer::changeConfiguration(Json::Value const& _settings)
183183
else if (text == "directly-opened-and-on-import")
184184
m_fileLoadStrategy = FileLoadStrategy::DirectlyOpenedAndOnImported;
185185
else
186-
lspAssert(false, ErrorCode::InvalidParams, "Invalid file load strategy: " + text);
186+
lspRequire(false, ErrorCode::InvalidParams, "Invalid file load strategy: " + text);
187187
}
188188

189189
m_settingsObject = _settings;
@@ -366,7 +366,7 @@ bool LanguageServer::run()
366366

367367
void LanguageServer::requireServerInitialized()
368368
{
369-
lspAssert(
369+
lspRequire(
370370
m_state == State::Initialized,
371371
ErrorCode::ServerNotInitialized,
372372
"Server is not properly initialized."
@@ -375,7 +375,7 @@ void LanguageServer::requireServerInitialized()
375375

376376
void LanguageServer::handleInitialize(MessageID _id, Json::Value const& _args)
377377
{
378-
lspAssert(
378+
lspRequire(
379379
m_state == State::Started,
380380
ErrorCode::RequestFailed,
381381
"Initialize called at the wrong time."
@@ -389,7 +389,7 @@ void LanguageServer::handleInitialize(MessageID _id, Json::Value const& _args)
389389
if (Json::Value uri = _args["rootUri"])
390390
{
391391
rootPath = uri.asString();
392-
lspAssert(
392+
lspRequire(
393393
boost::starts_with(rootPath, "file://"),
394394
ErrorCode::InvalidParams,
395395
"rootUri only supports file URI scheme."
@@ -471,7 +471,7 @@ void LanguageServer::handleTextDocumentDidOpen(Json::Value const& _args)
471471
{
472472
requireServerInitialized();
473473

474-
lspAssert(
474+
lspRequire(
475475
_args["textDocument"],
476476
ErrorCode::RequestFailed,
477477
"Text document parameter missing."
@@ -492,14 +492,14 @@ void LanguageServer::handleTextDocumentDidChange(Json::Value const& _args)
492492

493493
for (Json::Value jsonContentChange: _args["contentChanges"])
494494
{
495-
lspAssert(
495+
lspRequire(
496496
jsonContentChange.isObject(),
497497
ErrorCode::RequestFailed,
498498
"Invalid content reference."
499499
);
500500

501501
string const sourceUnitName = m_fileRepository.uriToSourceUnitName(uri);
502-
lspAssert(
502+
lspRequire(
503503
m_fileRepository.sourceUnits().count(sourceUnitName),
504504
ErrorCode::RequestFailed,
505505
"Unknown file: " + uri
@@ -509,7 +509,7 @@ void LanguageServer::handleTextDocumentDidChange(Json::Value const& _args)
509509
if (jsonContentChange["range"].isObject()) // otherwise full content update
510510
{
511511
optional<SourceLocation> change = parseRange(m_fileRepository, sourceUnitName, jsonContentChange["range"]);
512-
lspAssert(
512+
lspRequire(
513513
change && change->hasText(),
514514
ErrorCode::RequestFailed,
515515
"Invalid source range: " + util::jsonCompactPrint(jsonContentChange["range"])
@@ -529,7 +529,7 @@ void LanguageServer::handleTextDocumentDidClose(Json::Value const& _args)
529529
{
530530
requireServerInitialized();
531531

532-
lspAssert(
532+
lspRequire(
533533
_args["textDocument"],
534534
ErrorCode::RequestFailed,
535535
"Text document parameter missing."

libsolidity/lsp/Transport.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,14 @@ class RequestError: public util::Exception
7171
ErrorCode m_code;
7272
};
7373

74-
#define lspAssert(condition, errorCode, errorMessage) \
74+
/**
75+
* Ensures precondition check is valid.
76+
* This is supposed to be a recoverable error, that means, if the condition fails to be valid,
77+
* an exception is being raised to be thrown out of the current request handlers
78+
* of the current LSP's client RPC call and this will cause the current request to fail
79+
* with the given error code - but subsequent calls shall be able to continue.
80+
*/
81+
#define lspRequire(condition, errorCode, errorMessage) \
7582
if (!(condition)) \
7683
{ \
7784
BOOST_THROW_EXCEPTION( \

0 commit comments

Comments
 (0)