[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
This commit is contained in:
Haojian Wu 2017-11-07 10:21:02 +00:00
parent 167f2e34d5
commit 2375c926f4
4 changed files with 29 additions and 9 deletions

View File

@ -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);

View File

@ -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}}},
});
}
}

View File

@ -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);

View File

@ -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;