Skip to content

Commit

Permalink
Plugins: allow returning known error values as string (#14451)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Jun 23, 2024
1 parent 4171fd4 commit 7a0390c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
15 changes: 15 additions & 0 deletions provider/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"fmt"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
)

Expand All @@ -16,3 +17,17 @@ func setFormattedValue(message, param string, v interface{}) (string, error) {
param: v,
})
}

// knownErrors maps string responses to known error codes
func knownErrors(b []byte) error {
switch string(b) {
case "ErrAsleep":
return api.ErrAsleep
case "ErrMustRetry":
return api.ErrMustRetry
case "ErrNotAvailable":
return api.ErrNotAvailable
default:
return nil
}
}
5 changes: 5 additions & 0 deletions provider/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ func (p *HTTP) request(url string, body string) ([]byte, error) {
}

p.val, p.err = p.DoBody(req)
if p.err != nil {
if err := knownErrors(p.val); err != nil {
p.err = err
}
}
p.updated = time.Now()
}

Expand Down
4 changes: 4 additions & 0 deletions provider/mqtt_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (h *msgHandler) hasValue() (string, error) {
return "", err
}

if err := knownErrors([]byte(payload)); err != nil {
return "", err
}

if h.pipeline != nil {
b, err := h.pipeline.Process([]byte(payload))
return string(b), err
Expand Down
10 changes: 9 additions & 1 deletion provider/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,15 @@ var _ StringProvider = (*Socket)(nil)
func (p *Socket) StringGetter() (func() (string, error), error) {
return func() (string, error) {
val, err := p.val.Get()
return string(val), err
if err != nil {
return "", err
}

if err := knownErrors(val); err != nil {
return "", err
}

return string(val), nil
}, nil
}

Expand Down

0 comments on commit 7a0390c

Please sign in to comment.