Skip to content

Commit 2375c92

Browse files
committedNov 7, 2017
[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 llvm-svn: 317559
1 parent 167f2e3 commit 2375c92

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed
 

‎clang-tools-extra/clangd/ClangdLSPServer.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ void ClangdLSPServer::onDocumentDidOpen(Ctx C,
8585
void ClangdLSPServer::onDocumentDidChange(Ctx C,
8686
DidChangeTextDocumentParams &Params) {
8787
if (Params.contentChanges.size() != 1)
88-
return C.replyError(-32602, "can only apply one change at a time");
88+
return C.replyError(ErrorCode::InvalidParams,
89+
"can only apply one change at a time");
8990
// We only support full syncing right now.
9091
Server.addDocument(Params.textDocument.uri.file,
9192
Params.contentChanges[0].text);
@@ -119,7 +120,8 @@ void ClangdLSPServer::onCommand(Ctx C, ExecuteCommandParams &Params) {
119120
// parsed in the first place and this handler should not be called. But if
120121
// more commands are added, this will be here has a safe guard.
121122
C.replyError(
122-
1, llvm::formatv("Unsupported command \"{0}\".", Params.command).str());
123+
ErrorCode::InvalidParams,
124+
llvm::formatv("Unsupported command \"{0}\".", Params.command).str());
123125
}
124126
}
125127

@@ -191,7 +193,8 @@ void ClangdLSPServer::onSignatureHelp(Ctx C,
191193
Params.textDocument.uri.file,
192194
Position{Params.position.line, Params.position.character});
193195
if (!SignatureHelp)
194-
return C.replyError(-32602, llvm::toString(SignatureHelp.takeError()));
196+
return C.replyError(ErrorCode::InvalidParams,
197+
llvm::toString(SignatureHelp.takeError()));
195198
C.reply(SignatureHelp->Value);
196199
}
197200

@@ -201,7 +204,8 @@ void ClangdLSPServer::onGoToDefinition(Ctx C,
201204
Params.textDocument.uri.file,
202205
Position{Params.position.line, Params.position.character});
203206
if (!Items)
204-
return C.replyError(-32602, llvm::toString(Items.takeError()));
207+
return C.replyError(ErrorCode::InvalidParams,
208+
llvm::toString(Items.takeError()));
205209
C.reply(json::ary(Items->Value));
206210
}
207211

@@ -228,7 +232,7 @@ bool ClangdLSPServer::run(std::istream &In) {
228232
// Set up JSONRPCDispatcher.
229233
JSONRPCDispatcher Dispatcher(
230234
[](RequestContext Ctx, llvm::yaml::MappingNode *Params) {
231-
Ctx.replyError(-32601, "method not found");
235+
Ctx.replyError(ErrorCode::MethodNotFound, "method not found");
232236
});
233237
registerCallbackHandlers(Dispatcher, Out, /*Callbacks=*/*this);
234238

‎clang-tools-extra/clangd/JSONRPCDispatcher.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ void RequestContext::reply(json::Expr &&Result) {
6565
});
6666
}
6767

68-
void RequestContext::replyError(int code, const llvm::StringRef &Message) {
69-
Out.log("Error " + llvm::Twine(code) + ": " + Message + "\n");
68+
void RequestContext::replyError(ErrorCode code, const llvm::StringRef &Message) {
69+
Out.log("Error " + Twine(static_cast<int>(code)) + ": " + Message + "\n");
7070
if (ID) {
7171
Out.writeMessage(json::obj{
7272
{"jsonrpc", "2.0"},
7373
{"id", *ID},
74-
{"error", json::obj{{"code", code}, {"message", Message}}},
74+
{"error", json::obj{{"code", static_cast<int>(code)}, {"message", Message}}},
7575
});
7676
}
7777
}

‎clang-tools-extra/clangd/JSONRPCDispatcher.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "JSONExpr.h"
1414
#include "Logger.h"
15+
#include "Protocol.h"
1516
#include "clang/Basic/LLVM.h"
1617
#include "llvm/ADT/SmallString.h"
1718
#include "llvm/ADT/StringMap.h"
@@ -60,7 +61,7 @@ class RequestContext {
6061
/// Sends a successful reply.
6162
void reply(json::Expr &&Result);
6263
/// Sends an error response to the client, and logs it.
63-
void replyError(int code, const llvm::StringRef &Message);
64+
void replyError(ErrorCode code, const llvm::StringRef &Message);
6465
/// Sends a request to the client.
6566
void call(llvm::StringRef Method, json::Expr &&Params);
6667

‎clang-tools-extra/clangd/Protocol.h

+15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ namespace clangd {
3232

3333
class Logger;
3434

35+
enum class ErrorCode {
36+
// Defined by JSON RPC.
37+
ParseError = -32700,
38+
InvalidRequest = -32600,
39+
MethodNotFound = -32601,
40+
InvalidParams = -32602,
41+
InternalError = -32603,
42+
43+
ServerNotInitialized = -32002,
44+
UnknownErrorCode = -32001,
45+
46+
// Defined by the protocol.
47+
RequestCancelled = -32800,
48+
};
49+
3550
struct URI {
3651
std::string uri;
3752
std::string file;

0 commit comments

Comments
 (0)
Please sign in to comment.