zellij-server: improve thread_bus error handling (#1775)

* zellij-server: improve thread_bus error handling

* zellij-server/thread_bus: get rid of option.unwrap

* zellij-utils/errors.rs: generic error in to_anyhow
This commit is contained in:
Bohdan Ivashko 2022-10-07 12:04:08 +03:00 committed by GitHub
parent bc04983f06
commit 39f33a9a9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 35 deletions

View file

@ -971,7 +971,6 @@ impl Screen {
Some(*client_id), Some(*client_id),
Event::TabUpdate(tab_data), Event::TabUpdate(tab_data),
)) ))
.to_anyhow()
.context("failed to update tabs")?; .context("failed to update tabs")?;
} }
Ok(()) Ok(())
@ -1167,7 +1166,6 @@ impl Screen {
self.bus self.bus
.senders .senders
.send_to_server(ServerInstruction::UnblockInputThread) .send_to_server(ServerInstruction::UnblockInputThread)
.context("failed to send message to server")
.context("failed to unblock input") .context("failed to unblock input")
} }
} }
@ -1871,7 +1869,6 @@ pub(crate) fn screen_thread_main(
.bus .bus
.senders .senders
.send_to_server(*instruction) .send_to_server(*instruction)
.context("failed to send message to server")
.context("failed to confirm prompt")?; .context("failed to confirm prompt")?;
} }
screen.unblock_input()?; screen.unblock_input()?;

View file

@ -497,7 +497,6 @@ impl Tab {
let pane_title = run.location.to_string(); let pane_title = run.location.to_string();
self.senders self.senders
.send_to_plugin(PluginInstruction::Load(pid_tx, run, tab_index, client_id)) .send_to_plugin(PluginInstruction::Load(pid_tx, run, tab_index, client_id))
.to_anyhow()
.with_context(err_context)?; .with_context(err_context)?;
let pid = pid_rx.recv().with_context(err_context)?; let pid = pid_rx.recv().with_context(err_context)?;
let mut new_plugin = PluginPane::new( let mut new_plugin = PluginPane::new(
@ -588,7 +587,6 @@ impl Tab {
Some(*client_id), Some(*client_id),
Event::ModeUpdate(mode_info.clone()), Event::ModeUpdate(mode_info.clone()),
)) ))
.to_anyhow()
.with_context(|| { .with_context(|| {
format!( format!(
"failed to update plugins with mode info {:?}", "failed to update plugins with mode info {:?}",
@ -1169,7 +1167,6 @@ impl Tab {
for key in parse_keys(&input_bytes) { for key in parse_keys(&input_bytes) {
self.senders self.senders
.send_to_plugin(PluginInstruction::Update(Some(pid), None, Event::Key(key))) .send_to_plugin(PluginInstruction::Update(Some(pid), None, Event::Key(key)))
.to_anyhow()
.with_context(err_context)?; .with_context(err_context)?;
} }
}, },
@ -2390,7 +2387,6 @@ impl Tab {
None, None,
Event::CopyToClipboard(self.clipboard_provider.as_copy_destination()), Event::CopyToClipboard(self.clipboard_provider.as_copy_destination()),
)) ))
.to_anyhow()
.with_context(|| { .with_context(|| {
format!("failed to inform plugins about copy selection for client {client_id}") format!("failed to inform plugins about copy selection for client {client_id}")
}) })
@ -2424,7 +2420,6 @@ impl Tab {
}; };
self.senders self.senders
.send_to_plugin(PluginInstruction::Update(None, None, clipboard_event)) .send_to_plugin(PluginInstruction::Update(None, None, clipboard_event))
.to_anyhow()
.context("failed to notify plugins about new clipboard event") .context("failed to notify plugins about new clipboard event")
.non_fatal(); .non_fatal();
@ -2466,7 +2461,6 @@ impl Tab {
None, None,
Event::Visible(visible), Event::Visible(visible),
)) ))
.to_anyhow()
.with_context(|| format!("failed to set visibility of tab to {visible}"))?; .with_context(|| format!("failed to set visibility of tab to {visible}"))?;
} }
Ok(()) Ok(())

View file

