Skip to main content

sword_core/error/
macros.rs

1#[macro_export]
2macro_rules! sword_error {
3    (
4        title: $title:expr,
5        reason: $reason:expr,
6        $(source: $source:expr,)?
7        fields: { $($field:ident = $value:expr),* $(,)? },
8        hints: [$hint:expr $(,)?],
9        fatal: false
10        $(,)?
11    ) => {{
12        let title = ($title).to_string();
13        let reason = ($reason).to_string();
14
15        if tracing::dispatcher::has_been_set() {
16            tracing::error!(
17                target: "sword.startup.error",
18                $(source = %$source,)?
19                reason = %reason,
20                "{}",
21                title
22            );
23
24            tracing::debug!(
25                target: "sword.startup.error",
26                $(source = %$source,)?
27                $($field = %$value,)*
28                hint = %$hint,
29                "Startup diagnostic details"
30            );
31        } else {
32            eprintln!("ERROR: {title}");
33            eprintln!("Enable tracing in config to see details");
34        }
35    }};
36
37    (
38        title: $title:expr,
39        reason: $reason:expr,
40        $(source: $source:expr,)?
41        fields: { $($field:ident = $value:expr),* $(,)? },
42        hints: [$hint:expr $(,)?]
43        $(, fatal: true)?
44        $(,)?
45    ) => {{
46        let title = ($title).to_string();
47        let reason = ($reason).to_string();
48
49        if tracing::dispatcher::has_been_set() {
50            tracing::error!(
51                target: "sword.startup.error",
52                $(source = %$source,)?
53                reason = %reason,
54                "{}",
55                title
56            );
57
58            tracing::debug!(
59                target: "sword.startup.error",
60                $(source = %$source,)?
61                $($field = %$value,)*
62                hint = %$hint,
63                "Startup diagnostic details"
64            );
65        } else {
66            eprintln!("ERROR: {title}");
67            eprintln!("Enable tracing in config to see details");
68        }
69
70        panic!("fatal sword diagnostic emitted")
71    }};
72
73    (
74        title: $title:expr,
75        reason: $reason:expr
76        $(, source: $source:expr)?
77        $(, context: { $($context_key:expr => $context_value:expr),* $(,)? })?
78        $(, extra_context: $extra_context:expr)?
79        $(, hints: [ $($hint:expr),* $(,)? ])?
80        , fatal: false
81        $(,)?
82    ) => {{
83        let mut diagnostic =
84            $crate::error::StartupDiagnostic::new(($title).to_string(), ($reason).to_string());
85
86        $(
87            diagnostic = diagnostic.with_source(($source).to_string());
88        )?
89
90        $(
91            $(
92                diagnostic = diagnostic.add_context(
93                    ($context_key).to_string(),
94                    ($context_value).to_string(),
95                );
96            )*
97        )?
98
99        $(
100            diagnostic = diagnostic.extend_context(($extra_context).into_iter());
101        )?
102
103        $(
104            $(
105                diagnostic = diagnostic.add_hint(($hint).to_string());
106            )*
107        )?
108
109        $crate::error::emit(diagnostic);
110    }};
111
112    (
113        title: $title:expr,
114        reason: $reason:expr
115        $(, source: $source:expr)?
116        $(, context: { $($context_key:expr => $context_value:expr),* $(,)? })?
117        $(, extra_context: $extra_context:expr)?
118        $(, hints: [ $($hint:expr),* $(,)? ])?
119        $(, fatal: true)?
120        $(,)?
121    ) => {{
122        let mut diagnostic =
123            $crate::error::StartupDiagnostic::new(($title).to_string(), ($reason).to_string());
124
125        $(
126            diagnostic = diagnostic.with_source(($source).to_string());
127        )?
128
129        $(
130            $(
131                diagnostic = diagnostic.add_context(
132                    ($context_key).to_string(),
133                    ($context_value).to_string(),
134                );
135            )*
136        )?
137
138        $(
139            diagnostic = diagnostic.extend_context(($extra_context).into_iter());
140        )?
141
142        $(
143            $(
144                diagnostic = diagnostic.add_hint(($hint).to_string());
145            )*
146        )?
147
148        $crate::error::emit(diagnostic);
149        panic!("fatal sword diagnostic emitted")
150    }};
151}