Skip to content

Commit cb51dd6

Browse files
committed
refactor(lsp): centralize command execution and error handling
1 parent b922051 commit cb51dd6

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

src/app/lsp_server/lsp_server_inner.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use tower_lsp::lsp_types::{
44
CodeActionOrCommand, CodeActionParams, CodeActionProviderCapability, CodeActionResponse,
55
CodeLens, CodeLensOptions, CodeLensParams, Command, DidChangeConfigurationParams,
66
DidChangeTextDocumentParams, DidOpenTextDocumentParams, ExecuteCommandOptions,
7-
ExecuteCommandParams, InitializeParams, InitializeResult, InitializedParams, Location,
8-
MessageType, ServerCapabilities, TextDocumentSyncCapability, TextDocumentSyncKind,
7+
ExecuteCommandParams, InitializeParams, InitializeResult, InitializedParams, MessageType,
8+
ServerCapabilities, TextDocumentSyncCapability, TextDocumentSyncKind,
99
};
1010
use tracing::{debug, info};
1111

@@ -190,55 +190,61 @@ where
190190

191191
async fn execute_base_image_scan(
192192
&mut self,
193-
location: Location,
193+
location: tower_lsp::lsp_types::Location,
194194
image: String,
195-
) -> Result<Option<Value>> {
195+
) -> Result<()> {
196196
let image_scanner = self.component_factory_mut()?.image_scanner().map_err(|e| {
197197
Error::internal_error().with_message(format!("unable to create image scanner: {e}"))
198198
})?;
199-
let mut command =
200-
ScanBaseImageCommand::new(&image_scanner, &self.interactor, location, image);
201-
command.execute().await.map(|_| None)
199+
ScanBaseImageCommand::new(&image_scanner, &self.interactor, location, image)
200+
.execute()
201+
.await
202202
}
203203

204-
async fn execute_build_and_scan(&mut self, location: Location) -> Result<Option<Value>> {
204+
async fn execute_build_and_scan(
205+
&mut self,
206+
location: tower_lsp::lsp_types::Location,
207+
) -> Result<()> {
205208
let factory = self.component_factory_mut()?;
206209
let image_scanner = factory.image_scanner().map_err(|e| {
207210
Error::internal_error().with_message(format!("unable to create image scanner: {e}"))
208211
})?;
209212
let image_builder = factory.image_builder().map_err(|e| {
210213
Error::internal_error().with_message(format!("unable to create image builder: {e}"))
211214
})?;
212-
let mut command =
213-
BuildAndScanCommand::new(&image_builder, &image_scanner, &self.interactor, location);
214-
command.execute().await.map(|_| None)
215+
BuildAndScanCommand::new(&image_builder, &image_scanner, &self.interactor, location)
216+
.execute()
217+
.await
218+
}
219+
220+
async fn handle_command_error(&self, command_name: &str, e: Error) -> Error {
221+
self.interactor
222+
.show_message(MessageType::ERROR, e.to_string().as_str())
223+
.await;
224+
Error {
225+
code: e.code,
226+
message: format!("error calling command: '{command_name}': {}", e.message).into(),
227+
data: e.data,
228+
}
215229
}
216230

217231
pub async fn execute_command(&mut self, params: ExecuteCommandParams) -> Result<Option<Value>> {
218232
let command: SupportedCommands = params.try_into()?;
233+
let command_name = command.to_string();
219234

220-
let result = match command.clone() {
235+
let result = match command {
221236
SupportedCommands::ExecuteBaseImageScan { location, image } => {
222237
self.execute_base_image_scan(location, image).await
223238
}
224-
225239
SupportedCommands::ExecuteBuildAndScan { location } => {
226240
self.execute_build_and_scan(location).await
227241
}
228242
};
229243

230-
if let Err(e) = &result {
231-
self.interactor
232-
.show_message(MessageType::ERROR, e.to_string().as_str())
233-
.await;
234-
return Err(Error {
235-
code: e.code,
236-
message: format!("error calling command: '{command}': {}", e.message).into(),
237-
data: e.data.clone(),
238-
});
244+
match result {
245+
Ok(_) => Ok(None),
246+
Err(e) => Err(self.handle_command_error(&command_name, e).await),
239247
}
240-
241-
result
242248
}
243249

244250
pub async fn shutdown(&self) -> Result<()> {

0 commit comments

Comments
 (0)