diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-02-11 08:47:36 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-02-11 08:47:36 +0000 |
commit | 6ac871b1df68070cf04085120d588efcb5c55986 (patch) | |
tree | 64011248f5c92002bda04fc990d46363bf0e1967 /crates | |
parent | f8d6d6f23bfb15021be91031ba983da19f0d3ada (diff) | |
parent | 44425eaebcf9c1b3fb8ad311bed83887ab54a43b (diff) |
Merge #3102
3102: Better error messages while deserializing r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_lsp_server/src/lib.rs | 8 | ||||
-rw-r--r-- | crates/ra_lsp_server/src/main.rs | 17 | ||||
-rw-r--r-- | crates/ra_lsp_server/src/main_loop/handlers.rs | 3 |
3 files changed, 17 insertions, 11 deletions
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; | |||
31 | mod world; | 31 | mod world; |
32 | mod diagnostics; | 32 | mod diagnostics; |
33 | 33 | ||
34 | use serde::de::DeserializeOwned; | ||
35 | |||
34 | pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>; | 36 | pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>; |
35 | pub use crate::{ | 37 | pub use crate::{ |
36 | caps::server_capabilities, | 38 | caps::server_capabilities, |
@@ -38,3 +40,9 @@ pub use crate::{ | |||
38 | main_loop::LspError, | 40 | main_loop::LspError, |
39 | main_loop::{main_loop, show_message}, | 41 | main_loop::{main_loop, show_message}, |
40 | }; | 42 | }; |
43 | |||
44 | pub fn from_json<T: DeserializeOwned>(what: &'static str, json: serde_json::Value) -> Result<T> { | ||
45 | let res = T::deserialize(&json) | ||
46 | .map_err(|e| format!("Failed to deserialize {}: {}; {}", what, e, json))?; | ||
47 | Ok(res) | ||
48 | } | ||
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 @@ | |||
1 | //! `ra_lsp_server` binary | 1 | //! `ra_lsp_server` binary |
2 | 2 | ||
3 | use lsp_server::Connection; | 3 | use lsp_server::Connection; |
4 | use ra_lsp_server::{show_message, Result, ServerConfig}; | 4 | use ra_lsp_server::{from_json, show_message, Result, ServerConfig}; |
5 | use ra_prof; | 5 | use ra_prof; |
6 | 6 | ||
7 | fn main() -> Result<()> { | 7 | fn main() -> Result<()> { |
@@ -45,7 +45,8 @@ fn run_server() -> Result<()> { | |||
45 | let server_capabilities = serde_json::to_value(ra_lsp_server::server_capabilities()).unwrap(); | 45 | let server_capabilities = serde_json::to_value(ra_lsp_server::server_capabilities()).unwrap(); |
46 | 46 | ||
47 | let initialize_params = connection.initialize(server_capabilities)?; | 47 | let initialize_params = connection.initialize(server_capabilities)?; |
48 | let initialize_params: lsp_types::InitializeParams = serde_json::from_value(initialize_params)?; | 48 | let initialize_params = |
49 | from_json::<lsp_types::InitializeParams>("InitializeParams", initialize_params)?; | ||
49 | 50 | ||
50 | if let Some(client_info) = initialize_params.client_info { | 51 | if let Some(client_info) = initialize_params.client_info { |
51 | log::info!("Client '{}' {}", client_info.name, client_info.version.unwrap_or_default()); | 52 | log::info!("Client '{}' {}", client_info.name, client_info.version.unwrap_or_default()); |
@@ -62,17 +63,13 @@ fn run_server() -> Result<()> { | |||
62 | .filter(|workspaces| !workspaces.is_empty()) | 63 | .filter(|workspaces| !workspaces.is_empty()) |
63 | .unwrap_or_else(|| vec![root]); | 64 | .unwrap_or_else(|| vec![root]); |
64 | 65 | ||
65 | let server_config: ServerConfig = initialize_params | 66 | let server_config = initialize_params |
66 | .initialization_options | 67 | .initialization_options |
67 | .and_then(|v| { | 68 | .and_then(|v| { |
68 | serde_json::from_value(v) | 69 | from_json::<ServerConfig>("config", v) |
69 | .map_err(|e| { | 70 | .map_err(|e| { |
70 | log::error!("failed to deserialize config: {}", e); | 71 | log::error!("{}", e); |
71 | show_message( | 72 | show_message(lsp_types::MessageType::Error, e.to_string(), &connection.sender); |
72 | lsp_types::MessageType::Error, | ||
73 | format!("failed to deserialize config: {}", e), | ||
74 | &connection.sender, | ||
75 | ); | ||
76 | }) | 73 | }) |
77 | .ok() | 74 | .ok() |
78 | }) | 75 | }) |
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::{ | |||
35 | TryConvWithToVec, | 35 | TryConvWithToVec, |
36 | }, | 36 | }, |
37 | diagnostics::DiagnosticTask, | 37 | diagnostics::DiagnosticTask, |
38 | from_json, | ||
38 | req::{self, Decoration, InlayHint, InlayHintsParams, InlayKind}, | 39 | req::{self, Decoration, InlayHint, InlayHintsParams, InlayKind}, |
39 | world::WorldSnapshot, | 40 | world::WorldSnapshot, |
40 | LspError, Result, | 41 | LspError, Result, |
@@ -811,7 +812,7 @@ enum CodeLensResolveData { | |||
811 | pub fn handle_code_lens_resolve(world: WorldSnapshot, code_lens: CodeLens) -> Result<CodeLens> { | 812 | pub fn handle_code_lens_resolve(world: WorldSnapshot, code_lens: CodeLens) -> Result<CodeLens> { |
812 | let _p = profile("handle_code_lens_resolve"); | 813 | let _p = profile("handle_code_lens_resolve"); |
813 | let data = code_lens.data.unwrap(); | 814 | let data = code_lens.data.unwrap(); |
814 | let resolve = serde_json::from_value(data)?; | 815 | let resolve = from_json::<Option<CodeLensResolveData>>("CodeLensResolveData", data)?; |
815 | match resolve { | 816 | match resolve { |
816 | Some(CodeLensResolveData::Impls(lens_params)) => { | 817 | Some(CodeLensResolveData::Impls(lens_params)) => { |
817 | let locations: Vec<Location> = | 818 | let locations: Vec<Location> = |