Skip to content

Capability to check for .env file not found error and other possible errors #101

@LeoBorai

Description

@LeoBorai

Description

When running gotdotenv.Load:

func Load(filenames ...string) (err error) {

Theres no way to check for a "FileNotFound" error.
godotenv panics as follows:

➜  server git:(master) ✗ go run main.go
panic: open .env: no such file or directory

goroutine 1 [running]:
main.main()
	.../server/main.go:12 +0xa9
exit status 2

Possible Solution

A possible solution would be to enumerate errors:

var (
        // EnvFileNotFound returns a "env file not found". Happens when godotenv is not able to find a .env file in the cwd
	EnvFileNotFound = errors.New("env file not found")
)

Then provide a ErrorIs like function, for example:

	err := godotenv.Load()

	if err != nil {
		if godotenv.ErrorIs(godotenv.EnvFileNotFound, err) {
                    // Do something
                } else {
		    return nil, err
                }
	}

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions