From 1e43ec1b9c00c91e68fee1e6fd13b9d6e26232e9 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 14 Apr 2024 15:51:08 +0200 Subject: [PATCH] chore: use errors.As where appropriate --- meter/lgpcs/lgpcs.go | 3 ++- tariff/entsoe.go | 6 +++--- tariff/helper.go | 4 +++- tariff/ngeso.go | 2 +- vehicle/tronity.go | 8 ++++---- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/meter/lgpcs/lgpcs.go b/meter/lgpcs/lgpcs.go index 35d1297f6..e06a1c35d 100644 --- a/meter/lgpcs/lgpcs.go +++ b/meter/lgpcs/lgpcs.go @@ -156,7 +156,8 @@ func (m *Com) refreshData() (EssData, error) { if err = m.DoJSON(req, &resp); err != nil { // re-login if request returns 405-error - if err2, ok := err.(request.StatusError); ok && err2.HasStatus(http.StatusMethodNotAllowed) { + var se request.StatusError + if errors.As(err, &se) && se.StatusCode() == http.StatusMethodNotAllowed { err = m.Login() if err == nil { diff --git a/tariff/entsoe.go b/tariff/entsoe.go index db2c63409..9511aad84 100644 --- a/tariff/entsoe.go +++ b/tariff/entsoe.go @@ -97,9 +97,9 @@ func (t *Entsoe) run(done chan error) { // Request the next 24 hours of data. data, err := t.DoBody(entsoe.DayAheadPricesRequest(t.domain, time.Hour*24)) - // Consider whether errors.As would be more appropriate if this needs to start dealing with wrapped errors. - if se, ok := err.(request.StatusError); ok { - if se.HasStatus(http.StatusBadRequest) { + var se request.StatusError + if errors.As(err, &se) { + if se.StatusCode() == http.StatusBadRequest { return backoff.Permanent(se) } diff --git a/tariff/helper.go b/tariff/helper.go index 3b9666c96..746badc26 100644 --- a/tariff/helper.go +++ b/tariff/helper.go @@ -1,6 +1,7 @@ package tariff import ( + "errors" "time" "github.com/cenkalti/backoff/v4" @@ -16,7 +17,8 @@ func newBackoff() backoff.BackOff { // backoffPermanentError returns a permanent error in case of HTTP 400 func backoffPermanentError(err error) error { - if se, ok := err.(request.StatusError); ok { + var se request.StatusError + if errors.As(err, &se) { if code := se.StatusCode(); code >= 400 && code < 500 { return backoff.Permanent(se) } diff --git a/tariff/ngeso.go b/tariff/ngeso.go index 9b47b8dbb..71b7c87c2 100644 --- a/tariff/ngeso.go +++ b/tariff/ngeso.go @@ -80,7 +80,7 @@ func (t *Ngeso) run(done chan error) { res, err := backoff.RetryWithData(func() (ngeso.CarbonForecastResponse, error) { res, err := tReq.DoRequest(client) var se request.StatusError - if errors.As(err, &se) && se.HasStatus(http.StatusBadRequest) { + if errors.As(err, &se) && se.StatusCode() == http.StatusBadRequest { return nil, backoff.Permanent(se) } return res, err diff --git a/vehicle/tronity.go b/vehicle/tronity.go index 2000bd0ac..f4b2f03ab 100644 --- a/vehicle/tronity.go +++ b/vehicle/tronity.go @@ -19,6 +19,7 @@ package vehicle import ( "context" + "errors" "fmt" "net/http" "slices" @@ -224,10 +225,9 @@ func (v *Tronity) post(uri string) error { } // ignore HTTP 405 - if err != nil { - if err2, ok := err.(request.StatusError); ok && err2.HasStatus(http.StatusMethodNotAllowed) { - err = nil - } + var se request.StatusError + if errors.As(err, &se) && se.StatusCode() == http.StatusMethodNotAllowed { + err = nil } return err