1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#![allow(missing_docs, dead_code)]
use crate::compat::{boxed::Box, error::Error as ErrorTrait};
use serde::{Deserialize, Serialize};
use self::code::ErrorCode;
mod code;
mod inner;
pub mod errcode {
pub use super::code::*;
}
#[cfg(feature = "alloc")]
type ErrorData = Box<inner::ErrorData>;
#[cfg(not(feature = "alloc"))]
type ErrorData = Inner;
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(Serialize, Deserialize)]
pub struct Error(ErrorData);
impl Error {
#[cold]
#[track_caller]
#[cfg(feature = "std")]
pub fn new<E>(origin: code::Origin, kind: code::Kind, cause: E) -> Self
where
E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
Self(inner::ErrorData::new(ErrorCode::new(origin, kind), cause).into())
}
#[cold]
#[track_caller]
#[cfg(not(feature = "std"))]
pub fn new<E>(origin: code::Origin, kind: code::Kind, cause: E) -> Self
where
E: core::fmt::Display,
{
Self(inner::ErrorData::new(ErrorCode::new(origin, kind), cause).into())
}
#[cold]
#[cfg(feature = "std")]
#[track_caller]
pub fn new_unknown<E>(origin: code::Origin, cause: E) -> Self
where
E: Into<Box<dyn crate::compat::error::Error + Send + Sync>>,
{
Self::new(origin, code::Kind::Unknown, cause)
}
#[cold]
#[track_caller]
pub fn new_without_cause(origin: code::Origin, kind: code::Kind) -> Self {
Self(inner::ErrorData::new_without_cause(origin, kind).into())
}
pub fn code(&self) -> ErrorCode {
self.0.code
}
#[must_use]
pub fn context(mut self, key: &str, val: impl core::fmt::Display) -> Self {
self.0.add_context(key, &val);
self
}
}
impl core::fmt::Debug for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.0.fmt(f)
}
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.0.fmt(f)
}
}
impl ErrorTrait for Error {
#[cfg(feature = "std")]
fn source(&self) -> Option<&(dyn ErrorTrait + 'static)> {
if let Some(e) = self.0.cause() {
let force_coersion: &(dyn ErrorTrait + 'static) = e;
Some(force_coersion)
} else {
None
}
}
}