Skip to content

Instantly share code, notes, and snippets.

@detro
Created January 8, 2019 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save detro/16f95ae5cff5fe818fc8a654eaba7732 to your computer and use it in GitHub Desktop.
Save detro/16f95ae5cff5fe818fc8a654eaba7732 to your computer and use it in GitHub Desktop.
Is there a better way to write this?
use http::Error as HttpError;
use std::{fmt, error};
type Result<T> = std::result::Result<T, ResolutionError>;
/// A type of `Error` emitted by `Resolver`
///
/// It contains a description and an optional `HttpError` that might have caused it
#[derive(Debug)]
pub struct ResolutionError {
desc: &'static str,
src: Option<HttpError>,
}
impl fmt::Display for ResolutionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ResolutionError: {}", self.desc)
}
}
impl error::Error for ResolutionError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match &self.src { //< I find this match especially annoying and puzzling, but I couldn't find a better way
Some(src) => Some(src),
None => None
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment