Struct traceback_error::TracebackError
source · 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: 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;
use traceback_error::TracebackError;
let 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 boxedTracebackErrorrepresenting the parent error, if any.time_created: Achrono::DateTime<Utc>indicating when the error was created.extra_data: Aserde_json::Valuefor 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 function.
By default, this function simply serializes the error and writes it to a JSON file.
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_CALLBACKisSome(TracebackCallbackType::Async), an asynchronous callback function is used. - If
TRACEBACK_ERROR_CALLBACKisSome(TracebackCallbackType::Sync), a synchronous callback function is used. - If
TRACEBACK_ERROR_CALLBACKisNone, 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.
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.
Fields§
§message: String§file: String§line: u32§parent: Option<Box<TracebackError>>§time_created: DateTime<Utc>§extra_data: Value§project: Option<String>§computer: Option<String>§user: Option<String>§is_parent: bool§is_handled: boolImplementations§
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
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
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.