Skip to content

Commit

Permalink
chore: make environment variable error nicer (#9353)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Nov 19, 2024
1 parent 19249c3 commit 9b49082
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
18 changes: 13 additions & 5 deletions crates/config/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@ impl UnresolvedEnvVarError {
pub fn try_resolve(&self) -> Result<String, Self> {
interpolate(&self.unresolved)
}

fn is_simple(&self) -> bool {
RE_PLACEHOLDER.captures_iter(&self.unresolved).count() <= 1
}
}

impl fmt::Display for UnresolvedEnvVarError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Failed to resolve env var `{}` in `{}`: {}",
self.var, self.unresolved, self.source
)
write!(f, "environment variable `{}` ", self.var)?;
f.write_str(match self.source {
VarError::NotPresent => "not found",
VarError::NotUnicode(_) => "is not valid unicode",
})?;
if !self.is_simple() {
write!(f, " in `{}`", self.unresolved)?;
}
Ok(())
}
}

Expand Down
4 changes: 1 addition & 3 deletions testdata/default/cheats/RpcUrls.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ contract RpcUrlTest is DSTest {
// can set env and return correct url
function testCanSetAndGetURLAndAllUrls() public {
// this will fail because alias is not set
vm._expectCheatcodeRevert(
"Failed to resolve env var `RPC_ENV_ALIAS` in `${RPC_ENV_ALIAS}`: environment variable not found"
);
vm._expectCheatcodeRevert("environment variable `RPC_ENV_ALIAS` not found");
string[2][] memory _urls = vm.rpcUrls();

string memory url = vm.rpcUrl("mainnet");
Expand Down

0 comments on commit 9b49082

Please sign in to comment.