Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changes/menu-command-panic-on-wrong-input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
tauri: minor:bug
tauri-macros: minor:bug
---

Fix menu related commands can panic if called with invalid menu types through `invoke` directly.

- The internal `do_menu_item!` macro now returns `Err(crate::Error::UnexpectedMenuKind)` instead of `unreachable!()`
- Added a new error type `tauri::Error::UnexpectedMenuKind`
7 changes: 5 additions & 2 deletions crates/tauri-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// SPDX-License-Identifier: MIT

//! Create macros for `tauri::Context`, invoke handler and commands leveraging the `tauri-codegen` crate.
//!
//! Don't depend on this crate directly, use the re-exported types from tauri instead.

#![doc(
html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/.github/icon.png",
Expand Down Expand Up @@ -117,7 +119,7 @@ pub fn default_runtime(attributes: TokenStream, input: TokenStream) -> TokenStre
/// do_menu_item!(resources_table, rid, kind, |i| i.set_text(text), !Check | Submenu);
/// ```
///
/// #### Example
/// ## Examples
///
/// ```ignore
/// let rid = 23;
Expand Down Expand Up @@ -151,9 +153,10 @@ pub fn default_runtime(attributes: TokenStream, input: TokenStream) -> TokenStre
/// let i = resources_table.get::<IconMenuItem<R>>(rid)?;
/// i.set_text(text)
/// }
/// _ => unreachable!(),
/// _ => return Err(crate::Error::UnexpectedMenuKind),
/// }
/// ```
#[doc(hidden)]
#[proc_macro]
pub fn do_menu_item(input: TokenStream) -> TokenStream {
let tokens = parse_macro_input!(input as menu::DoMenuItemInput);
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri-macros/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub fn do_menu_item(input: DoMenuItemInput) -> TokenStream {
#expr
}
)*
_ => unreachable!(),
_ => return Err(crate::Error::UnexpectedMenuKind),
}
}
}
3 changes: 3 additions & 0 deletions crates/tauri/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ pub enum Error {
/// tokio oneshot channel failed to receive message
#[error(transparent)]
TokioOneshotRecv(#[from] tokio::sync::oneshot::error::RecvError),
/// Unexpected menu kind passed to menu/tray plugin command
#[error("Unexpected menu kind")]
UnexpectedMenuKind,
}

impl From<getrandom::Error> for Error {
Expand Down
16 changes: 8 additions & 8 deletions crates/tauri/src/menu/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ fn append<R: Runtime>(
item.with_item(&webview, &resources_table, |i| submenu.append(i))?;
}
}
_ => return Err(anyhow::anyhow!("unexpected menu item kind").into()),
_ => return Err(crate::Error::UnexpectedMenuKind),
};

Ok(())
Expand All @@ -513,7 +513,7 @@ fn prepend<R: Runtime>(
item.with_item(&webview, &resources_table, |i| submenu.prepend(i))?;
}
}
_ => return Err(anyhow::anyhow!("unexpected menu item kind").into()),
_ => return Err(crate::Error::UnexpectedMenuKind),
};

Ok(())
Expand Down Expand Up @@ -543,7 +543,7 @@ fn insert<R: Runtime>(
position += 1
}
}
_ => return Err(anyhow::anyhow!("unexpected menu item kind").into()),
_ => return Err(crate::Error::UnexpectedMenuKind),
};

Ok(())
Expand All @@ -568,7 +568,7 @@ fn remove<R: Runtime>(
do_menu_item!(resources_table, item_rid, item_kind, |i| submenu
.remove(&*i))?;
}
_ => return Err(anyhow::anyhow!("unexpected menu item kind").into()),
_ => return Err(crate::Error::UnexpectedMenuKind),
};

Ok(())
Expand Down Expand Up @@ -609,7 +609,7 @@ fn remove_at<R: Runtime>(
return Ok(Some(make_item_resource!(resources_table, item)));
}
}
_ => return Err(anyhow::anyhow!("unexpected menu item kind").into()),
_ => return Err(crate::Error::UnexpectedMenuKind),
};

Ok(None)
Expand All @@ -625,7 +625,7 @@ fn items<R: Runtime>(
let items = match kind {
ItemKind::Menu => resources_table.get::<Menu<R>>(rid)?.items()?,
ItemKind::Submenu => resources_table.get::<Submenu<R>>(rid)?.items()?,
_ => return Err(anyhow::anyhow!("unexpected menu item kind").into()),
_ => return Err(crate::Error::UnexpectedMenuKind),
};

Ok(
Expand Down Expand Up @@ -657,7 +657,7 @@ fn get<R: Runtime>(
return Ok(Some(make_item_resource!(resources_table, item)));
}
}
_ => return Err(anyhow::anyhow!("unexpected menu item kind").into()),
_ => return Err(crate::Error::UnexpectedMenuKind),
};

Ok(None)
Expand Down Expand Up @@ -687,7 +687,7 @@ async fn popup<R: Runtime>(
let submenu = resources_table.get::<Submenu<R>>(rid)?;
submenu.popup_inner(window, at)?;
}
_ => return Err(anyhow::anyhow!("unexpected menu item kind").into()),
_ => return Err(crate::Error::UnexpectedMenuKind),
};
}

Expand Down
4 changes: 2 additions & 2 deletions crates/tauri/src/tray/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn new<R: Runtime>(
let submenu = resources_table.get::<Submenu<R>>(rid)?;
builder = builder.menu(&*submenu);
}
_ => return Err(anyhow::anyhow!("unexpected menu item kind").into()),
_ => return Err(crate::Error::UnexpectedMenuKind),
};
}
if let Some(icon) = options.icon {
Expand Down Expand Up @@ -144,7 +144,7 @@ fn set_menu<R: Runtime>(
let submenu = webview_resources_table.get::<Submenu<R>>(rid)?;
tray.set_menu(Some((*submenu).clone()))?;
}
_ => return Err(anyhow::anyhow!("unexpected menu item kind").into()),
_ => return Err(crate::Error::UnexpectedMenuKind),
};
} else {
tray.set_menu(None::<Menu<R>>)?;
Expand Down
Loading