Proper error reporting when request fails due to missing file.

This commit is contained in:
Jacob Dufault 2017-05-27 00:10:21 -07:00
parent 385531521f
commit 79a4d8ad79
2 changed files with 95 additions and 69 deletions

View File

@ -201,35 +201,42 @@ void PushBack(NonElidedVector<lsLocation>* result, optional<lsLocation> location
result->push_back(*location);
}
QueryFile* FindFile(QueryDatabase* db, const std::string& filename, QueryFileId* file_id) {
auto it = db->usr_to_file.find(LowerPathIfCaseInsensitive(filename));
bool FindFileOrFail(QueryDatabase* db, lsRequestId id, const std::string& absolute_path, QueryFile** out_query_file, QueryFileId* out_file_id = nullptr) {
auto it = db->usr_to_file.find(LowerPathIfCaseInsensitive(absolute_path));
if (it != db->usr_to_file.end()) {
optional<QueryFile>& file = db->files[it->second.id];
if (file) {
*file_id = QueryFileId(it->second.id);
return &file.value();
*out_query_file = &file.value();
if (out_file_id)
*out_file_id = QueryFileId(it->second.id);
return true;
}
}
std::cerr << "Unable to find file " << filename << std::endl;
*file_id = QueryFileId((size_t)-1);
return nullptr;
if (out_file_id)
*out_file_id = QueryFileId((size_t)-1);
std::cerr << "Unable to find file " << absolute_path << std::endl;
Out_Error out;
out.id = id;
out.error.code = lsErrorCodes::InternalError;
out.error.message = "Unable to find file " + absolute_path;
IpcManager::instance()->SendOutMessageToClient(IpcId::Cout, out);
return false;
}
QueryFile* FindFile(QueryDatabase* db, const std::string& filename) {
auto it = db->usr_to_file.find(LowerPathIfCaseInsensitive(filename));
QueryFile* FindFile(QueryDatabase* db, const std::string& absolute_path) {
auto it = db->usr_to_file.find(LowerPathIfCaseInsensitive(absolute_path));
if (it != db->usr_to_file.end()) {
optional<QueryFile>& file = db->files[it->second.id];
if (file)
return &file.value();
}
std::cerr << "Unable to find file " << filename << std::endl;
return nullptr;
}
optional<QueryLocation> GetDefinitionSpellingOfSymbol(QueryDatabase* db, const QueryTypeId& id) {
optional<QueryType>& type = db->types[id.id];
if (type)
@ -1683,11 +1690,10 @@ bool QueryDbMainLoop(
case IpcId::CqueryTypeHierarchyTree: {
auto msg = static_cast<Ipc_CqueryTypeHierarchyTree*>(message.get());
QueryFile* file = FindFile(db, msg->params.textDocument.uri.GetPath());
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file))
break;
}
WorkingFile* working_file = working_files->GetFileByFilename(file->def.path);
Out_CqueryTypeHierarchyTree response;
@ -1707,11 +1713,10 @@ bool QueryDbMainLoop(
case IpcId::CqueryCallTreeInitial: {
auto msg = static_cast<Ipc_CqueryCallTreeInitial*>(message.get());
QueryFile* file = FindFile(db, msg->params.textDocument.uri.GetPath());
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file))
break;
}
WorkingFile* working_file = working_files->GetFileByFilename(file->def.path);
Out_CqueryCallTree response;
@ -1747,11 +1752,10 @@ bool QueryDbMainLoop(
case IpcId::CqueryVars: {
auto msg = static_cast<Ipc_CqueryVars*>(message.get());
QueryFile* file = FindFile(db, msg->params.textDocument.uri.GetPath());
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file))
break;
}
WorkingFile* working_file = working_files->GetFileByFilename(file->def.path);
Out_LocationList response;
@ -1771,11 +1775,10 @@ bool QueryDbMainLoop(
case IpcId::CqueryCallers: {
auto msg = static_cast<Ipc_CqueryCallers*>(message.get());
QueryFile* file = FindFile(db, msg->params.textDocument.uri.GetPath());
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file))
break;
}
WorkingFile* working_file = working_files->GetFileByFilename(file->def.path);
Out_LocationList response;
@ -1795,11 +1798,10 @@ bool QueryDbMainLoop(
case IpcId::CqueryBase: {
auto msg = static_cast<Ipc_CqueryBase*>(message.get());
QueryFile* file = FindFile(db, msg->params.textDocument.uri.GetPath());
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file))
break;
}
WorkingFile* working_file = working_files->GetFileByFilename(file->def.path);
Out_LocationList response;
@ -1828,11 +1830,10 @@ bool QueryDbMainLoop(
case IpcId::CqueryDerived: {
auto msg = static_cast<Ipc_CqueryDerived*>(message.get());
QueryFile* file = FindFile(db, msg->params.textDocument.uri.GetPath());
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file))
break;
}
WorkingFile* working_file = working_files->GetFileByFilename(file->def.path);
Out_LocationList response;
@ -1942,11 +1943,10 @@ bool QueryDbMainLoop(
auto msg = static_cast<Ipc_TextDocumentRename*>(message.get());
QueryFileId file_id;
QueryFile* file = FindFile(db, msg->params.textDocument.uri.GetPath(), &file_id);
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file, &file_id))
break;
}
WorkingFile* working_file = working_files->GetFileByFilename(file->def.path);
Out_TextDocumentRename response;
@ -2155,11 +2155,10 @@ bool QueryDbMainLoop(
auto msg = static_cast<Ipc_TextDocumentDefinition*>(message.get());
QueryFileId file_id;
QueryFile* file = FindFile(db, msg->params.textDocument.uri.GetPath(), &file_id);
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file, &file_id))
break;
}
WorkingFile* working_file = working_files->GetFileByFilename(file->def.path);
Out_TextDocumentDefinition response;
@ -2231,11 +2230,10 @@ bool QueryDbMainLoop(
auto msg = static_cast<Ipc_TextDocumentDocumentHighlight*>(message.get());
QueryFileId file_id;
QueryFile* file = FindFile(db, msg->params.textDocument.uri.GetPath(), &file_id);
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file, &file_id))
break;
}
WorkingFile* working_file = working_files->GetFileByFilename(file->def.path);
Out_TextDocumentDocumentHighlight response;
@ -2268,11 +2266,10 @@ bool QueryDbMainLoop(
case IpcId::TextDocumentHover: {
auto msg = static_cast<Ipc_TextDocumentHover*>(message.get());
QueryFile* file = FindFile(db, msg->params.textDocument.uri.GetPath());
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file))
break;
}
WorkingFile* working_file = working_files->GetFileByFilename(file->def.path);
Out_TextDocumentHover response;
@ -2296,11 +2293,10 @@ bool QueryDbMainLoop(
case IpcId::TextDocumentReferences: {
auto msg = static_cast<Ipc_TextDocumentReferences*>(message.get());
QueryFile* file = FindFile(db, msg->params.textDocument.uri.GetPath());
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file))
break;
}
WorkingFile* working_file = working_files->GetFileByFilename(file->def.path);
Out_TextDocumentReferences response;
@ -2337,11 +2333,10 @@ bool QueryDbMainLoop(
Out_TextDocumentDocumentSymbol response;
response.id = msg->id;
QueryFile* file = FindFile(db, msg->params.textDocument.uri.GetPath());
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file))
break;
}
for (SymbolRef ref : file->def.outline) {
optional<lsSymbolInformation> info = GetSymbolInfo(db, working_files, ref.idx);
@ -2366,11 +2361,9 @@ bool QueryDbMainLoop(
response.id = msg->id;
if (config->showDocumentLinksOnIncludes) {
QueryFile* file = FindFile(db, msg->params.textDocument.uri.GetPath());
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file))
break;
}
WorkingFile* working_file = working_files->GetFileByFilename(msg->params.textDocument.uri.GetPath());
if (!working_file) {
@ -2454,11 +2447,10 @@ bool QueryDbMainLoop(
clang_complete->NotifyView(path);
QueryFile* file = FindFile(db, path);
if (!file) {
std::cerr << "Unable to find file " << msg->params.textDocument.uri.GetPath() << std::endl;
QueryFile* file;
if (!FindFileOrFail(db, msg->id, msg->params.textDocument.uri.GetPath(), &file))
break;
}
CommonCodeLensParams common;
common.result = &response.result;
common.db = db;

View File

@ -1093,8 +1093,42 @@ MAKE_REFLECT_EMPTY_STRUCT(Ipc_Exit);
enum class lsErrorCodes {
// Defined by JSON RPC
ParseError = -32700,
InvalidRequest = -32600,
MethodNotFound = -32601,
InvalidParams = -32602,
InternalError = -32603,
serverErrorStart = -32099,
serverErrorEnd = -32000,
ServerNotInitialized = -32002,
UnknownErrorCode = -32001,
// Defined by the protocol.
RequestCancelled = -32800,
};
MAKE_REFLECT_TYPE_PROXY(lsErrorCodes, int);
struct Out_Error : public lsOutMessage<Out_Error> {
struct lsResponseError {
// A number indicating the error type that occurred.
lsErrorCodes 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.
// optional<D> data;
};
lsRequestId id;
// The error object in case a request fails.
lsResponseError error;
};
MAKE_REFLECT_STRUCT(Out_Error::lsResponseError, code, message);
MAKE_REFLECT_STRUCT(Out_Error, jsonrpc, id, error);