pub struct Error {
pub code: ErrorCode,
pub message: String,
pub context: Option<ErrorContext>,
}Expand description
Main error type for copybook operations
Uses thiserror for clean error handling with manual Display implementation to avoid allocations in hot paths.
§Examples
use copybook_error::{Error, ErrorCode};
let err = Error::new(ErrorCode::CBKP001_SYNTAX, "unexpected token");
assert_eq!(err.code(), ErrorCode::CBKP001_SYNTAX);
assert_eq!(err.family_prefix(), "CBKP");
assert_eq!(err.to_string(), "CBKP001_SYNTAX: unexpected token");Fields§
§code: ErrorCodeStable error code for programmatic handling
message: StringHuman-readable error message
context: Option<ErrorContext>Optional context information
Implementations§
Source§impl Error
impl Error
Sourcepub fn new(code: ErrorCode, message: impl Into<String>) -> Error
pub fn new(code: ErrorCode, message: impl Into<String>) -> Error
Create a new error with the specified code and message
This is the primary constructor for copybook-rs errors. The error code
should be chosen from the stable ErrorCode taxonomy to enable
programmatic error handling.
§Arguments
code- Stable error code from the copybook-rs taxonomymessage- Human-readable error description
§Example
use copybook_error::{Error, ErrorCode};
// Static message
let error1 = Error::new(
ErrorCode::CBKD411_ZONED_BAD_SIGN,
"Invalid sign zone in zoned decimal field"
);
// Dynamic message
let field_name = "AMOUNT";
let error2 = Error::new(
ErrorCode::CBKD411_ZONED_BAD_SIGN,
format!("Invalid sign zone in field {}", field_name)
);Sourcepub fn with_context(self, context: ErrorContext) -> Error
pub fn with_context(self, context: ErrorContext) -> Error
Add context information to the error
Sourcepub fn with_record(self, record_index: u64) -> Error
pub fn with_record(self, record_index: u64) -> Error
Add record context to the error
Sourcepub fn with_field(self, field_path: impl Into<String>) -> Error
pub fn with_field(self, field_path: impl Into<String>) -> Error
Add field path context to the error
Sourcepub fn with_offset(self, byte_offset: u64) -> Error
pub fn with_offset(self, byte_offset: u64) -> Error
Add byte offset context to the error
Trait Implementations§
Source§impl Error for Error
impl Error for Error
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
impl StructuralPartialEq for Error
Auto Trait Implementations§
impl Freeze for Error
impl RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl UnsafeUnpin for Error
impl UnwindSafe for Error
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more