Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #402 from Xanewok/json-rpc
Browse files Browse the repository at this point in the history
Use jsonrpc-core data for improved and more consistent error handling flow
  • Loading branch information
nrc authored Jul 13, 2017
2 parents 9b79a99 + 4c0dea4 commit d195862
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 90 deletions.
21 changes: 20 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ build = "build.rs"

[dependencies]
cargo = { git = "https://github.com/rust-lang/cargo" }
derive-new = "0.3"
env_logger = "0.4"
languageserver-types = "0.11.1"
log = "0.3"
Expand All @@ -27,3 +26,4 @@ serde_derive = "1.0"
toml = "0.4"
url = "1.1.0"
url_serde = "0.2.0"
jsonrpc-core = "7.0.1"
30 changes: 15 additions & 15 deletions src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use Span;
use build::*;
use lsp_data::*;
use server::{ResponseData, Output, Ack};
use jsonrpc_core::types::ErrorCode;

use std::collections::HashMap;
use std::panic;
Expand Down Expand Up @@ -401,7 +402,7 @@ impl ActionHandler {
}
_ => {
debug!("Error in Racer");
out.failure(id, "GotoDef failed to complete successfully");
out.failure_message(id, ErrorCode::InternalError, "GotoDef failed to complete successfully");
}
}
}
Expand Down Expand Up @@ -445,7 +446,7 @@ impl ActionHandler {
out.success(id, ResponseData::HoverSuccess(r));
}
Err(_) => {
out.failure(id, "Hover failed to complete successfully");
out.failure_message(id, ErrorCode::InternalError, "Hover failed to complete successfully");
}
}
}
Expand All @@ -459,7 +460,7 @@ impl ActionHandler {
}
c => {
debug!("Unknown command: {}", c);
out.failure(id, "Unknown command");
out.failure_message(id, ErrorCode::MethodNotFound, "Unknown command");
}
}
}
Expand Down Expand Up @@ -516,21 +517,20 @@ impl ActionHandler {

// Start by checking that the user has selected a glob import.
if span.range.start() == span.range.end() {
out.failure(id, "Empty selection");
out.failure_message(id, ErrorCode::InvalidParams, "Empty selection");
return;
}
match self.vfs.load_span(span.clone()) {
Ok(s) => {
if s != "*" {
out.failure(id, "Not a glob");
return;
}
Ok(ref s) if s != "*" => {
out.failure_message(id, ErrorCode::InvalidParams, "Not a glob");
return;
}
Err(e) => {
debug!("Deglob failed: {:?}", e);
out.failure(id, "Couldn't open file");
out.failure_message(id, ErrorCode::InternalError, "Couldn't open file");
return;
}
_ => {}
}

// Save-analysis exports the deglobbed version of a glob import as its type string.
Expand All @@ -548,7 +548,7 @@ impl ActionHandler {
let mut deglob_str = match result {
Ok(Ok(s)) => s,
_ => {
out.failure(id, "Couldn't get info from analysis");
out.failure_message(id, ErrorCode::InternalError, "Couldn't get info from analysis");
return;
}
};
Expand Down Expand Up @@ -579,12 +579,12 @@ impl ActionHandler {
Ok(FileContents::Text(s)) => FmtInput::Text(s),
Ok(_) => {
debug!("Reformat failed, found binary file");
out.failure(id, "Reformat failed to complete successfully");
out.failure_message(id, ErrorCode::InternalError, "Reformat failed to complete successfully");
return;
}
Err(e) => {
debug!("Reformat failed: {:?}", e);
out.failure(id, "Reformat failed to complete successfully");
out.failure_message(id, ErrorCode::InternalError, "Reformat failed to complete successfully");
return;
}
};
Expand Down Expand Up @@ -626,12 +626,12 @@ impl ActionHandler {
} else {
debug!("reformat: format_input failed: has errors, summary = {:?}", summary);

out.failure(id, "Reformat failed to complete successfully");
out.failure_message(id, ErrorCode::InternalError, "Reformat failed to complete successfully");
}
}
Err(e) => {
debug!("Reformat failed: {:?}", e);
out.failure(id, "Reformat failed to complete successfully");
out.failure_message(id, ErrorCode::InternalError, "Reformat failed to complete successfully");
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

use analysis::{AnalysisHost, Target};
use config::Config;
use server::{self, ServerMessage, Request, Notification, Method, LsService, ParseError, ResponseData};
use server::{self, ServerMessage, Request, Notification, Method, LsService, ResponseData};
use vfs::Vfs;
use jsonrpc_core;

use ls_types::{ClientCapabilities, TextDocumentPositionParams, TextDocumentIdentifier, TraceOption, Position, InitializeParams, RenameParams};
use std::time::Duration;
Expand Down Expand Up @@ -54,7 +55,7 @@ pub fn run() {
Some(a) => a,
None => continue,
};

// Switch on the action and build an appropriate message.
let msg = match action {
"def" => {
Expand Down Expand Up @@ -216,7 +217,7 @@ impl ChannelMsgReader {
}

impl server::MessageReader for ChannelMsgReader {
fn parsed_message(&self) -> Option<Result<ServerMessage, ParseError>> {
fn parsed_message(&self) -> Option<Result<ServerMessage, Option<jsonrpc_core::Failure>>> {
let channel = self.channel.lock().unwrap();
let msg = channel.recv().expect("Error reading from channel");
Some(Ok(msg))
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#![feature(fnbox)]

extern crate cargo;
#[macro_use]
extern crate derive_new;
extern crate env_logger;
extern crate languageserver_types as ls_types;
#[macro_use]
Expand All @@ -40,6 +38,7 @@ extern crate serde_json;
extern crate toml;
extern crate url;
extern crate url_serde;
extern crate jsonrpc_core;

use std::sync::Arc;

Expand Down
Loading

0 comments on commit d195862

Please sign in to comment.