Skip to content

Commit

Permalink
error: wrap io::Error in Arc for clone
Browse files Browse the repository at this point in the history
`ProtoErrorKind` is `Clone`, but the `Io` variant holding `io:Error`
runs into trouble with this: since the error can't be cloned we have to
reconstruct it and this is a lossy process: resulting in a "simple"
`io::Error` that only holds the error type from the parent it was cloned
from. This loses important details like the underlying error
source/message.

This commit changes `ProtoErrorKind::Io` to hold `Arc<io::Error>>`
instead. This makes implementing `Clone` trivial - we clone the arc
- and no error information is lost.
  • Loading branch information
cpu authored and djc committed Apr 14, 2024
1 parent 6c2a1e2 commit 2e84c11
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions crates/proto/src/error.rs
Expand Up @@ -10,6 +10,7 @@
#![deny(missing_docs)]

use std::cmp::Ordering;
use std::sync::Arc;
use std::{fmt, io, sync};

#[cfg(feature = "backtrace")]
Expand Down Expand Up @@ -233,7 +234,7 @@ pub enum ProtoErrorKind {
// foreign
/// An error got returned from IO
#[error("io error: {0}")]
Io(io::Error),
Io(Arc<io::Error>),

/// Any sync poised error
#[error("lock poisoned error")]
Expand Down Expand Up @@ -554,7 +555,7 @@ impl From<io::Error> for ProtoErrorKind {
fn from(e: io::Error) -> Self {
match e.kind() {
io::ErrorKind::TimedOut => Self::Timeout,
_ => Self::Io(e),
_ => Self::Io(e.into()),
}
}
}
Expand Down Expand Up @@ -641,11 +642,7 @@ impl Clone for ProtoErrorKind {
UnrecognizedLabelCode(value) => UnrecognizedLabelCode(value),
UnrecognizedNsec3Flags(flags) => UnrecognizedNsec3Flags(flags),
UnrecognizedCsyncFlags(flags) => UnrecognizedCsyncFlags(flags),
Io(ref e) => Io(if let Some(raw) = e.raw_os_error() {
io::Error::from_raw_os_error(raw)
} else {
io::Error::from(e.kind())
}),
Io(ref e) => Io(e.clone()),
Poisoned => Poisoned,
Ring(ref _e) => Ring(Unspecified),
SSL(ref e) => Msg(format!("there was an SSL error: {e}")),
Expand Down

0 comments on commit 2e84c11

Please sign in to comment.