commit 66aabbbd2c4ab13e8ae6d7ea4ec8a2158393a3eb
parent 887a1c6991744be3fc9ab28d275e051f6d109795
Author: triesap <triesap@radroots.dev>
Date: Mon, 19 Jan 2026 06:42:58 +0000
app-utils: add throw_err helper
- add throw_err helper for error panics
- add ErrInput to accept string or IError
- export throw_err helper
- add unit tests for throw_err
Diffstat:
2 files changed, 54 insertions(+), 4 deletions(-)
diff --git a/crates/utils/src/errors.rs b/crates/utils/src/errors.rs
@@ -5,13 +5,45 @@ use radroots_types::types::IError;
pub const ERR_PREFIX_APP: &str = "error.app";
pub const ERR_PREFIX_UTILS: &str = "error.app.utils";
-pub fn err_msg(err: impl Into<String>) -> IError<String> {
- IError { err: err.into() }
+pub enum ErrInput {
+ Message(String),
+ Error(IError<String>),
+}
+
+impl From<String> for ErrInput {
+ fn from(value: String) -> Self {
+ ErrInput::Message(value)
+ }
+}
+
+impl From<&str> for ErrInput {
+ fn from(value: &str) -> Self {
+ ErrInput::Message(value.to_string())
+ }
+}
+
+impl From<IError<String>> for ErrInput {
+ fn from(value: IError<String>) -> Self {
+ ErrInput::Error(value)
+ }
+}
+
+pub fn err_msg(err: impl Into<ErrInput>) -> IError<String> {
+ match err.into() {
+ ErrInput::Message(err) => IError { err },
+ ErrInput::Error(err) => err,
+ }
+}
+
+pub fn throw_err(err: impl Into<ErrInput>) -> ! {
+ let err = err_msg(err);
+ panic!("{}", err.err);
}
#[cfg(test)]
mod tests {
- use super::{err_msg, ERR_PREFIX_APP, ERR_PREFIX_UTILS};
+ use super::{err_msg, throw_err, ERR_PREFIX_APP, ERR_PREFIX_UTILS};
+ use radroots_types::types::IError;
#[test]
fn err_msg_wraps_string() {
@@ -20,6 +52,24 @@ mod tests {
}
#[test]
+ fn err_msg_accepts_error() {
+ let err = err_msg(IError { err: "boom".to_string() });
+ assert_eq!(err.err, "boom");
+ }
+
+ #[test]
+ #[should_panic(expected = "boom")]
+ fn throw_err_panics_with_string() {
+ throw_err("boom");
+ }
+
+ #[test]
+ #[should_panic(expected = "boom")]
+ fn throw_err_panics_with_error() {
+ throw_err(IError { err: "boom".to_string() });
+ }
+
+ #[test]
fn error_prefixes_match_spec() {
assert_eq!(ERR_PREFIX_APP, "error.app");
assert_eq!(ERR_PREFIX_UTILS, "error.app.utils");
diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs
@@ -4,5 +4,5 @@ pub mod error;
pub mod errors;
pub mod types;
-pub use errors::{err_msg, ERR_PREFIX_APP, ERR_PREFIX_UTILS};
+pub use errors::{err_msg, throw_err, ERR_PREFIX_APP, ERR_PREFIX_UTILS};
pub use types::{resolve_err, resolve_ok, ResolveError, ResultPass};