tachyon_i2p/error.rs
1//! Error type returned by this crate's fallible operations.
2
3use std::fmt;
4
5/// Errors that can occur while starting the router, creating a destination, or using a stream.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum I2pError {
9 /// Another [`I2pRouter`](crate::I2pRouter) is still running in this process. Once the last
10 /// clone of it has been dropped, a fresh `start` succeeds.
11 ///
12 /// The exception is a router whose start panicked inside libi2pd: the globals are then in an
13 /// unknown state, and every later `start` in that process returns this error rather than
14 /// re-initializing over them.
15 AlreadyRunning,
16 /// The application name contained an interior NUL byte and can't cross the C ABI.
17 InvalidAppName,
18 /// The blocking worker task backing an async call panicked -- a bug in this crate or libi2pd.
19 WorkerPanicked,
20 /// libi2pd failed to create the destination. Its API reports no further detail.
21 DestinationCreationFailed,
22 /// libi2pd failed to generate a new destination keypair.
23 KeyGenerationFailed,
24 /// [`Destination::connect`](crate::Destination::connect) timed out or failed -- most often
25 /// because a tunnel had to be built over the live network first, which takes seconds to
26 /// minutes.
27 ConnectFailed,
28 /// The destination was dropped while an [`accept`](crate::Destination::accept) was pending.
29 DestinationClosed,
30 /// An I/O error occurred reading/writing a persistent keys file.
31 Io(std::io::Error),
32}
33
34impl fmt::Display for I2pError {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 match self {
37 Self::AlreadyRunning => f.write_str("an I2pRouter is already running in this process"),
38 Self::InvalidAppName => f.write_str("application name contains an interior NUL byte"),
39 Self::WorkerPanicked => f.write_str("internal blocking worker task panicked"),
40 Self::DestinationCreationFailed => {
41 f.write_str("libi2pd failed to create the destination")
42 }
43 Self::KeyGenerationFailed => {
44 f.write_str("libi2pd failed to generate a destination keypair")
45 }
46 Self::ConnectFailed => f.write_str("failed to connect to the remote I2P destination"),
47 Self::DestinationClosed => f.write_str("the destination was closed"),
48 Self::Io(e) => write!(f, "I/O error: {e}"),
49 }
50 }
51}
52
53impl std::error::Error for I2pError {
54 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
55 match self {
56 Self::Io(e) => Some(e),
57 _ => None,
58 }
59 }
60}