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:
@@ -39,19 +39,25 @@ pub const OAKCODEC_ABI_VERSION: u32 = 1;
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Crate-internal error.
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
/// Null handle or invalid argument.
|
||||
#[error("codec: invalid argument")]
|
||||
Invalid,
|
||||
/// Wrong state.
|
||||
#[error("codec: invalid state")]
|
||||
State,
|
||||
/// Operation failed (context string is log-only).
|
||||
#[error("codec: operation failed: {0}")]
|
||||
Failed(String),
|
||||
/// Not found.
|
||||
#[error("codec: not found")]
|
||||
NotFound,
|
||||
/// Out of memory.
|
||||
#[error("codec: out of memory")]
|
||||
NoMem,
|
||||
/// The operation was cancelled.
|
||||
#[error("codec: operation cancelled")]
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
@@ -68,3 +74,60 @@ impl Error {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Every variant must produce a non-empty `Display` message; the
|
||||
/// `Failed` variant must surface its context string.
|
||||
#[test]
|
||||
fn display_is_non_empty() {
|
||||
for msg in [
|
||||
Error::Invalid.to_string(),
|
||||
Error::State.to_string(),
|
||||
Error::Failed("context".into()).to_string(),
|
||||
Error::NotFound.to_string(),
|
||||
Error::NoMem.to_string(),
|
||||
Error::Cancelled.to_string(),
|
||||
] {
|
||||
assert!(!msg.trim().is_empty(), "empty Display message");
|
||||
}
|
||||
assert!(
|
||||
Error::Failed("context".into())
|
||||
.to_string()
|
||||
.contains("context")
|
||||
);
|
||||
}
|
||||
|
||||
/// `Error` must be usable behind a trait object.
|
||||
#[test]
|
||||
fn error_is_object_safe() {
|
||||
let errs: Vec<Box<dyn std::error::Error>> = vec![
|
||||
Box::new(Error::Invalid),
|
||||
Box::new(Error::State),
|
||||
Box::new(Error::Failed("context".into())),
|
||||
Box::new(Error::NotFound),
|
||||
Box::new(Error::NoMem),
|
||||
Box::new(Error::Cancelled),
|
||||
];
|
||||
assert_eq!(errs.len(), 6);
|
||||
}
|
||||
|
||||
/// No variant wraps a downstream error, so `source()` stays `None`.
|
||||
#[test]
|
||||
fn source_is_none() {
|
||||
assert!(std::error::Error::source(&Error::Failed("context".into())).is_none());
|
||||
}
|
||||
|
||||
/// The `code()` mapping must be unchanged by the `Error` trait impl.
|
||||
#[test]
|
||||
fn code_mapping_unchanged() {
|
||||
assert_eq!(Error::Invalid.code(), OAKCODEC_E_INVALID);
|
||||
assert_eq!(Error::State.code(), OAKCODEC_E_STATE);
|
||||
assert_eq!(Error::Failed("context".into()).code(), OAKCODEC_E_FAILED);
|
||||
assert_eq!(Error::NotFound.code(), OAKCODEC_E_NOT_FOUND);
|
||||
assert_eq!(Error::NoMem.code(), OAKCODEC_E_NOMEM);
|
||||
assert_eq!(Error::Cancelled.code(), OAKCODEC_E_CANCELLED);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user