From 65e46af97a9622ca5be9658f310e700f6b4929ed Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 23 Nov 2018 10:08:44 -0800 Subject: [PATCH] Refactor ReplyOnce & error if InitializeParams.rootUri is null --- src/lsp.hh | 7 ------- src/message_handler.cc | 28 +++++++------------------ src/message_handler.hh | 5 +++-- src/messages/initialize.cc | 4 +++- src/messages/textDocument_formatting.cc | 14 +++++-------- 5 files changed, 19 insertions(+), 39 deletions(-) diff --git a/src/lsp.hh b/src/lsp.hh index b6a60877..2c4c735f 100644 --- a/src/lsp.hh +++ b/src/lsp.hh @@ -62,15 +62,8 @@ enum class ErrorCode { }; struct ResponseError { - // A number indicating the error type that occurred. ErrorCode code; - - // A string providing a short description of the error. std::string message; - - // A Primitive or Structured value that contains additional - // information about the error. Can be omitted. - // std::optional data; }; constexpr char ccls_xref[] = "ccls.xref"; diff --git a/src/message_handler.cc b/src/message_handler.cc index 804bbc92..69a2a3ca 100644 --- a/src/message_handler.cc +++ b/src/message_handler.cc @@ -207,21 +207,13 @@ void MessageHandler::Run(InMessage &msg) { try { it->second(reader, reply); } catch (std::invalid_argument &ex) { - ResponseError err; - err.code = ErrorCode::InvalidParams; - err.message = "invalid params of " + msg.method + ": " + ex.what(); - reply.Error(err); + reply.Error(ErrorCode::InvalidParams, + "invalid params of " + msg.method + ": " + ex.what()); } catch (...) { - ResponseError err; - err.code = ErrorCode::InternalError; - err.message = "failed to process " + msg.method; - reply.Error(err); + reply.Error(ErrorCode::InternalError, "failed to process " + msg.method); } } else { - ResponseError err; - err.code = ErrorCode::MethodNotFound; - err.message = "unknown request " + msg.method; - reply.Error(err); + reply.Error(ErrorCode::MethodNotFound, "unknown request " + msg.method); } } else { auto it = method2notification.find(msg.method); @@ -260,14 +252,10 @@ QueryFile *MessageHandler::FindFile(ReplyOnce &reply, has_entry |= folder.path2entry_index.count(path); } ResponseError err; - if (has_entry) { - err.code = ErrorCode::ServerNotInitialized; - err.message = path + " is being indexed"; - } else { - err.code = ErrorCode::InternalError; - err.message = "unable to find " + path; - } - reply.Error(err); + if (has_entry) + reply.Error(ErrorCode::ServerNotInitialized, path + " is being indexed"); + else + reply.Error(ErrorCode::InternalError, "unable to find " + path); } return ret; diff --git a/src/message_handler.hh b/src/message_handler.hh index 2cbee9be..afc169b5 100644 --- a/src/message_handler.hh +++ b/src/message_handler.hh @@ -199,11 +199,12 @@ using Callback = std::function; struct ReplyOnce { RequestId id; - template void operator()(Res &result) const { + template void operator()(Res &&result) const { if (id.Valid()) pipeline::Reply(id, [&](Writer &w) { Reflect(w, result); }); } - template void Error(Err &err) const { + void Error(ErrorCode code, std::string message) const { + ResponseError err{code, std::move(message)}; if (id.Valid()) pipeline::ReplyError(id, [&](Writer &w) { Reflect(w, err); }); } diff --git a/src/messages/initialize.cc b/src/messages/initialize.cc index 7c93511e..991a2359 100644 --- a/src/messages/initialize.cc +++ b/src/messages/initialize.cc @@ -342,8 +342,10 @@ void Initialize(MessageHandler *m, InitializeParam ¶m, ReplyOnce &reply) { void MessageHandler::initialize(Reader &reader, ReplyOnce &reply) { InitializeParam param; Reflect(reader, param); - if (!param.rootUri) + if (!param.rootUri) { + reply.Error(ErrorCode::InvalidRequest, "expected rootUri"); return; + } Initialize(this, param, reply); } diff --git a/src/messages/textDocument_formatting.cc b/src/messages/textDocument_formatting.cc index 211eac78..271d1a1d 100644 --- a/src/messages/textDocument_formatting.cc +++ b/src/messages/textDocument_formatting.cc @@ -70,15 +70,11 @@ std::vector ReplacementsToEdits(std::string_view code, void Format(ReplyOnce &reply, WorkingFile *wfile, tooling::Range range) { std::string_view code = wfile->buffer_content; auto ReplsOrErr = FormatCode(code, wfile->filename, range); - if (ReplsOrErr) { - auto result = ReplacementsToEdits(code, *ReplsOrErr); - reply(result); - } else { - ResponseError err; - err.code = ErrorCode::UnknownErrorCode; - err.message = llvm::toString(ReplsOrErr.takeError()); - reply.Error(err); - } + if (ReplsOrErr) + reply(ReplacementsToEdits(code, *ReplsOrErr)); + else + reply.Error(ErrorCode::UnknownErrorCode, + llvm::toString(ReplsOrErr.takeError())); } } // namespace