aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaojian Wu <hokein@google.com>2017-11-07 10:21:02 +0000
committerHaojian Wu <hokein@google.com>2017-11-07 10:21:02 +0000
commita98151e40ddbfdd848e185d112f4c2096e9538b5 (patch)
tree5c193e4c38573104eb8099d05ee5e64c1b83ca44
parent378e2d7631d269fbf7e52e9039be256a4323bfc9 (diff)
[clangd] Add ErrorCode enum class.
Summary: Avoid using magic number in the code everywhere. Reviewers: sammccall Reviewed By: sammccall Subscribers: ilya-biryukov, cfe-commits Differential Revision: https://reviews.llvm.org/D39718 git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@317559 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--clangd/ClangdLSPServer.cpp14
-rw-r--r--clangd/JSONRPCDispatcher.cpp6
-rw-r--r--clangd/JSONRPCDispatcher.h3
-rw-r--r--clangd/Protocol.h15
4 files changed, 29 insertions, 9 deletions
diff --git a/clangd/ClangdLSPServer.cpp b/clangd/ClangdLSPServer.cpp
index c916d537..e2169518 100644
--- a/clangd/ClangdLSPServer.cpp
+++ b/clangd/ClangdLSPServer.cpp
@@ -85,7 +85,8 @@ void ClangdLSPServer::onDocumentDidOpen(Ctx C,
void ClangdLSPServer::onDocumentDidChange(Ctx C,
DidChangeTextDocumentParams &Params) {
if (Params.contentChanges.size() != 1)
- return C.replyError(-32602, "can only apply one change at a time");
+ return C.replyError(ErrorCode::InvalidParams,
+ "can only apply one change at a time");
// We only support full syncing right now.
Server.addDocument(Params.textDocument.uri.file,
Params.contentChanges[0].text);
@@ -119,7 +120,8 @@ void ClangdLSPServer::onCommand(Ctx C, ExecuteCommandParams &Params) {
// parsed in the first place and this handler should not be called. But if
// more commands are added, this will be here has a safe guard.
C.replyError(
- 1, llvm::formatv("Unsupported command \"{0}\".", Params.command).str());
+ ErrorCode::InvalidParams,
+ llvm::formatv("Unsupported command \"{0}\".", Params.command).str());
}
}
@@ -191,7 +193,8 @@ void ClangdLSPServer::onSignatureHelp(Ctx C,
Params.textDocument.uri.file,
Position{Params.position.line, Params.position.character});
if (!SignatureHelp)
- return C.replyError(-32602, llvm::toString(SignatureHelp.takeError()));
+ return C.replyError(ErrorCode::InvalidParams,
+ llvm::toString(SignatureHelp.takeError()));
C.reply(SignatureHelp->Value);
}
@@ -201,7 +204,8 @@ void ClangdLSPServer::onGoToDefinition(Ctx C,
Params.textDocument.uri.file,
Position{Params.position.line, Params.position.character});
if (!Items)
- return C.replyError(-32602, llvm::toString(Items.takeError()));
+ return C.replyError(ErrorCode::InvalidParams,
+ llvm::toString(Items.takeError()));
C.reply(json::ary(Items->Value));
}
@@ -228,7 +232,7 @@ bool ClangdLSPServer::run(std::istream &In) {
// Set up JSONRPCDispatcher.
JSONRPCDispatcher Dispatcher(
[](RequestContext Ctx, llvm::yaml::MappingNode *Params) {
- Ctx.replyError(-32601, "method not found");
+ Ctx.replyError(ErrorCode::MethodNotFound, "method not found");
});
registerCallbackHandlers(Dispatcher, Out, /*Callbacks=*/*this);
diff --git a/clangd/JSONRPCDispatcher.cpp b/clangd/JSONRPCDispatcher.cpp
index 4abe7b7d..52493b23 100644
--- a/clangd/JSONRPCDispatcher.cpp
+++ b/clangd/JSONRPCDispatcher.cpp
@@ -65,13 +65,13 @@ void RequestContext::reply(json::Expr &&Result) {
});
}
-void RequestContext::replyError(int code, const llvm::StringRef &Message) {
- Out.log("Error " + llvm::Twine(code) + ": " + Message + "\n");
+void RequestContext::replyError(ErrorCode code, const llvm::StringRef &Message) {
+ Out.log("Error " + Twine(static_cast<int>(code)) + ": " + Message + "\n");
if (ID) {
Out.writeMessage(json::obj{
{"jsonrpc", "2.0"},
{"id", *ID},
- {"error", json::obj{{"code", code}, {"message", Message}}},
+ {"error", json::obj{{"code", static_cast<int>(code)}, {"message", Message}}},
});
}
}
diff --git a/clangd/JSONRPCDispatcher.h b/clangd/JSONRPCDispatcher.h
index 39ff7b27..afc787a2 100644
--- a/clangd/JSONRPCDispatcher.h
+++ b/clangd/JSONRPCDispatcher.h
@@ -12,6 +12,7 @@
#include "JSONExpr.h"
#include "Logger.h"
+#include "Protocol.h"
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
@@ -60,7 +61,7 @@ public:
/// Sends a successful reply.
void reply(json::Expr &&Result);
/// Sends an error response to the client, and logs it.
- void replyError(int code, const llvm::StringRef &Message);
+ void replyError(ErrorCode code, const llvm::StringRef &Message);
/// Sends a request to the client.
void call(llvm::StringRef Method, json::Expr &&Params);
diff --git a/clangd/Protocol.h b/clangd/Protocol.h
index fcde23b2..e89403f7 100644
--- a/clangd/Protocol.h
+++ b/clangd/Protocol.h
@@ -32,6 +32,21 @@ namespace clangd {
class Logger;
+enum class ErrorCode {
+ // Defined by JSON RPC.
+ ParseError = -32700,
+ InvalidRequest = -32600,
+ MethodNotFound = -32601,
+ InvalidParams = -32602,
+ InternalError = -32603,
+
+ ServerNotInitialized = -32002,
+ UnknownErrorCode = -32001,
+
+ // Defined by the protocol.
+ RequestCancelled = -32800,
+};
+
struct URI {
std::string uri;
std::string file;