Skip to content

Commit c01fb89

Browse files
authored
fix #2 bad field name is logged in resulting panic (#3)
1 parent e5915fe commit c01fb89

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

configurator.go

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

33
import (
4-
"fmt"
54
"log"
65
"os"
76
"reflect"
@@ -24,7 +23,7 @@ type Configurator interface {
2423
func GetEnvString(config Configurator, field string) string {
2524
defer func() {
2625
if r := recover(); r != nil {
27-
log.Fatalln(r)
26+
log.Panicf("configurator has no field named '%s'", field)
2827
}
2928
}()
3029
value, exists := os.LookupEnv(field)
@@ -39,7 +38,7 @@ func GetEnvString(config Configurator, field string) string {
3938
func GetEnvBool(config Configurator, field string) bool {
4039
defer func() {
4140
if r := recover(); r != nil {
42-
log.Fatalln(r)
41+
log.Panicf("configurator has no field named '%s'", field)
4342
}
4443
}()
4544
value, err := strconv.ParseBool(os.Getenv(field))
@@ -54,7 +53,7 @@ func GetEnvBool(config Configurator, field string) bool {
5453
func GetEnvInt(config Configurator, field string) int {
5554
defer func() {
5655
if r := recover(); r != nil {
57-
log.Fatalln(r)
56+
log.Panicf("configurator has no field named '%s'", field)
5857
}
5958
}()
6059
value, err := strconv.Atoi(os.Getenv(field))
@@ -69,7 +68,7 @@ func GetEnvInt(config Configurator, field string) int {
6968
func GetEnvFloat(config Configurator, field string) float64 {
7069
defer func() {
7170
if r := recover(); r != nil {
72-
log.Fatalln(r)
71+
log.Panicf("configurator has no field named '%s'", field)
7372
}
7473
}()
7574
value, err := strconv.ParseFloat(os.Getenv(field), 64)
@@ -89,7 +88,7 @@ func getString(c any, field string) string {
8988
result := field_value.String()
9089
re := regexp.MustCompile(`<[a-zA-Z]*\sValue>`)
9190
if re.MatchString(result) {
92-
panic(fmt.Sprintf("env var not in config struct: `%v`", field))
91+
log.Panicf("configurator has no field named '%s'", field)
9392
}
9493
return result
9594
}
@@ -116,7 +115,7 @@ func getNonString(c any, field string) reflect.Value {
116115
defer func() {
117116
//catch panic when field does not exist
118117
if r := recover(); r != nil {
119-
panic(fmt.Sprintf("env var not in config struct: `%v`", field))
118+
log.Panicf("configurator has no field named '%s'", field)
120119
}
121120
}()
122121
c_value := reflect.ValueOf(c)

configurator_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"os"
5+
"strings"
56
"testing"
67
)
78

@@ -213,6 +214,36 @@ func TestBoolWithBadEnvStrings(t *testing.T) {
213214
}
214215
}
215216

217+
func TestPanicMessageIncludesFieldName(t *testing.T) {
218+
panicCheck := func(f func()) {
219+
field_name := "DOES_NOT_EXIST"
220+
defer func() {
221+
r := recover()
222+
if r == nil {
223+
t.Errorf("Reading non existant field did not cause panic")
224+
} else {
225+
switch v := r.(type) {
226+
case string:
227+
if !strings.Contains(v, field_name) {
228+
t.Errorf("Panic msg should contain field name '%s'. Got '%v'", field_name, r)
229+
}
230+
case error:
231+
if !strings.Contains(v.Error(), "DOES_NOT_EXIST") {
232+
t.Errorf("Panic msg should contain field name '%s'. Got '%v'", field_name, r)
233+
}
234+
default:
235+
t.Errorf("Panic's type is unkown. Cannot check error msg. Type is '%T'", r)
236+
}
237+
}
238+
}()
239+
f()
240+
}
241+
panicCheck(func() { config_iface.GetEnvInt("DOES_NOT_EXIST") })
242+
panicCheck(func() { config_iface.GetEnvFloat("DOES_NOT_EXIST") })
243+
panicCheck(func() { config_iface.GetEnvString("DOES_NOT_EXIST") })
244+
panicCheck(func() { config_iface.GetEnvBool("DOES_NOT_EXIST") })
245+
}
246+
216247
func use(vals ...any) {
217248
for _, val := range vals {
218249
_ = val

0 commit comments

Comments
 (0)