improve error handling
diff --git a/gcfg.go b/gcfg.go
index 4c63cf2..c5d1309 100644
--- a/gcfg.go
+++ b/gcfg.go
@@ -110,7 +110,7 @@
vDest := reflect.ValueOf(cfg).Elem()
vSect := fieldFold(vDest, sect)
if !vSect.IsValid() {
- return fmt.Errorf("no corresponding field: section %q", sect)
+ return fmt.Errorf("invalid section: section %q", sect)
}
if vSect.Kind() == reflect.Map {
if vSect.IsNil() {
@@ -125,12 +125,13 @@
}
vSect = pv.Elem()
} else if sub != "" {
- return fmt.Errorf("corresponding field should be a map: "+
+ return fmt.Errorf("invalid subsection: "+
"section %q subsection %q", sect, sub)
}
vName := fieldFold(vSect, name)
if !vName.IsValid() {
- return fmt.Errorf("no corresponding field: name %q", name)
+ return fmt.Errorf("invalid variable: "+
+ "section %q subsection %q variable %q", sect, sub, name)
}
vAddr := vName.Addr().Interface()
switch v := vAddr.(type) {
diff --git a/gcfg_test.go b/gcfg_test.go
index 6a7ff5c..a436fc5 100644
--- a/gcfg_test.go
+++ b/gcfg_test.go
@@ -106,6 +106,8 @@
{"\n[sub \"\"]\nname=value", &conf04{}, false},
// error: section name not matched
{"\n[nonexistent]\nname=value", &conf01{}, false},
+ // error: subsection name not matched
+ {"\n[section \"nonexistent\"]\nname=value", &conf01{}, false},
// error: variable name not matched
{"\n[section]\nnonexistent=value", &conf01{}, false},
}},