veyron.io/veyron/veyron/services/config/lib: Convert to verror2
This change uses verror2 instead of verror throughout
veyron.io/veyron/veyron/services/config/lib/config.go.
Tests in veyron.io/veyron/veyron/services/config/lib still pass.
The import statement renames "verror2" to "verror" so that the renaming that
will happen when we eliminate the old verror will be simpler.
I've replaced the existing error formats with custom error codes with very
similar formats to what was there before. Let me know if you'd prefer to use
generic error codes instead. The tradeoff is between messgaes customized for a particular
module, and more familiar error codes.
(For example, instead of using the custom code "errNoConfig" with the English
string "no config", I could use verror2.NoExist, which has the English message
"Does not exist", and thus does not contain the word "config".)
Change-Id: Ia8af80111c4a99f2cc1a29d2a94941a4784a0f98
diff --git a/services/config/lib/config.go b/services/config/lib/config.go
index b6542b4..41669e1 100644
--- a/services/config/lib/config.go
+++ b/services/config/lib/config.go
@@ -17,7 +17,7 @@
"strings"
"sync"
- "veyron.io/veyron/veyron2/verror"
+ verror "veyron.io/veyron/veyron2/verror2"
"veyron.io/veyron/veyron2/vlog"
"github.com/presotto/go-mdns-sd"
@@ -42,6 +42,21 @@
gen int // incremented every config change
}
+const pkgPath = "veyron.io/veyron/veyron/services/config/lib"
+
+// Errors
+var (
+ errCantParse = verror.Register(pkgPath+".errCantParse", verror.NoRetry, "{1:}{2:} can't parse{:_}")
+ errEntryTooLong = verror.Register(pkgPath+".errEntryTooLong", verror.NoRetry, "{1:}{2:} entry {3}:{4} is too long{:_}")
+ errNoFileToRead = verror.Register(pkgPath+".errNoFileToRead", verror.NoRetry, "{1:}{2:} no file to read{:_}")
+ errFileError = verror.Register(pkgPath+".errFileError", verror.NoRetry, "{1:}{2:} file {3}{:_}")
+ errMissingLegalVersion = verror.Register(pkgPath+".errMissingLegalVersion", verror.NoRetry, "{1:}{2:} missing legal version for file{:_}")
+ errMissingConfigVersion = verror.Register(pkgPath+".errMissingConfigVersion", verror.NoRetry, "{1:}{2:} missing config version{:_}")
+ errNoConfig = verror.Register(pkgPath+".errNoConfig", verror.NoRetry, "{1:}{2:} no config{:_}")
+ errConfigHasNoKey = verror.Register(pkgPath+".errConfigHasNoKey", verror.NoRetry, "{1:}{2:} config has no key {3}{:_}")
+ errOfferingConfigError = verror.Register(pkgPath+".errOfferingConfigError", verror.NoRetry, "{1:}{2:} offering config {3}{:_}")
+)
+
// MDNSConfigService creates a new instance of the config service with the given name.
// If file is non blank, the initial config is read from file and any learned configs are
// stored in it. Only instances with a file to backup will offer configs to the net.
@@ -126,12 +141,12 @@
// The reset have to be key<white>*:<white>*value
f := strings.SplitN(l, ":", 2)
if len(f) != 2 {
- return verror.BadArgf("can't parse %s", l)
+ return verror.Make(errCantParse, nil, l)
}
k := strings.TrimSpace(f[0])
v := strings.TrimSpace(f[1])
if len(k)+len(v) > maxDNSStringLength {
- return verror.BadArgf("entry %s:%s is too long", k, v)
+ return verror.Make(errEntryTooLong, nil, k, v)
}
c.pairs[k] = v
if k != "version" {
@@ -144,14 +159,14 @@
func serializeEntry(k, v string) (string, error) {
if len(k)+len(v) > maxDNSStringLength {
- return "", verror.BadArgf("entry %s:%s is too long", k, v)
+ return "", verror.Make(errEntryTooLong, nil, k, v)
}
return k + ":" + v, nil
}
func readFile(file string) (*config, error) {
if len(file) == 0 {
- return nil, verror.NoExistf("no file to read")
+ return nil, verror.Make(errNoFileToRead, nil)
}
// The config has to be small so just read it all in one go.
@@ -162,11 +177,11 @@
c := newConfig()
for _, l := range strings.Split(string(b), "\n") {
if err := c.parseEntry(l); err != nil {
- return nil, verror.BadArgf("file %s: %s", file, err)
+ return nil, verror.Make(errFileError, nil, file, err)
}
}
if _, ok := c.pairs["version"]; !ok {
- return nil, verror.BadArgf("file %s: missing legal version", file)
+ return nil, verror.Make(errMissingLegalVersion, nil, file)
}
return c, nil
}
@@ -200,7 +215,7 @@
}
// Ignore any config with no version.
if _, ok := c.pairs["version"]; !ok {
- return nil, verror.NoExistf("missing config version")
+ return nil, verror.Make(errMissingConfigVersion, nil)
}
return c, nil
}
@@ -310,10 +325,10 @@
cs.rwlock.RLock()
defer cs.rwlock.RUnlock()
if cs.current == nil {
- return "", verror.NoExistf("no config")
+ return "", verror.Make(errNoConfig, nil)
}
if v, ok := cs.current.pairs[key]; !ok {
- return "", verror.NoExistf("config has no key %q", key)
+ return "", verror.Make(errConfigHasNoKey, nil, key)
} else {
return v, nil
}
@@ -331,7 +346,7 @@
cs.rwlock.RLock()
defer cs.rwlock.RUnlock()
if cs.current == nil {
- return nil, verror.NoExistf("no config found")
+ return nil, verror.Make(errNoConfig, nil)
}
// Copy so caller can't change the map under our feet.
reply := make(map[string]string)
@@ -366,7 +381,7 @@
for _, k := range keys {
e, err := serializeEntry(k, cs.current.pairs[k])
if err != nil {
- verror.NoExistf("offering config: %s", cs.file, err)
+ verror.Make(errOfferingConfigError, nil, cs.file, err)
return
}
txt = append(txt, e)