Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit cb80278

Browse files
authored
Merge pull request #1 from golemfactory/use-error-source-fixed
Use error source fixed
2 parents dcce9c6 + 0d69163 commit cb80278

File tree

1 file changed

+75
-34
lines changed

1 file changed

+75
-34
lines changed

src/error_chain.rs

Lines changed: 75 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,72 @@
1+
#[doc(hidden)]
2+
#[macro_export]
3+
#[cfg(not(has_error_source))]
4+
macro_rules! impl_error_chain_cause_or_source {
5+
(
6+
types {
7+
$error_kind_name:ident
8+
}
9+
10+
foreign_links {
11+
$( $foreign_link_variant:ident ( $foreign_link_error_path:path )
12+
$( #[$meta_foreign_links:meta] )*; )*
13+
}
14+
) => {
15+
#[allow(unknown_lints, renamed_and_removed_lints, unused_doc_comment, unused_doc_comments)]
16+
fn cause(&self) -> Option<&::std::error::Error> {
17+
match self.1.next_error {
18+
Some(ref c) => Some(&**c),
19+
None => {
20+
match self.0 {
21+
$(
22+
$(#[$meta_foreign_links])*
23+
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
24+
foreign_err.cause()
25+
}
26+
) *
27+
_ => None
28+
}
29+
}
30+
}
31+
}
32+
};
33+
}
34+
35+
#[cfg(has_error_source)]
36+
#[doc(hidden)]
37+
#[macro_export]
38+
macro_rules! impl_error_chain_cause_or_source {
39+
(
40+
types {
41+
$error_kind_name:ident
42+
}
43+
44+
foreign_links {
45+
$( $foreign_link_variant:ident ( $foreign_link_error_path:path )
46+
$( #[$meta_foreign_links:meta] )*; )*
47+
}
48+
) => {
49+
50+
#[allow(unknown_lints, renamed_and_removed_lints, unused_doc_comment, unused_doc_comments)]
51+
fn source(&self) -> Option<&(std::error::Error + 'static)> {
52+
match self.1.next_error {
53+
Some(ref c) => Some(&**c),
54+
None => {
55+
match self.0 {
56+
$(
57+
$(#[$meta_foreign_links])*
58+
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
59+
foreign_err.source()
60+
}
61+
) *
62+
_ => None
63+
}
64+
}
65+
}
66+
}
67+
};
68+
}
69+
170
/// Prefer to use `error_chain` instead of this macro.
271
#[doc(hidden)]
372
#[macro_export]
@@ -179,41 +248,13 @@ macro_rules! impl_error_chain_processed {
179248
self.description()
180249
}
181250

182-
#[cfg(not(has_error_source))]
183-
#[allow(unknown_lints, renamed_and_removed_lints, unused_doc_comment, unused_doc_comments)]
184-
fn cause(&self) -> Option<&::std::error::Error> {
185-
match self.1.next_error {
186-
Some(ref c) => Some(&**c),
187-
None => {
188-
match self.0 {
189-
$(
190-
$(#[$meta_foreign_links])*
191-
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
192-
foreign_err.cause()
193-
}
194-
) *
195-
_ => None
196-
}
197-
}
251+
impl_error_chain_cause_or_source!{
252+
types {
253+
$error_kind_name
198254
}
199-
}
200-
201-
#[cfg(has_error_source)]
202-
#[allow(unknown_lints, renamed_and_removed_lints, unused_doc_comment, unused_doc_comments)]
203-
fn source(&self) -> Option<&(std::error::Error + 'static)> {
204-
match self.1.next_error {
205-
Some(ref c) => Some(&**c),
206-
None => {
207-
match self.0 {
208-
$(
209-
$(#[$meta_foreign_links])*
210-
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
211-
foreign_err.source()
212-
}
213-
) *
214-
_ => None
215-
}
216-
}
255+
foreign_links {
256+
$( $foreign_link_variant ( $foreign_link_error_path )
257+
$( #[$meta_foreign_links] )*; )*
217258
}
218259
}
219260
}

0 commit comments

Comments
 (0)