zellij/zellij-tile/src/shim.rs
har7an 75801bdb0e
plugins: Improve error handling on plugin version mismatch (#1838)
* server/tab: Don't panic in `Pane::render`

and do not crash the application on failure to receive a render update
from plugins any longer. Instead, will print a simple string with a hint
to check the application logs, where a more thorough error indication
can be found.

* utils/errors: re-export `anyhow::Error`

to create ad-hoc errors with custom error types, without having to wrap
them into a `context()` before to turn the into anyhow errors.

* plugins: Check plugin version on startup

and terminate execution with a descriptive error message in case the
plugin version is incompatible with the version of zellij being run.

* server/wasm_vm: Add plugin path in version error

so the user knows which plugin to look at in case they're using custom
plugins.

* server/wasm_vm: Check plugin version for equality

Previously we would accept cases where the plugin version was newer than
the zellij version, which doesn't make a lot of sense.

* server/wasm_vm: Prettier error handling

in call to `wasmer::Function::call` in case a plugin version mismatch
can occur.

* tile: Install custom panic handler

that will print the panic message to a plugins stdout and then call a
panic handler on the host that turns it into a real application-level
panic.

* tile: Catch errors in event deserialization

and turn them into proper panics. These errors are symptomatic of an
uncaught plugin version mismatch, for example when developing from main
and compiling zellij/the plugins from source. Normal users should never
get to see this error.

* utils/errors: Improve output in `to_stdout`

for anyhow errors. The default anyhow error formatting of `{:?}` is
already very good, and we just made it worse by trying to invent our own
formatting.

* tile: Reword plugin mismatch error message

* zellij: Apply rustfmt

* changelog: Add PR #1838

Improve error handling on plugin version mismatch.

* server/wasm_vm: Rephrase error in passive voice
2022-10-23 13:14:24 +00:00

89 lines
2.1 KiB
Rust

use serde::{de::DeserializeOwned, Serialize};
use std::{io, path::Path};
use zellij_utils::data::*;
use zellij_utils::errors::prelude::*;
// Subscription Handling
pub fn subscribe(event_types: &[EventType]) {
object_to_stdout(&event_types);
unsafe { host_subscribe() };
}
pub fn unsubscribe(event_types: &[EventType]) {
object_to_stdout(&event_types);
unsafe { host_unsubscribe() };
}
// Plugin Settings
pub fn set_selectable(selectable: bool) {
unsafe { host_set_selectable(selectable as i32) };
}
// Query Functions
pub fn get_plugin_ids() -> PluginIds {
unsafe { host_get_plugin_ids() };
object_from_stdin().unwrap()
}
pub fn get_zellij_version() -> String {
unsafe { host_get_zellij_version() };
object_from_stdin().unwrap()
}
// Host Functions
pub fn open_file(path: &Path) {
object_to_stdout(&path);
unsafe { host_open_file() };
}
pub fn switch_tab_to(tab_idx: u32) {
unsafe { host_switch_tab_to(tab_idx) };
}
pub fn set_timeout(secs: f64) {
unsafe { host_set_timeout(secs) };
}
pub fn exec_cmd(cmd: &[&str]) {
object_to_stdout(&cmd);
unsafe { host_exec_cmd() };
}
pub fn report_panic(info: &std::panic::PanicInfo) {
println!("");
println!("A panic occured in a plugin");
println!("{:#?}", info);
unsafe { host_report_panic() };
}
// Internal Functions
#[doc(hidden)]
pub fn object_from_stdin<T: DeserializeOwned>() -> Result<T> {
let err_context = || "failed to deserialize object from stdin".to_string();
let mut json = String::new();
io::stdin().read_line(&mut json).with_context(err_context)?;
serde_json::from_str(&json).with_context(err_context)
}
#[doc(hidden)]
pub fn object_to_stdout(object: &impl Serialize) {
println!("{}", serde_json::to_string(object).unwrap());
}
#[link(wasm_import_module = "zellij")]
extern "C" {
fn host_subscribe();
fn host_unsubscribe();
fn host_set_selectable(selectable: i32);
fn host_get_plugin_ids();
fn host_get_zellij_version();
fn host_open_file();
fn host_switch_tab_to(tab_idx: u32);
fn host_set_timeout(secs: f64);
fn host_exec_cmd();
fn host_report_panic();
}