pub struct TracebackError {
pub message: String,
pub file: String,
pub line: u32,
pub parent: Option<Box<TracebackError>>,
pub time_created: DateTime<Utc>,
pub extra_data: Vec<Value>,
pub project: Option<String>,
pub computer: Option<String>,
pub user: Option<String>,
pub is_parent: bool,
pub is_handled: bool,
/* private fields */
}
Expand description
A custom error struct for handling tracebacks in Rust applications.
This struct is designed to capture error information such as the error message, the file and line where the error occurred, and additional contextual data.
§Examples
Creating a new TracebackError
with a custom message:
use chrono::{DateTime, Utc};
use serde_json::Value;
let error = traceback_error::traceback!("Custom error message");
println!("{:?}", error);
§Fields
message
: A string containing the error message.file
: A string containing the filename where the error occurred.line
: An unsigned integer representing the line number where the error occurred.parent
: An optional boxedTracebackError
representing the parent error, if any.time_created
: Achrono::DateTime<Utc>
indicating when the error was created.extra_data
: Aserde_json::Value
for storing additional error-related data.project
: An optional string representing the project name.computer
: An optional string representing the computer name.user
: An optional string representing the username.is_parent
: A boolean indicating if this error is considered a parent error.is_handled
: A boolean indicating if the error has been handled.is_default
: A boolean indicating if this error is the default error.
§Default Implementation
The Default
trait is implemented for TracebackError
, creating a default instance
with the following values:
message
: “Default message”file
: The current file’s name (usingfile!()
).line
: The current line number (usingline!()
).parent
: Nonetime_created
: The Unix epoch time.extra_data
: Value::Nullproject
: Nonecomputer
: Noneuser
: Noneis_parent
: falseis_handled
: falseis_default
: true
§Equality Comparison
The PartialEq
trait is implemented for TracebackError
, allowing you to compare
two TracebackError
instances for equality based on their message, file, line, and
other relevant fields. The is_handled
and is_default
fields are not considered
when comparing for equality.
§Dropping Errors
Errors are automatically dropped when they go out of scope, but before they are dropped,
they are handled by the TRACEBACK_ERROR_CALLBACK
variable.
By default, this variable is a function simply set to serialize the error and
write it to a JSON file, but the default function can be changed with the
set_callback!
macro.
§Callback Types
The callback function can be either synchronous or asynchronous, depending on the
TracebackCallbackType
set globally using the TRACEBACK_ERROR_CALLBACK
variable.
It can be set using the set_callback!
macro.
- If
TRACEBACK_ERROR_CALLBACK
isSome(TracebackCallbackType::Async)
, an asynchronous callback function is used. - If
TRACEBACK_ERROR_CALLBACK
isSome(TracebackCallbackType::Sync)
, a synchronous callback function is used. - If
TRACEBACK_ERROR_CALLBACK
isNone
, a default callback function is used.
§Creating Errors
You can create a new TracebackError
instance using the traceback!
macro. Additional
data can be added using the with_extra_data
method, and environment variables are
automatically added when the error is being handled.
The additional data should be stored in a serde_json::Value struct.
§Environment Variables
The with_env_vars
method populates the project
, computer
, and user
fields with
information obtained from environment variables (CARGO_PKG_NAME
, COMPUTERNAME
, and
USERNAME
, respectively) or assigns default values if the environment variables are
not present.
§Tracing
Tracing can be essential for diagnosing and debugging issues in your applications. When an
error occurs, you can create a TracebackError
instance to record the error’s details, such
as the error message, the location in the code where it occurred, and additional contextual
information.
Should a function return a TracebackError, it can then be re-captured to trace it even further.
Fields§
§message: String
§file: String
§line: u32
§parent: Option<Box<TracebackError>>
§time_created: DateTime<Utc>
§extra_data: Vec<Value>
§project: Option<String>
§computer: Option<String>
§user: Option<String>
§is_parent: bool
§is_handled: bool
Implementations§
Source§impl TracebackError
impl TracebackError
pub fn new(message: String, file: String, line: u32) -> Self
Sourcepub fn with_extra_data(self, extra_data: Value) -> Self
pub fn with_extra_data(self, extra_data: Value) -> Self
This method allows you to attach additional data to a TracebackError
instance.
This extra data can be valuable when diagnosing and debugging errors,
as it provides context and information related to the error.
§Parameters:
extra_data
: Aserde_json::Value
containing the extra data you want to associate with the error.
§Return Value:
- Returns a modified
TracebackError
instance with the providedextra_data
.
§Example Usage:
use traceback_error::{traceback, TracebackError, serde_json::json};
fn main() {
// Create a new TracebackError with extra data
let error = traceback!().with_extra_data(json!({
"foo": "bar",
"a": "b",
"1": "2"
}));
// Now the error instance contains the specified extra data
}
This method is useful when you want to enrich error objects with additional information relevant to the context in which the error occurred. It ensures that relevant data is available for analysis when handling errors in your Rust application.
Sourcepub fn with_env_vars(self) -> Self
pub fn with_env_vars(self) -> Self
Adds environment variables to the TracebackError.
This method populates the project
, computer
, and user
fields of the TracebackError
based on the values of specific environment variables. If any of these environment variables
are not found, default values are used, and the error message reflects that the information
is unknown due to the missing environment variables.
§Example:
use traceback_error::TracebackError;
// Create a new TracebackError and populate environment variables
let error = TracebackError::new("An error occurred".to_string(), file!().to_string(), line!())
.with_env_vars();
// The error now contains information about the project, computer, and user from
// environment variables, or default values if the environment variables are missing.
§Environment Variables Used:
CARGO_PKG_NAME
: Used to set theproject
field.COMPUTERNAME
: Used to set thecomputer
field.USERNAME
: Used to set theuser
field.
§Returns:
A modified TracebackError
with updated project
, computer
, and user
fields.
Sourcepub fn with_parent(self, parent: TracebackError) -> Self
pub fn with_parent(self, parent: TracebackError) -> Self
The with_parent
method allows you to associate a parent error with the current TracebackError
instance.
This can be useful when you want to create a hierarchical structure of errors, where one error is considered the parent of another.
§Parameters:
parent
: ATracebackError
instance that you want to set as the parent of the current error.
§Return Value:
- Returns a modified
TracebackError
instance with the specified parent error.
§Example:
use traceback_error::TracebackError;
fn main() {
// Create a new TracebackError
let parent_error = TracebackError::new("Parent error".to_string(), file!().to_string(), line!());
// Create a child error with the parent error
let child_error = TracebackError::new("Child error".to_string(), file!().to_string(), line!())
.with_parent(parent_error);
// Now, `child_error` has `parent_error` as its parent
}
The with_parent method is particularly useful when you want to establish relationships between errors, making it easier to understand error hierarchies and diagnose issues.
Trait Implementations§
Source§impl Clone for TracebackError
impl Clone for TracebackError
Source§fn clone(&self) -> TracebackError
fn clone(&self) -> TracebackError
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for TracebackError
impl Debug for TracebackError
Source§impl Default for TracebackError
impl Default for TracebackError
Source§impl<'de> Deserialize<'de> for TracebackError
impl<'de> Deserialize<'de> for TracebackError
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for TracebackError
This display implementation is recursive, and will print the error and all its parents
with a tab in front of each parent.
impl Display for TracebackError
This display implementation is recursive, and will print the error and all its parents with a tab in front of each parent.
Source§impl Drop for TracebackError
impl Drop for TracebackError
Source§impl Error for TracebackError
impl Error for TracebackError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl Error for TracebackError
impl Error for TracebackError
Source§fn custom<T: Display>(msg: T) -> Self
fn custom<T: Display>(msg: T) -> Self
Source§fn invalid_type(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
fn invalid_type(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
Deserialize
receives a type different from what it was
expecting. Read moreSource§fn invalid_value(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
fn invalid_value(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
Deserialize
receives a value of the right type but that
is wrong for some other reason. Read moreSource§fn invalid_length(len: usize, exp: &dyn Expected) -> Self
fn invalid_length(len: usize, exp: &dyn Expected) -> Self
Source§fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self
fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self
Deserialize
enum type received a variant with an
unrecognized name.Source§fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self
fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self
Deserialize
struct type received a field with an
unrecognized name.Source§fn missing_field(field: &'static str) -> Self
fn missing_field(field: &'static str) -> Self
Deserialize
struct type expected to receive a required
field with a particular name but that field was not present in the
input.Source§fn duplicate_field(field: &'static str) -> Self
fn duplicate_field(field: &'static str) -> Self
Deserialize
struct type received more than one of the
same field.