refactor(crates): implement std::error::Error for all module error enums

thiserror derive across oakundo/oakcommon/oaknode/oaktimeline/oaktask/
oakotio/oakcodec/oakaudio/oakrender/oakplugin/oakengine/oakstorage;
Display carries the module prefix and the Failed context, source()
stays default except oakotio's #[from] forwarding. code() mappings and
variants unchanged; each error.rs gains Display/object-safety/code
regression tests.
This commit is contained in:
2026-08-14 05:45:05 +08:00
parent d460d57805
commit 4240df1d71
29 changed files with 773 additions and 51 deletions
+69 -1
View File
@@ -23,6 +23,8 @@
//! (e.g. -20004 is oakundo's NOT_FOUND, -30001 oaknode's INVALID) and the
//! facade never rewrites them.
use thiserror::Error;
/// Success.
pub const OAKENGINE_OK: i32 = 0;
/// Empty handle or invalid argument.
@@ -43,21 +45,28 @@ pub const OAKENGINE_E_CANCELLED: i32 = -6;
pub type Result<T> = std::result::Result<T, Error>;
/// Crate-internal error.
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum Error {
/// Empty handle or invalid argument.
#[error("engine: invalid argument")]
Invalid,
/// Wrong state.
#[error("engine: call not valid in the current state")]
State,
/// The underlying operation failed (context string is log-only).
#[error("engine: operation failed: {0}")]
Failed(String),
/// Not found.
#[error("engine: not found")]
NotFound,
/// Out of memory.
#[error("engine: out of memory")]
NoMem,
/// Cancelled.
#[error("engine: cancelled")]
Cancelled,
/// A module error code that must pass through untranslated.
#[error("engine: module error code {0}")]
Module(i32),
}
@@ -85,3 +94,62 @@ impl Error {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// One instance of every variant (data-carrying ones get a sample
/// payload).
fn all_errors() -> Vec<Error> {
vec![
Error::Invalid,
Error::State,
Error::Failed("boom".to_string()),
Error::NotFound,
Error::NoMem,
Error::Cancelled,
Error::Module(-20004),
]
}
#[test]
fn display_is_non_empty_for_every_variant() {
for e in all_errors() {
let s = e.to_string();
assert!(!s.is_empty(), "Display produced an empty message for {e:?}");
}
}
#[test]
fn error_is_object_safe() {
// `Box<dyn std::error::Error>` must be constructible for every
// variant; `source()` stays None (no wrapped downstream error).
let errors: Vec<Box<dyn std::error::Error>> = all_errors()
.into_iter()
.map(|e| Box::new(e) as Box<dyn std::error::Error>)
.collect();
for e in &errors {
assert!(!e.to_string().is_empty());
assert!(e.source().is_none());
}
}
#[test]
fn code_is_unaffected_by_trait_impl() {
assert_eq!(Error::Invalid.code(), OAKENGINE_E_INVALID);
assert_eq!(Error::State.code(), OAKENGINE_E_STATE);
assert_eq!(Error::Failed("boom".to_string()).code(), OAKENGINE_E_FAILED);
assert_eq!(Error::NotFound.code(), OAKENGINE_E_NOT_FOUND);
assert_eq!(Error::NoMem.code(), OAKENGINE_E_NOMEM);
assert_eq!(Error::Cancelled.code(), OAKENGINE_E_CANCELLED);
// Module codes pass through verbatim, untranslated.
assert_eq!(Error::Module(-20004).code(), -20004);
}
#[test]
fn from_module_wraps_negative_and_accepts_ok() {
assert!(Error::from_module(0).is_ok());
assert_eq!(Error::from_module(-20004).unwrap_err().code(), -20004);
}
}