Skip to content

Commit

Permalink
Merge pull request #1 from lhopcroft123/err-msg-missing-reqd
Browse files Browse the repository at this point in the history
print the name of the env var when a required var is missing
  • Loading branch information
jared-mackey authored Nov 20, 2023
2 parents f90df63 + 51bb5dc commit 003c58d
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cleanenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,18 @@ func readEnvVars(cfg interface{}, update bool) error {
}

if rawValue == nil && meta.required && meta.isFieldValueZero() {
if len(meta.envList) == 0 {
return fmt.Errorf("env list for field %q is empty but is set as required", meta.fieldName)
}
if len(meta.envList) == 1 {
return fmt.Errorf(
"environment variable %q is required but the value is not provided",
meta.envList[0],
)
}
return fmt.Errorf(
"field %q is required but the value is not provided",
meta.fieldName,
"environment variable %q (aliases=%v) is required but the value is not provided",
meta.envList[0], meta.envList[1:],
)
}

Expand Down

0 comments on commit 003c58d

Please sign in to comment.