use crate::{PdbGuid, PdbIdentity};
use std::{error::Error as StdError, fmt, io, path::PathBuf};
#[derive(Debug)]
pub(crate) enum Error {
Io {
operation: &'static str,
path: PathBuf,
source: io::Error,
},
Msf {
reason: &'static str,
},
MsfIo {
operation: &'static str,
source: io::Error,
},
Identity {
expected: PdbIdentity,
actual: ActualIdentity,
},
Http(HttpError),
Cache {
path: PathBuf,
reason: &'static str,
},
Unavailable {
identity: PdbIdentity,
},
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct ActualIdentity {
pub(crate) guid: PdbGuid,
pub(crate) age: u32,
}
#[derive(Debug)]
pub(crate) enum HttpError {
Transport(ureq::Error),
Status(u16),
NotFound,
ContentRange(&'static str),
BodyLength,
}
impl Error {
pub(super) fn io(operation: &'static str, path: PathBuf, source: io::Error) -> Self {
Self::Io {
operation,
path,
source,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io {
operation,
path,
source,
} => write!(
f,
"PDB acquisition {operation} {}: {source}",
path.display()
),
Self::Msf { reason } => write!(f, "malformed PDB MSF7: {reason}"),
Self::MsfIo { operation, source } => {
write!(f, "PDB MSF7 {operation} failed: {source}")
}
Self::Identity { expected, actual } => write!(
f,
"PDB identity mismatch: expected {} age {}, got {} age {}",
expected.guid(),
expected.age(),
actual.guid,
actual.age
),
Self::Http(e) => write!(f, "PDB Symbol Server HTTP error: {e}"),
Self::Cache { path, reason } => write!(f, "PDB cache {}: {reason}", path.display()),
Self::Unavailable { identity } => write!(
f,
"PDB {} is unavailable offline or with Symbol Server disabled",
identity.filename()
),
}
}
}
impl fmt::Display for HttpError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Transport(e) => write!(f, "transport: {e}"),
Self::Status(s) => write!(f, "unexpected status {s}"),
Self::NotFound => f.write_str("PDB not found"),
Self::ContentRange(s) => write!(f, "invalid Content-Range: {s}"),
Self::BodyLength => {
f.write_str("response body length differs from declared range or PDB length")
}
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::Io { source, .. } | Self::MsfIo { source, .. } => Some(source),
Self::Http(HttpError::Transport(e)) => Some(e),
_ => None,
}
}
}