errors: Don't unwrap in server::pty_writer (#1872)

* server/pty_writer: Don't unwrap

and handle occuring errors instead. Replace calls to `log::error` with
`non_fatal` instead.

* server/pty_writer: Apply rustfmt
This commit is contained in:
har7an 2022-10-28 14:43:06 +00:00 committed by GitHub
parent f23108f63f
commit 6ae18b4187
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 12 deletions

View file

@ -737,7 +737,7 @@ fn init_session(
None, None,
Some(os_input.clone()), Some(os_input.clone()),
); );
|| pty_writer_main(pty_writer_bus) || pty_writer_main(pty_writer_bus).fatal()
}) })
.unwrap(); .unwrap();

View file

@ -1,4 +1,4 @@
use zellij_utils::errors::{ContextType, PtyWriteContext}; use zellij_utils::errors::{prelude::*, ContextType, PtyWriteContext};
use crate::thread_bus::Bus; use crate::thread_bus::Bus;
@ -17,22 +17,30 @@ impl From<&PtyWriteInstruction> for PtyWriteContext {
} }
} }
pub(crate) fn pty_writer_main(bus: Bus<PtyWriteInstruction>) { pub(crate) fn pty_writer_main(bus: Bus<PtyWriteInstruction>) -> Result<()> {
let err_context = || "failed to write to pty".to_string();
loop { loop {
let (event, mut err_ctx) = bus.recv().expect("failed to receive event on channel"); let (event, mut err_ctx) = bus.recv().with_context(err_context)?;
err_ctx.add_call(ContextType::PtyWrite((&event).into())); err_ctx.add_call(ContextType::PtyWrite((&event).into()));
let os_input = bus.os_input.clone().unwrap(); let os_input = bus
.os_input
.clone()
.context("no OS input API found")
.with_context(err_context)?;
match event { match event {
PtyWriteInstruction::Write(bytes, terminal_id) => { PtyWriteInstruction::Write(bytes, terminal_id) => {
if let Err(e) = os_input.write_to_tty_stdin(terminal_id, &bytes) { os_input
log::error!("failed to write to terminal: {}", e); .write_to_tty_stdin(terminal_id, &bytes)
} .with_context(err_context)
if let Err(e) = os_input.tcdrain(terminal_id) { .non_fatal();
log::error!("failed to drain terminal: {}", e); os_input
}; .tcdrain(terminal_id)
.with_context(err_context)
.non_fatal();
}, },
PtyWriteInstruction::Exit => { PtyWriteInstruction::Exit => {
break; return Ok(());
}, },
} }
} }