diff options
author | har7an <99636919+har7an@users.noreply.github.com> | 2022-11-02 05:29:50 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-02 05:29:50 +0000 |
commit | e45a3e58267925b710a5612b4d01b175c2962bcf (patch) | |
tree | 213d9012b71001c3b1358e85ad6f9bb03b07094b /zellij-utils | |
parent | 373351a26578bff1d1a3656a48b5656600f2bb43 (diff) |
errors: Don't unwrap in `server::os_input_output` (#1895)
* server/os_io: Redefine `ServerOsApi` result types
to use `anyhow::Result` instead. This mostly makes the need of custom
`SpawnTerminalError` obsolete (tbd in subsequent commits) and unifies
error handling across the application.
* utils/errors: Implement new `ZellijError` type
to replace any previously defined, isolated custom error types
throughout the application. Currently implements all error variants
found in `SpawnTerminalError`.
In the long term, this will allow zellij to fall back to a single error
type for all application-specific errors, instead of having different
error types per module.
* server/unit/screen: Impl new `ServerOsApi`
with updated `Result`-types.
* server/tab/unit: Impl new `ServerOsApi`
with updated `Result`-types.
* server/os_io: Impl new `ServerOsApi`
with updated `Result`-types.
* utils/ipc: Return `anyhow::Error` in `send`
rather than a `&'static str`, which isn't compatible with
`anyhow::Context`.
* server/tab: Handle `Result` in `resize_pty!`
which is returned due to the changed return types in `ServerOsApi`.
* server/tab: Handle new `Result`s
originating in the change to the `ServerOsApi` trait definition.
* server/screen: Handle new `Result`s
originating in the change to the `ServerOsApi` trait definition.
* server/panes/tiled: Handle new `Result`s
originating in the change to the `ServerOsApi` trait definition.
* server/panes/floating: Handle new `Result`s
originating in the change to the `ServerOsApi` trait definition.
* server/lib: Unwrap on new `Result`s
originating in the change to the `ServerOsApi` trait definition. The
functions here don't return a `Result` yet, this is better left to a
follow-up PR.
* server: Remove `SpawnTerminalError`
and make use of the new `ZellijError` instead. Make use of `anyhow`s
downcast capabilities to restore the underlying original errors where
necessary, as was done previously. This gives us the flexibility to
attach context information to all errors while still allowing us to
handle specific errors in greater detail.
* server/pty: Fix vars broken in rebase
* server/os_io: Remove last `SpawnTerminalError`
* changelog: Add PR #1895
Diffstat (limited to 'zellij-utils')
-rw-r--r-- | zellij-utils/src/errors.rs | 20 | ||||
-rw-r--r-- | zellij-utils/src/ipc.rs | 6 |
2 files changed, 23 insertions, 3 deletions
diff --git a/zellij-utils/src/errors.rs b/zellij-utils/src/errors.rs index 7ec02b9a5..1594912b6 100644 --- a/zellij-utils/src/errors.rs +++ b/zellij-utils/src/errors.rs @@ -24,6 +24,7 @@ pub mod prelude { pub use super::LoggableError; #[cfg(not(target_family = "wasm"))] pub use super::ToAnyhow; + pub use super::ZellijError; pub use anyhow::anyhow; pub use anyhow::bail; pub use anyhow::Context; @@ -376,6 +377,25 @@ pub enum PtyWriteContext { Exit, } +use thiserror::Error; +#[derive(Debug, Error)] +pub enum ZellijError { + #[error("could not find command '{command}' for terminal {terminal_id}")] + CommandNotFound { terminal_id: u32, command: String }, + + #[error("could not determine default editor")] + NoEditorFound, + + #[error("failed to allocate another terminal id")] + NoMoreTerminalIds, + + #[error("failed to start PTY")] + FailedToStartPty, + + #[error("an error occured")] + GenericError { source: anyhow::Error }, +} + #[cfg(not(target_family = "wasm"))] pub use not_wasm::*; diff --git a/zellij-utils/src/ipc.rs b/zellij-utils/src/ipc.rs index 930087886..b264f48e5 100644 --- a/zellij-utils/src/ipc.rs +++ b/zellij-utils/src/ipc.rs @@ -2,7 +2,7 @@ use crate::{ cli::CliArgs, data::{ClientId, InputMode, Style}, - errors::{get_current_ctx, ErrorContext}, + errors::{get_current_ctx, prelude::*, ErrorContext}, input::keybinds::Keybinds, input::{actions::Action, layout::Layout, options::Options, plugins::PluginsConfig}, pane_size::{Size, SizeInPixels}, @@ -154,10 +154,10 @@ impl<T: Serialize> IpcSenderWithContext<T> { } /// Sends an event, along with the current [`ErrorContext`], on this [`IpcSenderWithContext`]'s socket. - pub fn send(&mut self, msg: T) -> Result<(), &'static str> { + pub fn send(&mut self, msg: T) -> Result<()> { let err_ctx = get_current_ctx(); if rmp_serde::encode::write(&mut self.sender, &(msg, err_ctx)).is_err() { - Err("Failed to send message to client") + Err(anyhow!("failed to send message to client")) } else { // TODO: unwrapping here can cause issues when the server disconnects which we don't mind // do we need to handle errors here in other cases? |