summaryrefslogtreecommitdiffstats
path: root/vendor/eyre/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:19 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:19 +0000
commita0b8f38ab54ac451646aa00cd5e91b6c76f22a84 (patch)
treefc451898ccaf445814e26b46664d78702178101d /vendor/eyre/examples
parentAdding debian version 1.71.1+dfsg1-2. (diff)
downloadrustc-a0b8f38ab54ac451646aa00cd5e91b6c76f22a84.tar.xz
rustc-a0b8f38ab54ac451646aa00cd5e91b6c76f22a84.zip
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/eyre/examples')
-rw-r--r--vendor/eyre/examples/custom_handler.rs81
-rw-r--r--vendor/eyre/examples/usage.rs7
2 files changed, 88 insertions, 0 deletions
diff --git a/vendor/eyre/examples/custom_handler.rs b/vendor/eyre/examples/custom_handler.rs
new file mode 100644
index 000000000..a6f0ab206
--- /dev/null
+++ b/vendor/eyre/examples/custom_handler.rs
@@ -0,0 +1,81 @@
+use backtrace::Backtrace;
+use eyre::EyreHandler;
+use std::error::Error;
+use std::{fmt, iter};
+
+fn main() -> eyre::Result<()> {
+ // Install our custom eyre report hook for constructing our custom Handlers
+ install().unwrap();
+
+ // construct a report with, hopefully, our custom handler!
+ let mut report = eyre::eyre!("hello from custom error town!");
+
+ // manually set the custom msg for this report after it has been constructed
+ if let Some(handler) = report.handler_mut().downcast_mut::<Handler>() {
+ handler.custom_msg = Some("you're the best users, you know that right???");
+ }
+
+ // print that shit!!
+ Err(report)
+}
+
+// define a handler that captures backtraces unless told not to
+fn install() -> Result<(), impl Error> {
+ let capture_backtrace = std::env::var("RUST_BACKWARDS_TRACE")
+ .map(|val| val != "0")
+ .unwrap_or(true);
+
+ let hook = Hook { capture_backtrace };
+
+ eyre::set_hook(Box::new(move |e| Box::new(hook.make_handler(e))))
+}
+
+struct Hook {
+ capture_backtrace: bool,
+}
+
+impl Hook {
+ fn make_handler(&self, _error: &(dyn Error + 'static)) -> Handler {
+ let backtrace = if self.capture_backtrace {
+ Some(Backtrace::new())
+ } else {
+ None
+ };
+
+ Handler {
+ backtrace,
+ custom_msg: None,
+ }
+ }
+}
+
+struct Handler {
+ // custom configured backtrace capture
+ backtrace: Option<Backtrace>,
+ // customizable message payload associated with reports
+ custom_msg: Option<&'static str>,
+}
+
+impl EyreHandler for Handler {
+ fn debug(&self, error: &(dyn Error + 'static), f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ if f.alternate() {
+ return fmt::Debug::fmt(error, f);
+ }
+
+ let errors = iter::successors(Some(error), |error| (*error).source());
+
+ for (ind, error) in errors.enumerate() {
+ write!(f, "\n{:>4}: {}", ind, error)?;
+ }
+
+ if let Some(backtrace) = self.backtrace.as_ref() {
+ writeln!(f, "\n\nBacktrace:\n{:?}", backtrace)?;
+ }
+
+ if let Some(msg) = self.custom_msg.as_ref() {
+ writeln!(f, "\n\n{}", msg)?;
+ }
+
+ Ok(())
+ }
+}
diff --git a/vendor/eyre/examples/usage.rs b/vendor/eyre/examples/usage.rs
new file mode 100644
index 000000000..77e7811df
--- /dev/null
+++ b/vendor/eyre/examples/usage.rs
@@ -0,0 +1,7 @@
+use eyre::{eyre, Report, WrapErr};
+
+fn main() -> Result<(), Report> {
+ let e: Report = eyre!("oh no this program is just bad!");
+
+ Err(e).wrap_err("usage example successfully experienced a failure")
+}