1 use core
::fmt
::{self, Debug, Display}
;
6 /// An error with context around it.
8 /// The context is intended to be a human-readable, user-facing explanation for the
9 /// error that has occurred. The underlying error is not assumed to be end-user-relevant
12 /// The `Display` impl for `Context` only prints the human-readable context, while the
13 /// `Debug` impl also prints the underlying error.
14 pub struct Context
<D
: Display
+ Send
+ Sync
+ '
static> {
18 impl<D
: Display
+ Send
+ Sync
+ '
static> Context
<D
> {
19 /// Creates a new context without an underlying error message.
20 pub fn new(context
: D
) -> Context
<D
> {
24 /// Returns a reference to the context provided with this error.
25 pub fn get_context(&self) -> &D
{
29 pub(crate) fn with_err
<E
: Fail
>(context
: D
, _
: E
) -> Context
<D
> {
34 impl<D
: Display
+ Send
+ Sync
+ '
static> Fail
for Context
<D
> { }
36 impl<D
: Display
+ Send
+ Sync
+ '
static> Debug
for Context
<D
> {
37 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
38 write
!(f
, "{}", self.context
)
42 impl<D
: Display
+ Send
+ Sync
+ '
static> Display
for Context
<D
> {
43 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
44 write
!(f
, "{}", self.context
)
50 use {Error, Backtrace}
;
52 /// An error with context around it.
54 /// The context is intended to be a human-readable, user-facing explanation for the
55 /// error that has occurred. The underlying error is not assumed to be end-user-relevant
58 /// The `Display` impl for `Context` only prints the human-readable context, while the
59 /// `Debug` impl also prints the underlying error.
60 pub struct Context
<D
: Display
+ Send
+ Sync
+ '
static> {
62 failure
: Either
<Backtrace
, Error
>,
65 impl<D
: Display
+ Send
+ Sync
+ '
static> Context
<D
> {
66 /// Creates a new context without an underlying error message.
67 pub fn new(context
: D
) -> Context
<D
> {
68 let failure
= Either
::This(Backtrace
::new());
69 Context { context, failure }
72 /// Returns a reference to the context provided with this error.
73 pub fn get_context(&self) -> &D
{
77 pub(crate) fn with_err
<E
: Into
<Error
>>(context
: D
, error
: E
) -> Context
<D
> {
78 let failure
= Either
::That(error
.into());
79 Context { context, failure }
83 impl<D
: Display
+ Send
+ Sync
+ '
static> Fail
for Context
<D
> {
84 fn cause(&self) -> Option
<&Fail
> {
85 self.failure
.as_cause()
88 fn backtrace(&self) -> Option
<&Backtrace
> {
89 Some(self.failure
.backtrace())
93 impl<D
: Display
+ Send
+ Sync
+ '
static> Debug
for Context
<D
> {
94 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
95 write
!(f
, "{:?}\n\n{}", self.failure
, self.context
)
99 impl<D
: Display
+ Send
+ Sync
+ '
static> Display
for Context
<D
> {
100 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
101 write
!(f
, "{}", self.context
)
110 impl Either
<Backtrace
, Error
> {
111 fn backtrace(&self) -> &Backtrace
{
113 Either
::This(ref backtrace
) => backtrace
,
114 Either
::That(ref error
) => error
.backtrace(),
118 fn as_cause(&self) -> Option
<&Fail
> {
120 Either
::This(_
) => None
,
121 Either
::That(ref error
) => Some(error
.as_fail())
126 impl Debug
for Either
<Backtrace
, Error
> {
127 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
129 Either
::This(ref backtrace
) => write
!(f
, "{:?}", backtrace
),
130 Either
::That(ref error
) => write
!(f
, "{:?}", error
),
136 impl<D
> From
<D
> for Context
<D
>
138 D
: Display
+ Send
+ Sync
+ '
static,
140 fn from(display
: D
) -> Context
<D
> {
141 Context
::new(display
)