Return errors instead of fatal

This commit is contained in:
andig 2020-07-31 13:32:12 +02:00
parent 2d187750fb
commit 1be64c7353
4 changed files with 11 additions and 12 deletions

View file

@ -38,7 +38,7 @@ func NewSimpleEVSE(conn, device string) (api.Charger, error) {
var handler modbus.ClientHandler
if conn != "" && device != "" {
log.FATAL.Fatal("cannot define uri and device both")
return nil, errors.New("cannot define uri and device both")
}
if conn != "" {
handler = modbus.NewTCPClientHandler(conn)

View file

@ -31,13 +31,14 @@ type Auth struct {
}
// NewAuth creates authorization headers from config
func NewAuth(log *util.Logger, auth Auth, headers map[string]string) {
func NewAuth(log *util.Logger, auth Auth, headers map[string]string) error {
if strings.ToLower(auth.Type) != "basic" {
log.FATAL.Fatalf("unsupported auth type: %s", auth.Type)
return fmt.Errorf("unsupported auth type: %s", auth.Type)
}
basicAuth := auth.User + ":" + auth.Password
headers["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte(basicAuth))
return nil
}
// NewHTTPProviderFromConfig creates a HTTP provider
@ -50,7 +51,7 @@ func NewHTTPProviderFromConfig(other map[string]interface{}) (*HTTP, error) {
Scale float64
Insecure bool
Auth Auth
}{}
}{Headers: make(map[string]string)}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
@ -69,10 +70,9 @@ func NewHTTPProviderFromConfig(other map[string]interface{}) (*HTTP, error) {
// handle basic auth
if cc.Auth.Type != "" {
if p.headers == nil {
p.headers = make(map[string]string)
if err := NewAuth(log, cc.Auth, p.headers); err != nil {
return nil, err
}
NewAuth(log, cc.Auth, p.headers)
}
// ignore the self signed certificate

View file

@ -37,7 +37,7 @@ func NewSocketProviderFromConfig(other map[string]interface{}) (*Socket, error)
Insecure bool
Auth Auth
Timeout time.Duration
}{}
}{Headers: make(map[string]string)}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
@ -54,10 +54,9 @@ func NewSocketProviderFromConfig(other map[string]interface{}) (*Socket, error)
// handle basic auth
if cc.Auth.Type != "" {
if p.headers == nil {
p.headers = make(map[string]string)
if err := NewAuth(log, cc.Auth, p.headers); err != nil {
return nil, err
}
NewAuth(log, cc.Auth, p.headers)
}
// ignore the self signed certificate

View file

@ -11,7 +11,7 @@ const config = "../errors.yaml"
var acceptable map[string][]string
// Acceptable returns true is a test error is configured as acceptable
// Acceptable checks if a test error is configured as acceptable
func Acceptable(class string, err error) bool {
if len(acceptable) == 0 {
definitions, err := ioutil.ReadFile(config)