Home Download Docs Code Community
     1	/*
     2	Copyright 2013 The Perkeep Authors
     3	
     4	Licensed under the Apache License, Version 2.0 (the "License");
     5	you may not use this file except in compliance with the License.
     6	You may obtain a copy of the License at
     7	
     8	     http://www.apache.org/licenses/LICENSE-2.0
     9	
    10	Unless required by applicable law or agreed to in writing, software
    11	distributed under the License is distributed on an "AS IS" BASIS,
    12	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13	See the License for the specific language governing permissions and
    14	limitations under the License.
    15	*/
    16	
    17	package camtypes
    18	
    19	import (
    20		"fmt"
    21		"log"
    22	
    23		"perkeep.org/internal/osutil"
    24	)
    25	
    26	// TODO(mpl): move pkg/camerrors stuff in here
    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	// Err returns the error registered for key.
    67	// It panics for an unregistered key.
    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	}
Website layout inspired by memcached.
Content by the authors.