[clang-tools-extra] r317559 - [clangd] Add ErrorCode enum class.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 7 02:21:02 PST 2017


Author: hokein
Date: Tue Nov  7 02:21:02 2017
New Revision: 317559

URL: http://llvm.org/viewvc/llvm-project?rev=317559&view=rev
Log:
[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

Modified:
    clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp
    clang-tools-extra/trunk/clangd/JSONRPCDispatcher.cpp
    clang-tools-extra/trunk/clangd/JSONRPCDispatcher.h
    clang-tools-extra/trunk/clangd/Protocol.h

Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp?rev=317559&r1=317558&r2=317559&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp Tue Nov  7 02:21:02 2017
@@ -85,7 +85,8 @@ void ClangdLSPServer::onDocumentDidOpen(
 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, E
     // 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(Ct
       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(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 &
   // 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);
 

Modified: clang-tools-extra/trunk/clangd/JSONRPCDispatcher.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/JSONRPCDispatcher.cpp?rev=317559&r1=317558&r2=317559&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/JSONRPCDispatcher.cpp (original)
+++ clang-tools-extra/trunk/clangd/JSONRPCDispatcher.cpp Tue Nov  7 02:21:02 2017
@@ -65,13 +65,13 @@ void RequestContext::reply(json::Expr &&
   });
 }
 
-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}}},
     });
   }
 }

Modified: clang-tools-extra/trunk/clangd/JSONRPCDispatcher.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/JSONRPCDispatcher.h?rev=317559&r1=317558&r2=317559&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/JSONRPCDispatcher.h (original)
+++ clang-tools-extra/trunk/clangd/JSONRPCDispatcher.h Tue Nov  7 02:21:02 2017
@@ -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);
 

Modified: clang-tools-extra/trunk/clangd/Protocol.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Protocol.h?rev=317559&r1=317558&r2=317559&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/Protocol.h (original)
+++ clang-tools-extra/trunk/clangd/Protocol.h Tue Nov  7 02:21:02 2017
@@ -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;




More information about the cfe-commits mailing list