diff options
| author | Kunal Mohan <44079328+kunalmohan@users.noreply.github.com> | 2021-05-15 23:03:38 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-15 23:03:38 +0530 |
| commit | 4c198b42431e37542da60f9551f3e6d19f84bd18 (patch) | |
| tree | ce50f8ab9e6e514e8503b2a52a37f0b640af236a /src/common/errors.rs | |
| parent | docs(changelog): Remove unused import (diff) | |
| parent | clean and exit on window close (diff) | |
| download | sl0ppij-4c198b42431e37542da60f9551f3e6d19f84bd18.tar.gz sl0ppij-4c198b42431e37542da60f9551f3e6d19f84bd18.tar.xz sl0ppij-4c198b42431e37542da60f9551f3e6d19f84bd18.zip | |
Merge pull request #499 from zellij-org/multiprocess
Switch to a multiprocess client-sever model
Diffstat (limited to 'src/common/errors.rs')
| -rw-r--r-- | src/common/errors.rs | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/src/common/errors.rs b/src/common/errors.rs index c2caf19d..262dfd6b 100644 --- a/src/common/errors.rs +++ b/src/common/errors.rs @@ -19,12 +19,29 @@ const MAX_THREAD_CALL_STACK: usize = 6; use super::thread_bus::SenderWithContext; #[cfg(not(test))] use std::panic::PanicInfo; + +pub trait ErrorInstruction { + fn error(err: String) -> Self; +} + +impl ErrorInstruction for ClientInstruction { + fn error(err: String) -> Self { + ClientInstruction::Error(err) + } +} + +impl ErrorInstruction for ServerInstruction { + fn error(err: String) -> Self { + ServerInstruction::Error(err) + } +} + /// Custom panic handler/hook. Prints the [`ErrorContext`]. #[cfg(not(test))] -pub fn handle_panic( - info: &PanicInfo<'_>, - send_app_instructions: &SenderWithContext<ClientInstruction>, -) { +pub fn handle_panic<T>(info: &PanicInfo<'_>, sender: &SenderWithContext<T>) +where + T: ErrorInstruction + Clone, +{ use backtrace::Backtrace; use std::{process, thread}; let backtrace = Backtrace::new(); @@ -70,7 +87,7 @@ pub fn handle_panic( println!("{}", backtrace); process::exit(1); } else { - let _ = send_app_instructions.send(ClientInstruction::Error(backtrace)); + let _ = sender.send(T::error(backtrace)); } } @@ -103,6 +120,11 @@ impl ErrorContext { break; } } + self.update_thread_ctx() + } + + /// Updates the thread local [`ErrorContext`]. + pub fn update_thread_ctx(&self) { ASYNCOPENCALLS .try_with(|ctx| *ctx.borrow_mut() = *self) .unwrap_or_else(|_| OPENCALLS.with(|ctx| *ctx.borrow_mut() = *self)); @@ -333,6 +355,7 @@ pub enum ClientContext { Error, UnblockInputThread, Render, + ServerError, } impl From<&ClientInstruction> for ClientContext { @@ -340,6 +363,7 @@ impl From<&ClientInstruction> for ClientContext { match *client_instruction { ClientInstruction::Exit => ClientContext::Exit, ClientInstruction::Error(_) => ClientContext::Error, + ClientInstruction::ServerError(_) => ClientContext::ServerError, ClientInstruction::Render(_) => ClientContext::Render, ClientInstruction::UnblockInputThread => ClientContext::UnblockInputThread, } @@ -350,22 +374,20 @@ impl From<&ClientInstruction> for ClientContext { #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] pub enum ServerContext { NewClient, - Action, Render, - TerminalResize, UnblockInputThread, ClientExit, + Error, } impl From<&ServerInstruction> for ServerContext { fn from(server_instruction: &ServerInstruction) -> Self { match *server_instruction { ServerInstruction::NewClient(..) => ServerContext::NewClient, - ServerInstruction::Action(_) => ServerContext::Action, - ServerInstruction::TerminalResize(_) => ServerContext::TerminalResize, ServerInstruction::Render(_) => ServerContext::Render, ServerInstruction::UnblockInputThread => ServerContext::UnblockInputThread, ServerInstruction::ClientExit => ServerContext::ClientExit, + ServerInstruction::Error(_) => ServerContext::Error, } } } |
