Skip to content

Commit 1808ef8

Browse files
authored
Merge pull request #52 from vertti/improve-error-messages
Improve error messages to show actual values
2 parents 6bd924a + 18d7014 commit 1808ef8

3 files changed

Lines changed: 23 additions & 17 deletions

File tree

pkg/envcheck/check.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ func (c *Check) Run() check.Result {
7676
return result.Failf("invalid regex pattern: %v", err)
7777
}
7878
if !re.MatchString(value) {
79-
return result.Failf("value does not match pattern %q", c.Match)
79+
return result.Failf("%q does not match pattern %q", c.formatValue(value), c.Match)
8080
}
8181
}
8282

8383
// --exact: exact value match
8484
if c.Exact != "" && value != c.Exact {
85-
return result.Failf("value does not equal %q", c.Exact)
85+
return result.Failf("%q does not equal %q", c.formatValue(value), c.Exact)
8686
}
8787

8888
// --one-of: value must be one of the allowed values
@@ -94,23 +94,23 @@ func (c *Check) Run() check.Result {
9494

9595
// --starts-with: value must start with prefix
9696
if c.StartsWith != "" && !strings.HasPrefix(value, c.StartsWith) {
97-
return result.Failf("value does not start with %q", c.StartsWith)
97+
return result.Failf("%q does not start with %q", c.formatValue(value), c.StartsWith)
9898
}
9999

100100
// --ends-with: value must end with suffix
101101
if c.EndsWith != "" && !strings.HasSuffix(value, c.EndsWith) {
102-
return result.Failf("value does not end with %q", c.EndsWith)
102+
return result.Failf("%q does not end with %q", c.formatValue(value), c.EndsWith)
103103
}
104104

105105
// --contains: value must contain substring
106106
if c.Contains != "" && !strings.Contains(value, c.Contains) {
107-
return result.Failf("value does not contain %q", c.Contains)
107+
return result.Failf("%q does not contain %q", c.formatValue(value), c.Contains)
108108
}
109109

110110
// --is-numeric: value must be a valid number
111111
if c.IsNumeric {
112112
if _, err := strconv.ParseFloat(value, 64); err != nil {
113-
return result.Fail("value is not numeric", errors.New("value is not numeric"))
113+
return result.Failf("%q is not numeric", c.formatValue(value))
114114
}
115115
}
116116

@@ -133,7 +133,7 @@ func (c *Check) Run() check.Result {
133133
// --is-json: value must be valid JSON
134134
if c.IsJSON {
135135
if !gjson.Valid(value) {
136-
return result.Fail("value is not valid JSON", errors.New("invalid JSON"))
136+
return result.Failf("%q is not valid JSON", c.formatValue(value))
137137
}
138138
}
139139

pkg/envcheck/check_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestEnvCheck_Run(t *testing.T) {
122122
Getter: &mockEnvGetter{Vars: map[string]string{"DATABASE_URL": "mysql://localhost:3306/db"}},
123123
},
124124
wantStatus: check.StatusFail,
125-
wantDetail: `value does not match pattern "^postgres://"`,
125+
wantDetail: `"mysql://localhost:3306/db" does not match pattern "^postgres://"`,
126126
},
127127

128128
// --exact flag tests
@@ -143,7 +143,7 @@ func TestEnvCheck_Run(t *testing.T) {
143143
Getter: &mockEnvGetter{Vars: map[string]string{"NODE_ENV": "development"}},
144144
},
145145
wantStatus: check.StatusFail,
146-
wantDetail: `value does not equal "production"`,
146+
wantDetail: `"development" does not equal "production"`,
147147
},
148148

149149
// --one-of flag tests
@@ -230,7 +230,7 @@ func TestEnvCheck_Run(t *testing.T) {
230230
Getter: &mockEnvGetter{Vars: map[string]string{"PATH": "/opt/bin"}},
231231
},
232232
wantStatus: check.StatusFail,
233-
wantDetail: `value does not start with "/usr"`,
233+
wantDetail: `"/opt/bin" does not start with "/usr"`,
234234
},
235235

236236
// --ends-with flag tests
@@ -251,7 +251,7 @@ func TestEnvCheck_Run(t *testing.T) {
251251
Getter: &mockEnvGetter{Vars: map[string]string{"CONFIG": "/app/config.json"}},
252252
},
253253
wantStatus: check.StatusFail,
254-
wantDetail: `value does not end with ".yaml"`,
254+
wantDetail: `"/app/config.json" does not end with ".yaml"`,
255255
},
256256

257257
// --contains flag tests
@@ -272,7 +272,7 @@ func TestEnvCheck_Run(t *testing.T) {
272272
Getter: &mockEnvGetter{Vars: map[string]string{"URL": "https://other.com/api"}},
273273
},
274274
wantStatus: check.StatusFail,
275-
wantDetail: `value does not contain "example"`,
275+
wantDetail: `"https://other.com/api" does not contain "example"`,
276276
},
277277

278278
// --is-numeric flag tests
@@ -302,7 +302,7 @@ func TestEnvCheck_Run(t *testing.T) {
302302
Getter: &mockEnvGetter{Vars: map[string]string{"PORT": "not-a-number"}},
303303
},
304304
wantStatus: check.StatusFail,
305-
wantDetail: "value is not numeric",
305+
wantDetail: `"not-a-number" is not numeric`,
306306
},
307307

308308
// --min-len flag tests
@@ -505,7 +505,7 @@ func TestEnvCheck_Run(t *testing.T) {
505505
Getter: &mockEnvGetter{Vars: map[string]string{"BAD": `{key: value}`}},
506506
},
507507
wantStatus: check.StatusFail,
508-
wantDetail: "value is not valid JSON",
508+
wantDetail: `"{key: value}" is not valid JSON`,
509509
},
510510

511511
// --is-bool flag tests

pkg/jsoncheck/check.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package jsoncheck
22

33
import (
4-
"errors"
4+
"encoding/json"
55

66
"github.com/tidwall/gjson"
77

@@ -30,10 +30,16 @@ func (c *Check) Run() check.Result {
3030
return result.Failf("failed to read file: %v", err)
3131
}
3232

33-
// Validate JSON syntax
33+
// Validate JSON syntax using encoding/json for detailed errors
3434
jsonStr := string(content)
3535
if !gjson.Valid(jsonStr) {
36-
return result.Fail("invalid JSON", errors.New("invalid JSON syntax"))
36+
// Use encoding/json to get detailed parse error
37+
var v any
38+
if err := json.Unmarshal(content, &v); err != nil {
39+
return result.Failf("invalid JSON: %v", err)
40+
}
41+
// Fallback if gjson says invalid but encoding/json passes (shouldn't happen)
42+
return result.Failf("invalid JSON")
3743
}
3844

3945
result.AddDetail("syntax: valid")

0 commit comments

Comments
 (0)