@ -4,6 +4,7 @@ use crate::{
os_input_output::ServerOsApi, pty::PtyInstruction, pty_writer::PtyWriteInstruction, os_input_output::ServerOsApi, pty::PtyInstruction, pty_writer::PtyWriteInstruction,
screen::ScreenInstruction, wasm_vm::PluginInstruction, ServerInstruction, screen::ScreenInstruction, wasm_vm::PluginInstruction, ServerInstruction,
}; };
use zellij_utils::errors::prelude::*;
use zellij_utils::{channels, channels::SenderWithContext, errors::ErrorContext}; use zellij_utils::{channels, channels::SenderWithContext, errors::ErrorContext};
/// A container for senders to the different threads in zellij on the server side /// A container for senders to the different threads in zellij on the server side
@ -20,10 +21,7 @@ pub(crate) struct ThreadSenders {
} }
impl ThreadSenders { impl ThreadSenders {
pub fn send_to_screen( pub fn send_to_screen(&self, instruction: ScreenInstruction) -> Result<()> {
&self,
instruction: ScreenInstruction,
) -> Result<(), channels::SendError<(ScreenInstruction, ErrorContext)>> {
if self.should_silently_fail { if self.should_silently_fail {
let _ = self let _ = self
.to_screen .to_screen
@ -32,14 +30,16 @@ impl ThreadSenders {
.unwrap_or_else(|| Ok(())); .unwrap_or_else(|| Ok(()));
Ok(()) Ok(())
} else { } else {
self.to_screen.as_ref().unwrap().send(instruction) self.to_screen
.as_ref()
.context("failed to get screen sender")?
.send(instruction)
.to_anyhow()
.context("failed to send message to screen")
} }
} }
pub fn send_to_pty( pub fn send_to_pty(&self, instruction: PtyInstruction) -> Result<()> {
&self,
instruction: PtyInstruction,
) -> Result<(), channels::SendError<(PtyInstruction, ErrorContext)>> {
if self.should_silently_fail { if self.should_silently_fail {
let _ = self let _ = self
.to_pty .to_pty
@ -48,14 +48,16 @@ impl ThreadSenders {
.unwrap_or_else(|| Ok(())); .unwrap_or_else(|| Ok(()));
Ok(()) Ok(())
} else { } else {
self.to_pty.as_ref().unwrap().send(instruction) self.to_pty
.as_ref()
.context("failed to get pty sender")?
.send(instruction)
.to_anyhow()
.context("failed to send message to pty")
} }
} }
pub fn send_to_plugin( pub fn send_to_plugin(&self, instruction: PluginInstruction) -> Result<()> {
&self,
instruction: PluginInstruction,
) -> Result<(), channels::SendError<(PluginInstruction, ErrorContext)>> {
if self.should_silently_fail { if self.should_silently_fail {
let _ = self let _ = self
.to_plugin .to_plugin
@ -64,14 +66,16 @@ impl ThreadSenders {
.unwrap_or_else(|| Ok(())); .unwrap_or_else(|| Ok(()));
Ok(()) Ok(())
} else { } else {
self.to_plugin.as_ref().unwrap().send(instruction) self.to_plugin
.as_ref()
.context("failed to get plugin sender")?
.send(instruction)
.to_anyhow()
.context("failed to send message to plugin")
} }
} }
pub fn send_to_server( pub fn send_to_server(&self, instruction: ServerInstruction) -> Result<()> {
&self,
instruction: ServerInstruction,
) -> Result<(), channels::SendError<(ServerInstruction, ErrorContext)>> {
if self.should_silently_fail { if self.should_silently_fail {
let _ = self let _ = self
.to_server .to_server
@ -80,13 +84,15 @@ impl ThreadSenders {
.unwrap_or_else(|| Ok(())); .unwrap_or_else(|| Ok(()));
Ok(()) Ok(())
} else { } else {
self.to_server.as_ref().unwrap().send(instruction) self.to_server
.as_ref()
.context("failed to get server sender")?
.send(instruction)
.to_anyhow()
.context("failed to send message to server")
} }
} }
pub fn send_to_pty_writer( pub fn send_to_pty_writer(&self, instruction: PtyWriteInstruction) -> Result<()> {
&self,
instruction: PtyWriteInstruction,
) -> Result<(), channels::SendError<(PtyWriteInstruction, ErrorContext)>> {
if self.should_silently_fail { if self.should_silently_fail {
let _ = self let _ = self
.to_pty_writer .to_pty_writer
@ -95,7 +101,12 @@ impl ThreadSenders {
.unwrap_or_else(|| Ok(())); .unwrap_or_else(|| Ok(()));
Ok(()) Ok(())
} else { } else {
self.to_pty_writer.as_ref().unwrap().send(instruction) self.to_pty_writer
.as_ref()
.context("failed to get pty writer sender")?
.send(instruction)
.to_anyhow()
.context("failed to send message to pty writer")
} }
} }

View file

@ -546,7 +546,7 @@ mod not_wasm {
Err(e) => { Err(e) => {
let (msg, context) = e.into_inner(); let (msg, context) = e.into_inner();
Err( Err(
crate::anyhow::anyhow!("failed to send message to client: {:#?}", msg) crate::anyhow::anyhow!("failed to send message to channel: {:#?}", msg)
.context(context.to_string()), .context(context.to_string()),
) )
}, },