Refactor tor-netdoc a bit more, to use Bug right.

This commit is contained in:
Nick Mathewson 2022-02-15 10:22:38 -05:00
parent 13f39ed896
commit 6bd21d4372
1 changed files with 23 additions and 4 deletions

View File

@ -171,6 +171,9 @@ pub enum ParseErrorKind {
/// An internal error in the parser: these should never happen.
#[display(fmt = "internal error")]
Internal,
/// Invoked an API in an incorrect manner.
#[display(fmt = "bad API usage")]
BadApiUsage,
/// An entry was found with no keyword.
#[display(fmt = "no keyword for entry")]
MissingKeyword,
@ -291,9 +294,9 @@ pub(crate) enum ParseErrorSource {
/// Invalid protocol versions.
#[error("Protocol versions")]
Protovers(#[from] tor_protover::ParseError),
/// Internal error.
#[error("Internal error")]
Internal(#[from] tor_error::Bug),
/// A bug in our programming, or somebody else's.
#[error("Internal error or bug")]
Bug(#[from] tor_error::Bug),
}
impl ParseErrorKind {
@ -457,7 +460,23 @@ declare_into! { tor_bytes::Error => Undecodable }
declare_into! { std::num::ParseIntError => BadArgument }
declare_into! { std::net::AddrParseError => BadArgument }
declare_into! { PolicyError => BadPolicy }
declare_into! { tor_error::Bug => Internal }
impl From<tor_error::Bug> for Error {
fn from(err: tor_error::Bug) -> Self {
use tor_error::HasKind;
let kind = match err.kind() {
tor_error::ErrorKind::BadApiUsage => ParseErrorKind::BadApiUsage,
_ => ParseErrorKind::Internal,
};
Error {
kind,
msg: None,
pos: None,
source: Some(err.into()),
}
}
}
/// An error that occurs while trying to construct a network document.
#[derive(Clone, Debug, Error)]