chore: use errors.As where appropriate
This commit is contained in:
parent
a591a5cc6d
commit
1e43ec1b9c
5 changed files with 13 additions and 10 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue