Skip to content

Commit

Permalink
Return errors instead of fatal
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Jul 31, 2020
1 parent 2d18775 commit 1be64c7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion charger/simpleevse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions provider/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions provider/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion util/test/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1be64c7

Please sign in to comment.