forked from coop-cloud-mirrors/godotenv
Merge pull request #22 from mmilata/dont-swallow-errors
Improve error handling
This commit is contained in:
commit
726cc8b906
2
fixtures/invalid1.env
Normal file
2
fixtures/invalid1.env
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
INVALID LINE
|
||||||
|
foo=bar
|
12
godotenv.go
12
godotenv.go
@ -143,13 +143,19 @@ func readFile(filename string) (envMap map[string]string, err error) {
|
|||||||
lines = append(lines, scanner.Text())
|
lines = append(lines, scanner.Text())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err = scanner.Err(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for _, fullLine := range lines {
|
for _, fullLine := range lines {
|
||||||
if !isIgnoredLine(fullLine) {
|
if !isIgnoredLine(fullLine) {
|
||||||
key, value, err := parseLine(fullLine)
|
var key, value string
|
||||||
|
key, value, err = parseLine(fullLine)
|
||||||
|
|
||||||
if err == nil {
|
if err != nil {
|
||||||
envMap[key] = value
|
return
|
||||||
}
|
}
|
||||||
|
envMap[key] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -269,3 +269,20 @@ func TestLinesToIgnore(t *testing.T) {
|
|||||||
t.Error("ignoring a perfectly valid line to parse")
|
t.Error("ignoring a perfectly valid line to parse")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestErrorReadDirectory(t *testing.T) {
|
||||||
|
envFileName := "fixtures/"
|
||||||
|
envMap, err := Read(envFileName)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected error, got %v", envMap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrorParsing(t *testing.T) {
|
||||||
|
envFileName := "fixtures/invalid1.env"
|
||||||
|
envMap, err := Read(envFileName)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected error, got %v", envMap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user