From c712eca76f471172144eac2ec7974a6c466f97da Mon Sep 17 00:00:00 2001 From: Oleg Balunenko Date: Mon, 16 Nov 2026 00:26:19 +0400 Subject: [PATCH 1/2] fix: put "invalid value" before the cause in parse errors Parse wrapped the parser error first and ErrInvalidValue last, so the message read backwards: strconv.ParseInt: parsing "x": invalid syntax: invalid value Swap the operands so the message goes from the general error to its cause, as Go error chains usually do: invalid value: strconv.ParseInt: parsing "x": invalid syntax Only the text changes: errors.Is still matches both ErrInvalidValue and the cause. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- getenv_example_test.go | 2 +- internal/parse.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7597a7a6..d2abd34b 100644 --- a/README.md +++ b/README.md @@ -199,7 +199,7 @@ Output: ``` [string]: golly; err: [int]: 123; err: -[int]: 0; err: failed to parse environment variable[GH_GETENV_TEST]: strconv.ParseInt: parsing "123s4": invalid syntax: invalid value +[int]: 0; err: failed to parse environment variable[GH_GETENV_TEST]: invalid value: strconv.ParseInt: parsing "123s4": invalid syntax [time.Time]: 2022-01-20 00:00:00 +0000 UTC; err: [[]float64]: [26.89 0.67]; err: [time.Duration]: 2h35m0s; err: diff --git a/getenv_example_test.go b/getenv_example_test.go index 54d0b30a..9d0b1330 100644 --- a/getenv_example_test.go +++ b/getenv_example_test.go @@ -241,7 +241,7 @@ func ExampleEnv() { // Output: // [string]: golly; err: // [int]: 123; err: - // [int]: 0; err: failed to parse environment variable[GH_GETENV_TEST]: strconv.ParseInt: parsing "123s4": invalid syntax: invalid value + // [int]: 0; err: failed to parse environment variable[GH_GETENV_TEST]: invalid value: strconv.ParseInt: parsing "123s4": invalid syntax // [time.Time]: 2022-01-20 00:00:00 +0000 UTC; err: // [[]float64]: [26.89 0.67]; err: // [time.Duration]: 2h35m0s; err: diff --git a/internal/parse.go b/internal/parse.go index eaf5ed0e..703f4f8d 100644 --- a/internal/parse.go +++ b/internal/parse.go @@ -23,7 +23,7 @@ func Parse[T EnvParsable](raw string, p Parameters) (T, error) { if err := parseValue(&res, raw, p); err != nil { var zero T - return zero, fmt.Errorf("%w: %w", err, ErrInvalidValue) + return zero, fmt.Errorf("%w: %w", ErrInvalidValue, err) } return res, nil From 9744de6c9fb5815e321a6f71b3c5073095414ed1 Mon Sep 17 00:00:00 2001 From: Oleg Balunenko Date: Mon, 16 Nov 2026 00:26:19 +0400 Subject: [PATCH 2/2] docs(option): describe Option without the removed EnvParser The EnvParser interface was removed with the parser hierarchy in #223, but the Option comment still called it "a contract for EnvParser parameters options". Describe what an option does instead. Co-Authored-By: Claude Opus 5.5 --- option/option.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/option/option.go b/option/option.go index eab81ee7..ae8dde07 100644 --- a/option/option.go +++ b/option/option.go @@ -5,7 +5,7 @@ import ( "github.com/obalunenko/getenv/internal" ) -// Option is a contract for EnvParser parameters options. +// Option configures how Env and EnvOrDefault parse the value of a variable. type Option interface { // Apply applies the option to the parameters. Apply(params *internal.Parameters)