Skip to content

Commit

Permalink
Merge pull request #22 from mmilata/dont-swallow-errors
Browse files Browse the repository at this point in the history
Improve error handling
  • Loading branch information
joho authored Dec 16, 2016
2 parents 4ed1339 + 861984c commit 726cc8b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions fixtures/invalid1.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INVALID LINE
foo=bar
12 changes: 9 additions & 3 deletions godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,19 @@ func readFile(filename string) (envMap map[string]string, err error) {
lines = append(lines, scanner.Text())
}

if err = scanner.Err(); err != nil {
return
}

for _, fullLine := range lines {
if !isIgnoredLine(fullLine) {
key, value, err := parseLine(fullLine)
var key, value string
key, value, err = parseLine(fullLine)

if err == nil {
envMap[key] = value
if err != nil {
return
}
envMap[key] = value
}
}
return
Expand Down
17 changes: 17 additions & 0 deletions godotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,20 @@ func TestLinesToIgnore(t *testing.T) {
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)
}
}

0 comments on commit 726cc8b

Please sign in to comment.