GithubHelp home page GithubHelp logo

Comments (1)

Mythra avatar Mythra commented on July 24, 2024 1

FWIW this is currently not possible, and while there is work ongoing in the meantime if someone runs into a similar error I got around this by not returning a Report (in the initial error that gets Into<Box<std::errror::Error>>), but instead returning a type I wrote which carries the report through without losing the context.

The type is the below:

pub struct EyreSpyASmuggler {
	inner: Report,
}

impl EyreSpyASmuggler {
	/// Construct a new temporary error holder.
	pub fn new(reportable: Report) -> Self {
		Self { inner: reportable }
	}

	/// Consume an error holder to get the report back out of it.
	pub fn consume(self) -> Report {
		self.inner
	}
}

impl std::fmt::Display for EyreSpyASmuggler {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "{}", self.inner)
	}
}

impl std::fmt::Debug for EyreSpyASmuggler {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "{:?}", self.inner)
	}
}

impl std::error::Error for EyreSpyASmuggler {
	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
		None
	}
}

This prints out exactly like a report would incase someone forgets to unwrap it, or use it directly. However in the case mentioned above I can just consume it, and use it like so. The code originally producing the error looks like this:

let resp = other_conn.call(req).await;
if let Err(resp_err) = resp {
  return Err(EyreSpyASmuggler::new(tcp_stream_to_eyre(resp_err)));
}
Ok(EtherStream::Tcp(resp.unwrap()))

Then when handling it, to get the report back I simply downcast, and consume:

if cause.is::<EyreSpyASmuggler>() {
  let as_report: Report = cause.downcast::<EyreSpyASmuggler>().unwrap().consume();
  // ... Do stuff with report.
} else {
  eyre!("Unknown Cause!")
}

from color-eyre.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.