From 9709bd39ca8a564d517372ee33304d34ac0b09bf Mon Sep 17 00:00:00 2001 From: Muhammad Mominul Huque Date: Sat, 15 Jun 2019 02:42:56 +0600 Subject: Get rid of failure: ra_lsp_server & ra_project_model --- crates/ra_lsp_server/src/lib.rs | 2 +- crates/ra_lsp_server/src/main.rs | 2 +- crates/ra_lsp_server/src/main_loop.rs | 31 ++++++++++++++++++------------- crates/ra_lsp_server/src/world.rs | 11 +++++------ 4 files changed, 25 insertions(+), 21 deletions(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/lib.rs b/crates/ra_lsp_server/src/lib.rs index aabde420b..14cfa401f 100644 --- a/crates/ra_lsp_server/src/lib.rs +++ b/crates/ra_lsp_server/src/lib.rs @@ -9,5 +9,5 @@ pub mod req; pub mod init; mod world; -pub type Result = ::std::result::Result; +pub type Result = std::result::Result>; pub use crate::{caps::server_capabilities, main_loop::main_loop, main_loop::LspError, init::InitializationOptions}; diff --git a/crates/ra_lsp_server/src/main.rs b/crates/ra_lsp_server/src/main.rs index 3c3e8b5b0..7749d97d6 100644 --- a/crates/ra_lsp_server/src/main.rs +++ b/crates/ra_lsp_server/src/main.rs @@ -25,7 +25,7 @@ fn main() -> Result<()> { } Err(_) => { log::error!("server panicked"); - failure::bail!("server panicked") + Err("server panicked")? } } } diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index 0790ea472..fe6b360d4 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -2,11 +2,9 @@ mod handlers; mod subscriptions; pub(crate) mod pending_requests; -use std::{fmt, path::PathBuf, sync::Arc, time::Instant}; +use std::{fmt, path::PathBuf, sync::Arc, time::Instant, error::Error}; use crossbeam_channel::{select, unbounded, Receiver, RecvError, Sender}; -use failure::{bail, format_err}; -use failure_derive::Fail; use gen_lsp_server::{ handle_shutdown, ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse, }; @@ -32,8 +30,7 @@ use crate::{ const THREADPOOL_SIZE: usize = 8; const MAX_IN_FLIGHT_LIBS: usize = THREADPOOL_SIZE - 3; -#[derive(Debug, Fail)] -#[fail(display = "Language Server request failed with {}. ({})", code, message)] +#[derive(Debug)] pub struct LspError { pub code: i32, pub message: String, @@ -45,6 +42,14 @@ impl LspError { } } +impl fmt::Display for LspError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Language Server request failed with {}. ({})", self.code, self.message) + } +} + +impl Error for LspError {} + pub fn main_loop( ws_roots: Vec, options: InitializationOptions, @@ -177,12 +182,12 @@ fn main_loop_inner( let event = select! { recv(msg_receiver) -> msg => match msg { Ok(msg) => Event::Msg(msg), - Err(RecvError) => bail!("client exited without shutdown"), + Err(RecvError) => Err("client exited without shutdown")?, }, recv(task_receiver) -> task => Event::Task(task.unwrap()), recv(state.vfs.read().task_receiver()) -> task => match task { Ok(task) => Event::Vfs(task), - Err(RecvError) => bail!("vfs died"), + Err(RecvError) => Err("vfs died")?, }, recv(libdata_receiver) -> data => Event::Lib(data.unwrap()) }; @@ -380,7 +385,7 @@ fn on_notification( let not = match not.cast::() { Ok(params) => { let uri = params.text_document.uri; - let path = uri.to_file_path().map_err(|()| format_err!("invalid uri: {}", uri))?; + let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?; if let Some(file_id) = state.vfs.write().add_file_overlay(&path, params.text_document.text) { @@ -393,9 +398,9 @@ fn on_notification( let not = match not.cast::() { Ok(mut params) => { let uri = params.text_document.uri; - let path = uri.to_file_path().map_err(|()| format_err!("invalid uri: {}", uri))?; + let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?; let text = - params.content_changes.pop().ok_or_else(|| format_err!("empty changes"))?.text; + params.content_changes.pop().ok_or_else(|| format!("empty changes"))?.text; state.vfs.write().change_file_overlay(path.as_path(), text); return Ok(()); } @@ -404,7 +409,7 @@ fn on_notification( let not = match not.cast::() { Ok(params) => { let uri = params.text_document.uri; - let path = uri.to_file_path().map_err(|()| format_err!("invalid uri: {}", uri))?; + let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?; if let Some(file_id) = state.vfs.write().remove_file_overlay(path.as_path()) { subs.remove_sub(FileId(file_id.0)); } @@ -546,7 +551,7 @@ where RawResponse::err( id, ErrorCode::InternalError as i32, - format!("{}\n{}", e, e.backtrace()), + e.to_string() ) } } @@ -599,6 +604,6 @@ fn show_message(typ: req::MessageType, message: impl Into, sender: &Send sender.send(not.into()).unwrap(); } -fn is_canceled(e: &failure::Error) -> bool { +fn is_canceled(e: &Box) -> bool { e.downcast_ref::().is_some() } diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index f9ce570ca..7822e1c1c 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs @@ -11,7 +11,6 @@ use ra_ide_api::{ use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot}; use relative_path::RelativePathBuf; use parking_lot::RwLock; -use failure::{Error, format_err}; use gen_lsp_server::ErrorCode; use crate::{ @@ -169,13 +168,13 @@ impl WorldSnapshot { } pub fn uri_to_file_id(&self, uri: &Url) -> Result { - let path = uri.to_file_path().map_err(|()| format_err!("invalid uri: {}", uri))?; + let path = uri.to_file_path().map_err(|()| format!("invalid uri: {}", uri))?; let file = self.vfs.read().path2file(&path).ok_or_else(|| { // Show warning as this file is outside current workspace - Error::from(LspError { + LspError { code: ErrorCode::InvalidRequest as i32, message: "Rust file outside current workspace is not supported yet.".to_string(), - }) + } })?; Ok(FileId(file.0)) } @@ -183,7 +182,7 @@ impl WorldSnapshot { pub fn file_id_to_uri(&self, id: FileId) -> Result { let path = self.vfs.read().file2path(VfsFile(id.0)); let url = Url::from_file_path(&path) - .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?; + .map_err(|_| format!("can't convert path to url: {}", path.display()))?; Ok(url) } @@ -191,7 +190,7 @@ impl WorldSnapshot { let base = self.vfs.read().root2path(VfsRoot(root.0)); let path = path.to_path(base); let url = Url::from_file_path(&path) - .map_err(|_| format_err!("can't convert path to url: {}", path.display()))?; + .map_err(|_| format!("can't convert path to url: {}", path.display()))?; Ok(url) } -- cgit v1.2.3