From 44425eaebcf9c1b3fb8ad311bed83887ab54a43b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 11 Feb 2020 09:46:45 +0100 Subject: Better error messages while deserializing --- crates/ra_lsp_server/src/lib.rs | 8 ++++++++ crates/ra_lsp_server/src/main.rs | 17 +++++++---------- crates/ra_lsp_server/src/main_loop/handlers.rs | 3 ++- 3 files changed, 17 insertions(+), 11 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 1208c1343..a3464a5a3 100644 --- a/crates/ra_lsp_server/src/lib.rs +++ b/crates/ra_lsp_server/src/lib.rs @@ -31,6 +31,8 @@ mod config; mod world; mod diagnostics; +use serde::de::DeserializeOwned; + pub type Result = std::result::Result>; pub use crate::{ caps::server_capabilities, @@ -38,3 +40,9 @@ pub use crate::{ main_loop::LspError, main_loop::{main_loop, show_message}, }; + +pub fn from_json(what: &'static str, json: serde_json::Value) -> Result { + let res = T::deserialize(&json) + .map_err(|e| format!("Failed to deserialize {}: {}; {}", what, e, json))?; + Ok(res) +} diff --git a/crates/ra_lsp_server/src/main.rs b/crates/ra_lsp_server/src/main.rs index 3879eeff2..c8a017c5c 100644 --- a/crates/ra_lsp_server/src/main.rs +++ b/crates/ra_lsp_server/src/main.rs @@ -1,7 +1,7 @@ //! `ra_lsp_server` binary use lsp_server::Connection; -use ra_lsp_server::{show_message, Result, ServerConfig}; +use ra_lsp_server::{from_json, show_message, Result, ServerConfig}; use ra_prof; fn main() -> Result<()> { @@ -45,7 +45,8 @@ fn run_server() -> Result<()> { let server_capabilities = serde_json::to_value(ra_lsp_server::server_capabilities()).unwrap(); let initialize_params = connection.initialize(server_capabilities)?; - let initialize_params: lsp_types::InitializeParams = serde_json::from_value(initialize_params)?; + let initialize_params = + from_json::("InitializeParams", initialize_params)?; if let Some(client_info) = initialize_params.client_info { log::info!("Client '{}' {}", client_info.name, client_info.version.unwrap_or_default()); @@ -62,17 +63,13 @@ fn run_server() -> Result<()> { .filter(|workspaces| !workspaces.is_empty()) .unwrap_or_else(|| vec![root]); - let server_config: ServerConfig = initialize_params + let server_config = initialize_params .initialization_options .and_then(|v| { - serde_json::from_value(v) + from_json::("config", v) .map_err(|e| { - log::error!("failed to deserialize config: {}", e); - show_message( - lsp_types::MessageType::Error, - format!("failed to deserialize config: {}", e), - &connection.sender, - ); + log::error!("{}", e); + show_message(lsp_types::MessageType::Error, e.to_string(), &connection.sender); }) .ok() }) diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 65e8bc856..59c86bbfa 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -35,6 +35,7 @@ use crate::{ TryConvWithToVec, }, diagnostics::DiagnosticTask, + from_json, req::{self, Decoration, InlayHint, InlayHintsParams, InlayKind}, world::WorldSnapshot, LspError, Result, @@ -811,7 +812,7 @@ enum CodeLensResolveData { pub fn handle_code_lens_resolve(world: WorldSnapshot, code_lens: CodeLens) -> Result { let _p = profile("handle_code_lens_resolve"); let data = code_lens.data.unwrap(); - let resolve = serde_json::from_value(data)?; + let resolve = from_json::>("CodeLensResolveData", data)?; match resolve { Some(CodeLensResolveData::Impls(lens_params)) => { let locations: Vec = -- cgit v1.2.3