Skip to content

Commit

Permalink
Server: Remove panics in tab module (#1748)
Browse files Browse the repository at this point in the history
* utils/errors: Add `ToAnyhow` trait

for converting `Result` types that don't satisfy `anyhow`s trait
constraints (`Display + Send + Sync + 'static`) conveniently.

An example of such a Result is the `SendError` returned from
`send_to_plugins`, which sends `PluginInstruction`s as message type.
One of the enum variants can contain a `mpsc::Sender`, which is `!Sync`
and hence makes the whole `SendError` be `!Sync` in this case. Add an
implementation for this case that takes the message and converts it into
an error containing the message formatted as string, with the additional
`ErrorContext` as anyhow context.

* server/tab: Remove calls to `unwrap()`

and apply error reporting via `anyhow` instead. Make all relevant
functions return `Result`s where previously a panic could occur and
attach error context.

* server/screen: Modify `update_tab!`

to accept an optional 4th parameter, a literal "?". If present, this
will append a `?` to the given closure verbatim to handle/propagate
errors from within the generated macro code.

* server/screen: Handle new `Result`s from `Tab`

and apply appropriate error context and propagate errors further up.

* server/tab/unit: `unwrap` on new `Result`s

* server/unit: Unwrap `Results` in screen tests

* server/tab: Better message for ad-hoc errors

created with `anyhow!`. Since these errors don't have an underlying
cause, we describe the cause in the macro instead and then attach the
error context as usual before `?`ing the error back up.

* utils/cargo: Activate `anyhow`s "backtrace" feature

to capture error backtraces at the error origins (i.e. where we first
receive an error and convert it to a `anyhow::Error`). Since we
propagate error back up the call stack now, the place where we `unwrap`
on errors doesn't match the place where the error originated. Hence, the
callstack, too, is quite misleading since it contains barely any
references of the functions that triggered the error.

As a consequence, we have 2 backtraces now when zellij crashes: One from
`anyhow` (that is implicitly attached to anyhows error reports), and one
from the custom panic handler (which is displayed through `miette`).

* utils/errors: Separate stack traces

in the output of miette. Since we record backtraces with `anyhow` now,
we end up having two backtraces in the output: One from the `anyhow`
error and one from the actual call to `panic`. Adds a comment explaining
the situation and another "section" to the error output of miette: We
print the backtrace from anyhow as "Stack backtrace", and the output
from the panic handler as "Panic backtrace". We keep both for the
(hopefully unlikely) case that the anyhow backtrace isn't existent, so
we still have at least something to work with.

* server/screen: Remove calls to `fatal`

and leave the `panic`ing to the calling function instead.

* server/screen: Remove needless macro

which extended `active_tab!` by passing the client IDs to the closure.
However, this isn't necessary because closures capture their environment
already, and the client_id needn't be mutable.

* server/screen: Handle unused result

* server/screen: Reintroduce arcane macro

that defaults to some default client_id if it isn't valid (e.g. when the
ScreenInstruction is sent via CLI).

* server/tab/unit: Unwrap new results
  • Loading branch information
har7an authored Oct 6, 2022
1 parent 46edc59 commit 6715f46
Show file tree
Hide file tree
Showing 8 changed files with 1,517 additions and 903 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

272 changes: 175 additions & 97 deletions zellij-server/src/screen.rs

Large diffs are not rendered by default.

656 changes: 489 additions & 167 deletions zellij-server/src/tab/mod.rs

Large diffs are not rendered by default.

876 changes: 522 additions & 354 deletions zellij-server/src/tab/unit/tab_integration_tests.rs

Large diffs are not rendered by default.

Loading

0 comments on commit 6715f46

Please sign in to comment.