pub struct GErr<C: Config = DefaultConfig, D = NoData> { /* private fields */ }Expand description
GErr - Structured Error Type.
It’s generically configurable with id, code, data, etc.
Error location is automatically generated.
It contains stack-trace if backtrace feature is enabled.
It provides 2 type parameters C and D:
Cis for error config. It bounds withConfigtrait containing error id and code.Idtype: defining error id type.id()function: defining auto-generation function for id.CODEcode constant: automatically set error code at construction.displayfunction: forDisplaytrait.
Dis for error data.
GErr contains error as having these attributes:
id: The error id. This is to identify the error. It can be autogenerated or from outside(e.g. request id).code: The error code. Acts as label/category/group of errors.message: The error message.sources: The sources of current error. Errors can be linked/chained from multiple errors, serial or parallel.tags: The error tags. Things related to the error. For easy querying.data: The error data. This is additional data about the error. It can be error kinds(enum), user data triggering the errors, etc.help: Hint about how to solve the error.location: Where the error happen.backtrace: The error stacktrace, feature-gated behindbacktrace.
§Note
Set aside size of error ID and Data:
GErr<DefaultConfig, NoData>is approximately 160 bytes.- Enabling the
backtracefeature increases this to approximately 208 bytes (+48 bytes fromBacktrace).
Clippy’s result_large_err lint is intentionally allowed for this crate.
Users requiring a smaller error representation may box GErr in their
own APIs or using provided GErrBox.
Implementations§
Source§impl<C: Config, D> GErr<C, D>
impl<C: Config, D> GErr<C, D>
Sourcepub fn from_error<E>(err: E) -> Self
pub fn from_error<E>(err: E) -> Self
Constructs new GErr from any error implementing trait Error
Id is auto-generated and only contains message, location and the error as source.
Sourcepub fn new_with_id<M>(id: C::Id, message: M) -> Self
pub fn new_with_id<M>(id: C::Id, message: M) -> Self
Constructs new GErr with id and message.
This is to set error id manually.
Source§impl<C: Config, D> GErr<C, D>
impl<C: Config, D> GErr<C, D>
Sourcepub fn set_sources<I>(self, sources: I) -> Selfwhere
I: IntoIterator<Item = Source>,
pub fn set_sources<I>(self, sources: I) -> Selfwhere
I: IntoIterator<Item = Source>,
Set list of sources.
Sourcepub fn add_source<E>(self, source: E) -> Self
pub fn add_source<E>(self, source: E) -> Self
Add general error(non-gerr) into list of sources.
Sourcepub fn add_source_gerr<E>(self, gerr: E) -> Self
pub fn add_source_gerr<E>(self, gerr: E) -> Self
Add GErr as error source.
Any errors implementing Into<GErrSource> pass.
Add multiple tags at once to GErr.
Sourcepub fn with_config<T: Config>(self) -> GErr<T, D>
pub fn with_config<T: Config>(self) -> GErr<T, D>
Override config type and auto-generate id and code.
This will overwrite previous id and code with auto-generated ones.
Sourcepub fn with_data_type<T>(self) -> GErr<C, T>
pub fn with_data_type<T>(self) -> GErr<C, T>
Override Data(D) type.
Sourcepub fn with_data<T>(self, data: T) -> GErr<C, T>
pub fn with_data<T>(self, data: T) -> GErr<C, T>
Override Data(D) value and type.
This returns new GErr with different type of Data.
Error tags.
Iterate over tags.
Sourcepub fn location(&self) -> &ErrorLocation
pub fn location(&self) -> &ErrorLocation
Error location.
Source§impl<C: Config, D> GErr<C, D>
impl<C: Config, D> GErr<C, D>
Sourcepub fn into_gerr_source(self) -> GErrSource
pub fn into_gerr_source(self) -> GErrSource
Converts GErr into GErrSource.
Source§impl<C: Config, D> GErr<C, D>
impl<C: Config, D> GErr<C, D>
Sourcepub fn display_json_data(&self) -> DisplayJsonData
pub fn display_json_data(&self) -> DisplayJsonData
Public JSON data of GErr.
Source§impl<C: Config, D> GErr<C, D>
impl<C: Config, D> GErr<C, D>
Sourcepub fn iter_by_code<'a, 'b>(
&'a self,
code: &'b str,
) -> impl Iterator<Item = GErrNode<'a, C, D>> + 'awhere
'b: 'a,
pub fn iter_by_code<'a, 'b>(
&'a self,
code: &'b str,
) -> impl Iterator<Item = GErrNode<'a, C, D>> + 'awhere
'b: 'a,
Iterate over GErr’s codes.
Sourcepub fn iter_by_tag<'a, 'b>(
&'a self,
tag: &'b str,
) -> impl Iterator<Item = GErrNode<'a, C, D>> + 'awhere
'b: 'a,
pub fn iter_by_tag<'a, 'b>(
&'a self,
tag: &'b str,
) -> impl Iterator<Item = GErrNode<'a, C, D>> + 'awhere
'b: 'a,
Iterate over GErr’s tags.
Sourcepub fn iter_id<T>(&self) -> impl Iterator<Item = GErrNode<'_, C, D>>where
T: Any,
pub fn iter_id<T>(&self) -> impl Iterator<Item = GErrNode<'_, C, D>>where
T: Any,
Iterate over GErr’s id type.
Sourcepub fn iter_by_id<'a, 'b, T>(
&'a self,
value: &'b T,
) -> impl Iterator<Item = GErrNode<'a, C, D>>
pub fn iter_by_id<'a, 'b, T>( &'a self, value: &'b T, ) -> impl Iterator<Item = GErrNode<'a, C, D>>
Iterate over GErr’s id.
Sourcepub fn iter_data<T>(&self) -> impl Iterator<Item = GErrNode<'_, C, D>>where
T: Any,
pub fn iter_data<T>(&self) -> impl Iterator<Item = GErrNode<'_, C, D>>where
T: Any,
Iterate over GErr’s data’s type.
Sourcepub fn iter_by_data<'a, 'b, T>(
&'a self,
value: &'b T,
) -> impl Iterator<Item = GErrNode<'a, C, D>>
pub fn iter_by_data<'a, 'b, T>( &'a self, value: &'b T, ) -> impl Iterator<Item = GErrNode<'a, C, D>>
Iterate over GErr’s data.
Sourcepub fn iter_source<E>(&self) -> impl Iterator<Item = GErrNode<'_, C, D>>where
E: Error + 'static,
pub fn iter_source<E>(&self) -> impl Iterator<Item = GErrNode<'_, C, D>>where
E: Error + 'static,
Iterate over GErr’s sources by source’s type.
Trait Implementations§
Source§impl<C: Config, D> Error for GErr<C, D>
impl<C: Config, D> Error for GErr<C, D>
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
use the Display impl or to_string()
Source§impl<C: Config, D> From<Box<GErr<C, D>>> for GErrSource
Available on crate feature serde only.
impl<C: Config, D> From<Box<GErr<C, D>>> for GErrSource
serde only.