1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16
17 package camtypes
18
19 import (
20 "fmt"
21 "log"
22
23 "perkeep.org/internal/osutil"
24 )
25
26
27
28 var camErrors = map[string]*camErr{}
29
30 var (
31 ErrClientNoServer = addCamError("client-no-server", funcStr(func() string {
32 return fmt.Sprintf("No valid server defined. It can be set with the CAMLI_SERVER environment variable, or the --server flag, or in the \"servers\" section of %q (see https://perkeep.org/doc/client-config).", osutil.UserClientConfigPath())
33 }))
34 ErrClientNoPublicKey = addCamError("client-no-public-key", str("No public key configured: see 'pk-put init'."))
35 )
36
37 type str string
38
39 func (s str) String() string { return string(s) }
40
41 type funcStr func() string
42
43 func (f funcStr) String() string { return f() }
44
45 type camErr struct {
46 key string
47 des fmt.Stringer
48 }
49
50 func (ce *camErr) Error() string {
51 return ce.des.String()
52 }
53
54 func (ce *camErr) Fatal() {
55 log.Fatalf("%v error. See %v", ce.key, ce.URL())
56 }
57
58 func (ce *camErr) Warn() {
59 log.Printf("%v error. See %v.", ce.key, ce.URL())
60 }
61
62 func (ce *camErr) URL() string {
63 return fmt.Sprintf("https://perkeep.org/err/%s", ce.key)
64 }
65
66
67
68 func Err(key string) error {
69 v, ok := camErrors[key]
70 if !ok {
71 panic(fmt.Sprintf("unknown/unregistered error key %v", key))
72 }
73 return v
74 }
75
76 func addCamError(key string, des fmt.Stringer) *camErr {
77 if e, ok := camErrors[key]; ok {
78 panic(fmt.Sprintf("error %v already registered as %q", key, e.Error()))
79 }
80 e := &camErr{
81 key: key,
82 des: des,
83 }
84 camErrors[key] = e
85 return e
86 }