Skip to content

Commit

Permalink
feat(error): support server errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed May 22, 2022
1 parent 15b4c1c commit 0f54252
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ daemon = [
"simctl",
"process-stream"
]
server = [ "serial", "logging", "dirs", "bsp-server", "url", "wax", "shell-words", "compilation" ]
server = [
"serial",
"logging",
"dirs",
"bsp-server",
"url",
"wax",
"shell-words",
"compilation",
"crossbeam-channel"
]
lua = [ "mlua", "serial", "simctl" ]
serial = [ "serde", "serde_json", "serde_yaml" ]
compilation = [ "serial", "lazy_static", "shell-words", "xcode" ]
Expand Down Expand Up @@ -87,3 +97,4 @@ parity-tokio-ipc = { version = "0.9.0", optional = true }
simctl = { path = "../../../sources/simctl/", optional = true }
thiserror = "1.0.31"
libc = "0.2.126"
crossbeam-channel = { version = "0.5.4", optional = true }
9 changes: 9 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ pub enum Error {
Watch(#[from] WatchError),
#[error("[Error] (Message) {0}")]
Message(String),
#[error("[Error] (send) {0}")]
#[cfg(feature = "server")]
Channel(#[from] crossbeam_channel::SendError<bsp_server::Message>),
#[cfg(feature = "server")]
#[error("[Error] (BSP) {0}")]
BspServer(#[from] bsp_server::ProtocolError),
#[cfg(feature = "server")]
#[error("[Error] (Lock) {0}")]
Lock(String),
}

#[derive(ThisError, Debug)]
Expand Down
34 changes: 19 additions & 15 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Module for communicating with SourceKit Build Server Protocol.
mod extensions;

use anyhow::{Context, Result};
use crate::{Error, Result};
use anyhow::Context;
use bsp_server::{types::*, Connection, Message, Request, RequestId, Response};
use serde_json::{json, Value};
use std::{fs::read_to_string, path::PathBuf};
Expand Down Expand Up @@ -71,8 +72,8 @@ impl BuildServer {
let notification: Message =
OptionsChangedNotification::new(params.uri, flags, root).try_into()?;

conn.send(notification)
.context("notify registration for changes")
conn.send(notification)?;
Ok(())
}

/// List of compiler options necessary to compile a file.
Expand All @@ -90,8 +91,9 @@ impl BuildServer {
let flags = self.file_flags(filepath)?;
let response = OptionsResponse::new(flags, root).as_response(id);

conn.send(response)
.context("Respond to textDocument/sourceKitOptions")
conn.send(response)?;

Ok(())
}

/// Process Workspace BuildTarget request
Expand All @@ -100,8 +102,8 @@ impl BuildServer {
tracing::debug!("Processing");
let response = WorkspaceBuildTargetsResult::new(vec![]);

conn.send((id, response))
.context("Respond to workspace/buildTargets")
conn.send((id, response))?;
Ok(())
}

/// Process BuildTarget output paths
Expand All @@ -114,8 +116,9 @@ impl BuildServer {
) -> Result<()> {
tracing::debug!("Processing");
let response = BuildTargetOutputPathsResponse::new(vec![]).as_response(id);
conn.send(response)
.context("Respond to buildTarget/outputPaths")
conn.send(response)?;

Ok(())
}

/// Process BuildTarget Sources Request
Expand All @@ -128,8 +131,8 @@ impl BuildServer {
) -> Result<()> {
tracing::debug!("Processing");
let response = BuildTargetSourcesResult::new(vec![]);
conn.send((id, response))
.context("Respond to buildTarget/outputPaths")
conn.send((id, response))?;
Ok(())
}

/// Return Default response for unhandled requests.
Expand All @@ -146,8 +149,8 @@ impl BuildServer {
id.clone(),
123,
format!("unhandled method {method}"),
))
.context("Fail to respond")
))?;
Ok(())
}

/// Handle Shutdown Request
Expand All @@ -171,8 +174,9 @@ impl BuildServer {
.to_vec()
.pipe(Result::Ok),
Err(err) => {
tracing::error!("fail to get file flags {err}");
anyhow::bail!("fail to get file flags {err}")
let msg = format!("fail to get file flags {err}");
tracing::error!("{}", msg);
return Err(Error::Lock(msg));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/bin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use bsp_server::{Connection, Message, Request};
use tracing::Level;
use xbase::Result;
use xbase::{
server::{BuildServer, BuildTargetOutputPathsRequest, OptionsChangedRequest, OptionsRequest},
util::tracing::install_tracing,
Expand Down

0 comments on commit 0f54252

Please sign in to comment.