Skip to content

Commit 9b623c2

Browse files
committed
using valid env var with incorrect type function gives relevant panic msg
1 parent c01fb89 commit 9b623c2

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

configurator.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Configurator interface {
2323
func GetEnvString(config Configurator, field string) string {
2424
defer func() {
2525
if r := recover(); r != nil {
26-
log.Panicf("configurator has no field named '%s'", field)
26+
log.Panicf("configurator has no string field named '%s'", field)
2727
}
2828
}()
2929
value, exists := os.LookupEnv(field)
@@ -38,6 +38,9 @@ func GetEnvString(config Configurator, field string) string {
3838
func GetEnvBool(config Configurator, field string) bool {
3939
defer func() {
4040
if r := recover(); r != nil {
41+
if reflect.ValueOf(config).FieldByName(field).IsValid() {
42+
log.Panicf("configurator field '%s' is not a bool", field)
43+
}
4144
log.Panicf("configurator has no field named '%s'", field)
4245
}
4346
}()
@@ -53,6 +56,9 @@ func GetEnvBool(config Configurator, field string) bool {
5356
func GetEnvInt(config Configurator, field string) int {
5457
defer func() {
5558
if r := recover(); r != nil {
59+
if reflect.ValueOf(config).FieldByName(field).IsValid() {
60+
log.Panicf("configurator field '%s' is not an int", field)
61+
}
5662
log.Panicf("configurator has no field named '%s'", field)
5763
}
5864
}()
@@ -68,6 +74,9 @@ func GetEnvInt(config Configurator, field string) int {
6874
func GetEnvFloat(config Configurator, field string) float64 {
6975
defer func() {
7076
if r := recover(); r != nil {
77+
if reflect.ValueOf(config).FieldByName(field).IsValid() {
78+
log.Panicf("configurator field '%s' is not a float", field)
79+
}
7180
log.Panicf("configurator has no field named '%s'", field)
7281
}
7382
}()

configurator_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,47 @@ func TestPanicMessageIncludesFieldName(t *testing.T) {
244244
panicCheck(func() { config_iface.GetEnvBool("DOES_NOT_EXIST") })
245245
}
246246

247+
func TestPanicOnExistingFieldWrongType(t *testing.T) {
248+
var field_name, field_type string
249+
panicCheck := func(f func()) {
250+
defer func() {
251+
r := recover()
252+
if r == nil {
253+
t.Errorf("Reading field using wrong type did not cause panic")
254+
} else {
255+
switch v := r.(type) {
256+
case string:
257+
if !strings.Contains(v, field_name) {
258+
t.Errorf("Panic msg should contain field name '%s'. Got '%v'", field_name, r)
259+
}
260+
if !strings.Contains(v, field_type) {
261+
t.Errorf("Panic msg should contain field type. Got '%v'", r)
262+
}
263+
case error:
264+
if !strings.Contains(v.Error(), "DOES_NOT_EXIST") {
265+
t.Errorf("Panic msg should contain field name '%s'. Got '%v'", field_name, r)
266+
}
267+
if !strings.Contains(v.Error(), field_type) {
268+
t.Errorf("Panic msg should contain field type. Got '%v'", r)
269+
}
270+
default:
271+
t.Errorf("Panic's type is unkown. Cannot check error msg. Type is '%T'", r)
272+
}
273+
}
274+
}()
275+
f()
276+
}
277+
field_name = "DB_HOST"
278+
field_type = "int"
279+
panicCheck(func() { config_iface.GetEnvInt("DB_HOST") })
280+
field_type = "float"
281+
panicCheck(func() { config_iface.GetEnvFloat("DB_HOST") })
282+
field_type = "bool"
283+
panicCheck(func() { config_iface.GetEnvBool("DB_HOST") })
284+
field_name = "DB_PORT"
285+
field_type = "string"
286+
panicCheck(func() { config_iface.GetEnvString("DB_PORT") })
287+
}
247288
func use(vals ...any) {
248289
for _, val := range vals {
249290
_ = val

0 commit comments

Comments
 (0)