Skip to content

Commit 7304fec

Browse files
authored
fix: make a simpler connect error (#60)
* fix: make a simpler connect error * chore: more bounds * fix: derive test
1 parent 2e1e018 commit 7304fec

File tree

3 files changed

+78
-10
lines changed

3 files changed

+78
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ name = "init4-bin-base"
44
description = "Internal utilities for binaries produced by the init4 team"
55
keywords = ["init4", "bin", "base"]
66

7-
version = "0.9.1"
7+
version = "0.9.2"
88
edition = "2021"
99
rust-version = "1.81"
1010
authors = ["init4", "James Prestwich"]

src/utils/from_env.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use signet_constants::{
33
SignetSystemConstants,
44
};
55
use std::{convert::Infallible, env::VarError, num::ParseIntError, str::FromStr};
6+
use tracing_core::metadata::ParseLevelError;
67

78
/// The `derive(FromEnv)` macro.
89
///
@@ -278,7 +279,7 @@ pub fn parse_env_if_present<T: FromStr>(env_var: &str) -> Result<T, FromEnvErr<T
278279
///
279280
pub trait FromEnv: core::fmt::Debug + Sized + 'static {
280281
/// Error type produced when loading from the environment.
281-
type Error: core::error::Error + Clone;
282+
type Error: core::error::Error + Clone + PartialEq + Eq;
282283

283284
/// Get the required environment variable names for this type.
284285
///
@@ -453,7 +454,7 @@ where
453454
/// ```
454455
pub trait FromEnvVar: core::fmt::Debug + Sized + 'static {
455456
/// Error type produced when parsing the primitive.
456-
type Error: core::error::Error;
457+
type Error: core::error::Error + Clone + PartialEq + Eq;
457458

458459
/// Load the primitive from the environment at the given variable.
459460
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>;
@@ -613,7 +614,6 @@ impl_for_parseable!(
613614
i128,
614615
isize,
615616
url::Url,
616-
tracing::Level,
617617
SignetConstants,
618618
SignetEnvironmentConstants,
619619
SignetSystemConstants,
@@ -646,6 +646,28 @@ impl FromEnvVar for bool {
646646
}
647647
}
648648

649+
/// Error type for parsing tracing levels from the environment.
650+
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
651+
#[error("failed to parse tracing level from environment variable")]
652+
pub struct LevelParseError;
653+
654+
impl From<ParseLevelError> for LevelParseError {
655+
fn from(_: ParseLevelError) -> Self {
656+
LevelParseError
657+
}
658+
}
659+
660+
impl FromEnvVar for tracing::Level {
661+
type Error = LevelParseError;
662+
663+
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>> {
664+
let s: String = std::env::var(env_var).map_err(|e| FromEnvErr::env_err(env_var, e))?;
665+
s.parse()
666+
.map_err(Into::into)
667+
.map_err(FromEnvErr::parse_error)
668+
}
669+
}
670+
649671
#[cfg(test)]
650672
mod test {
651673
use std::{borrow::Cow, time::Duration};

src/utils/provider.rs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,43 @@ use alloy::{
88
},
99
};
1010

11+
/// Errors when connecting a provider
12+
#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)]
13+
pub enum ProviderConnectError {
14+
/// Pubsub is not available for the configured transport
15+
#[error("pubsub is not available for the configured transport")]
16+
PubsubUnavailable,
17+
/// Custom error message
18+
#[error("{0}")]
19+
Custom(String),
20+
}
21+
22+
impl From<TransportErrorKind> for ProviderConnectError {
23+
fn from(err: TransportErrorKind) -> Self {
24+
match err {
25+
TransportErrorKind::Custom(err) => ProviderConnectError::Custom(err.to_string()),
26+
TransportErrorKind::PubsubUnavailable => ProviderConnectError::PubsubUnavailable,
27+
_ => panic!("Unexpected TransportErrorKind variant: {err:?}"),
28+
}
29+
}
30+
}
31+
32+
impl From<TransportError> for ProviderConnectError {
33+
fn from(err: TransportError) -> Self {
34+
match err {
35+
TransportError::Transport(e) => e.into(),
36+
_ => panic!("Unexpected TransportError variant: {err:?}"),
37+
}
38+
}
39+
}
40+
1141
impl FromEnvVar for BuiltInConnectionString {
12-
type Error = TransportError;
42+
type Error = ProviderConnectError;
1343

1444
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>> {
1545
let conn_str = String::from_env_var(env_var).map_err(FromEnvErr::infallible_into)?;
16-
conn_str.parse().map_err(Into::into)
46+
let built_in = conn_str.parse().map_err(ProviderConnectError::from)?;
47+
Ok(built_in)
1748
}
1849
}
1950

@@ -41,7 +72,7 @@ impl ProviderConfig {
4172
}
4273

4374
impl FromEnvVar for ProviderConfig {
44-
type Error = TransportError;
75+
type Error = ProviderConnectError;
4576

4677
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>> {
4778
let connection_string = BuiltInConnectionString::from_env_var(env_var)?;
@@ -81,21 +112,21 @@ impl PubSubConfig {
81112
}
82113

83114
impl TryFrom<BuiltInConnectionString> for PubSubConfig {
84-
type Error = TransportError;
115+
type Error = ProviderConnectError;
85116

86117
fn try_from(connection_string: BuiltInConnectionString) -> Result<Self, Self::Error> {
87118
if !matches!(
88119
connection_string,
89120
BuiltInConnectionString::Ws(_, _) | BuiltInConnectionString::Ipc(_)
90121
) {
91-
return Err(TransportErrorKind::pubsub_unavailable());
122+
return Err(ProviderConnectError::PubsubUnavailable);
92123
}
93124
Ok(Self { connection_string })
94125
}
95126
}
96127

97128
impl FromEnvVar for PubSubConfig {
98-
type Error = TransportError;
129+
type Error = ProviderConnectError;
99130

100131
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>> {
101132
let cs = BuiltInConnectionString::from_env_var(env_var)?;
@@ -137,3 +168,18 @@ impl PubSubConnect for PubSubConfig {
137168
}
138169
}
139170
}
171+
172+
#[cfg(test)]
173+
mod test {
174+
use super::*;
175+
use crate::utils::from_env::FromEnv;
176+
177+
#[derive(FromEnv, Debug, Clone, PartialEq, Eq)]
178+
#[from_env(crate)]
179+
struct CompileCheck {
180+
#[from_env(var = "COOL_DUDE", desc = "provider")]
181+
cool_dude: ProviderConfig,
182+
#[from_env(var = "COOL_DUDE2", desc = "provider2")]
183+
cool_dude2: PubSubConfig,
184+
}
185+
}

0 commit comments

Comments
 (0)