Skip to content

Commit

Permalink
better error
Browse files Browse the repository at this point in the history
  • Loading branch information
meowjesty committed Oct 15, 2024
1 parent 11da23c commit 0ff4221
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
25 changes: 14 additions & 11 deletions mirrord/agent/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,24 @@ impl DnsApi {

/// Returns the result of the oldest outstanding DNS request issued with this struct (see
/// [`Self::make_request`]).
#[tracing::instrument(level = Level::TRACE, skip(self), ret, err)]
pub(crate) async fn recv(&mut self) -> Result<GetAddrInfoResponse, AgentError> {
let Some(response) = self.responses.next().await else {
return future::pending().await;
};

match response? {
Ok(lookup) => Ok(GetAddrInfoResponse(Ok(lookup))),
Err(ResponseError::DnsLookup(err)) => {
Ok(GetAddrInfoResponse(Err(ResponseError::DnsLookup(err))))
}
Err(..) => Ok(GetAddrInfoResponse(Err(ResponseError::DnsLookup(
DnsLookupError {
kind: ResolveErrorKindInternal::Unknown,
},
)))),
}
tracing::info!(?response);

let response = response?.map_err(|fail| match fail {
ResponseError::RemoteIO(remote_ioerror) => ResponseError::DnsLookup(DnsLookupError {
kind: remote_ioerror.kind.into(),
}),
fail @ ResponseError::DnsLookup(_) => fail,
_ => ResponseError::DnsLookup(DnsLookupError {
kind: ResolveErrorKindInternal::Unknown,
}),
});

Ok(GetAddrInfoResponse(response))
}
}
2 changes: 1 addition & 1 deletion mirrord/layer/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub(crate) enum HookError {
/// handling [`ProxyToLayerMessage`](mirrord_intproxy_protocol::ProxyToLayerMessage).
#[derive(Error, Debug)]
pub(crate) enum LayerError {
#[error("mirrord-layer: Failed while getting a response!")]
#[error(transparent)]
ResponseError(#[from] ResponseError),

#[error("mirrord-layer: Frida failed with `{0}`!")]
Expand Down
35 changes: 33 additions & 2 deletions mirrord/protocol/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub struct RemoteIOError {
/// Our internal version of Rust's `std::io::Error` that can be passed between mirrord-layer and
/// mirrord-agent.
#[derive(Encode, Decode, Debug, PartialEq, Clone, Eq, Error)]
#[error("Failed performing `getaddrinfo` with {kind:?}!")]
#[error("Failed performing `getaddrinfo` with: {kind}!")]
pub struct DnsLookupError {
pub kind: ResolveErrorKindInternal,
}
Expand Down Expand Up @@ -239,6 +239,28 @@ pub enum ResolveErrorKindInternal {
PermissionDenied,
}

impl core::fmt::Display for ResolveErrorKindInternal {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let pretty_fail = match self {
ResolveErrorKindInternal::Message(message) => message.to_string(),
ResolveErrorKindInternal::NoConnections => "no connections".to_string(),
ResolveErrorKindInternal::NoRecordsFound(records) => records.to_string(),
ResolveErrorKindInternal::Proto => "protocol".to_string(),
ResolveErrorKindInternal::Timeout => "timeout".to_string(),
ResolveErrorKindInternal::Unknown => "unknown".to_string(),
ResolveErrorKindInternal::NotFound => "the agent could not find a DNS related \
file, such as `/etc/resolv.conf` or `/etc/hosts`"
.to_string(),
ResolveErrorKindInternal::PermissionDenied => "the agent lacks sufficient \
permissions to open or read a DNS related file, such as \
`/etc/resolv.conf` or `/etc/hosts`"
.to_string(),
};

write!(f, "{pretty_fail}")
}
}

impl From<io::ErrorKind> for ErrorKindInternal {
fn from(error_kind: io::ErrorKind) -> Self {
match error_kind {
Expand Down Expand Up @@ -289,7 +311,6 @@ impl From<io::ErrorKind> for ErrorKindInternal {

impl From<ResolveErrorKind> for ResolveErrorKindInternal {
fn from(error_kind: ResolveErrorKind) -> Self {
println!("{error_kind:?}");
match error_kind {
ResolveErrorKind::Message(message) => {
ResolveErrorKindInternal::Message(message.to_string())
Expand All @@ -316,3 +337,13 @@ impl From<ResolveErrorKind> for ResolveErrorKindInternal {
}
}
}

impl From<ErrorKindInternal> for ResolveErrorKindInternal {
fn from(kind: ErrorKindInternal) -> Self {
match kind {
ErrorKindInternal::NotFound => Self::NotFound,
ErrorKindInternal::PermissionDenied => Self::PermissionDenied,
_ => Self::Unknown,
}
}
}

0 comments on commit 0ff4221

Please sign in to comment.