Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve errors.As usage #18794

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions charger/ocpp/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func wait(err error, rc chan error) error {
close(rc)
}

oe := new(ocpp.Error)
if errors.As(err, &oe) && oe.Code == ocppj.GenericError {
if oe := new(ocpp.Error); errors.As(err, &oe) && oe.Code == ocppj.GenericError {
err = api.ErrTimeout
}
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func (e ClassError) MarshalJSON() ([]byte, error) {
Error: e.err.Error(),
}

var de *DeviceError
if errors.As(e.err, &de) {
if de := new(DeviceError); errors.As(e.err, &de) {
res.Device = de.Name
}

Expand Down
6 changes: 2 additions & 4 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,7 @@ func vehicleInstance(cc config.Named) (api.Vehicle, error) {

instance, err := vehicle.NewFromConfig(ctx, cc.Type, cc.Other)
if err != nil {
var ce *util.ConfigError
if errors.As(err, &ce) {
if ce := new(util.ConfigError); errors.As(err, &ce) {
return nil, err
}

Expand Down Expand Up @@ -742,8 +741,7 @@ func tariffInstance(name string, conf config.Typed) (api.Tariff, error) {

instance, err := tariff.NewFromConfig(ctx, conf.Type, conf.Other)
if err != nil {
var ce *util.ConfigError
if errors.As(err, &ce) {
if ce := new(util.ConfigError); errors.As(err, &ce) {
return nil, err
}

Expand Down
3 changes: 1 addition & 2 deletions meter/lgpcs/lgpcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ func (m *Com) update(meterData any) error {

if err := m.DoJSON(req, meterData); err != nil {
// re-login if request returns 405-error
var se request.StatusError
if errors.As(err, &se) && se.StatusCode() == http.StatusMethodNotAllowed {
if se := new(request.StatusError); errors.As(err, &se) && se.StatusCode() == http.StatusMethodNotAllowed {
if err := m.Login(); err != nil {
return err
}
Expand Down
10 changes: 4 additions & 6 deletions server/modbus/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ func (h *handler) logResult(op string, b []byte, err error) {
func (h *handler) exceptionToUint16AndError(op string, b []byte, err error) ([]uint16, error) {
h.logResult(op, b, err)

var modbusError *gridx.Error
if errors.As(err, &modbusError) {
err = mbserver.MapExceptionCodeToError(modbusError.ExceptionCode)
if me := new(gridx.Error); errors.As(err, &me) {
err = mbserver.MapExceptionCodeToError(me.ExceptionCode)
}

return bytesAsUint16(b), err
Expand Down Expand Up @@ -75,9 +74,8 @@ func coilsToBytes(b []bool) []byte {
func (h *handler) bytesToBoolResult(op string, qty uint16, b []byte, err error) ([]bool, error) {
h.logResult(op, b, err)

var modbusError *gridx.Error
if errors.As(err, &modbusError) {
err = mbserver.MapExceptionCodeToError(modbusError.ExceptionCode)
if me := new(gridx.Error); errors.As(err, &me) {
err = mbserver.MapExceptionCodeToError(me.ExceptionCode)
}

var res []bool
Expand Down
3 changes: 1 addition & 2 deletions tariff/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func bo() backoff.BackOff {

// backoffPermanentError returns a permanent error in case of HTTP 400
func backoffPermanentError(err error) error {
var se request.StatusError
if errors.As(err, &se) {
if se := new(request.StatusError); errors.As(err, &se) {
if code := se.StatusCode(); code >= 400 && code <= 599 {
return backoff.Permanent(se)
}
Expand Down
12 changes: 6 additions & 6 deletions util/request/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,22 @@ type StatusError struct {
resp *http.Response
}

func (e StatusError) Error() string {
func (e *StatusError) Error() string {
return fmt.Sprintf("unexpected status: %d (%s)", e.resp.StatusCode, http.StatusText(e.resp.StatusCode))
}

// Response returns the response with the unexpected error
func (e StatusError) Response() *http.Response {
func (e *StatusError) Response() *http.Response {
return e.resp
}

// StatusCode returns the response's status code
func (e StatusError) StatusCode() int {
func (e *StatusError) StatusCode() int {
return e.resp.StatusCode
}

// HasStatus returns true if the response's status code matches any of the given codes
func (e StatusError) HasStatus(codes ...int) bool {
func (e *StatusError) HasStatus(codes ...int) bool {
for _, code := range codes {
if e.resp.StatusCode == code {
return true
Expand All @@ -70,7 +70,7 @@ func (e StatusError) HasStatus(codes ...int) bool {
// ResponseError turns an HTTP status code into an error
func ResponseError(resp *http.Response) error {
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return StatusError{resp: resp}
return &StatusError{resp: resp}
}
return nil
}
Expand All @@ -85,7 +85,7 @@ func ReadBody(resp *http.Response) ([]byte, error) {
}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return b, StatusError{resp: resp}
return b, &StatusError{resp: resp}
}

return b, nil
Expand Down
3 changes: 1 addition & 2 deletions vehicle/fiat/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ func (v *Provider) deepRefresh() error {
if err == nil && res.ResponseStatus != "pending" {
err = fmt.Errorf("invalid response status: %s", res.ResponseStatus)
} else {
var se request.StatusError
if errors.As(err, &se) && se.StatusCode() == http.StatusForbidden {
if se := new(request.StatusError); errors.As(err, &se) && se.StatusCode() == http.StatusForbidden {
err = nil
}
}
Expand Down
3 changes: 2 additions & 1 deletion vehicle/renault/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package renault

import (
"errors"
"net/http"
"slices"
"strings"
Expand Down Expand Up @@ -140,7 +141,7 @@ func (v *Provider) Climater() (bool, error) {
res, err := v.hvacG()

// Zoe Ph2, Megane e-tech
if err, ok := err.(request.StatusError); ok && err.HasStatus(http.StatusForbidden, http.StatusNotFound, http.StatusBadGateway) {
if se := new(request.StatusError); errors.As(err, &se) && se.HasStatus(http.StatusForbidden, http.StatusNotFound, http.StatusBadGateway) {
return false, api.ErrNotAvailable
}

Expand Down
3 changes: 1 addition & 2 deletions vehicle/tronity.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ func (v *Tronity) post(uri string) error {
}

// ignore HTTP 405
var se request.StatusError
if errors.As(err, &se) && se.StatusCode() == http.StatusMethodNotAllowed {
if se := new(request.StatusError); errors.As(err, &se) && se.StatusCode() == http.StatusMethodNotAllowed {
err = nil
}

Expand Down
3 changes: 2 additions & 1 deletion vehicle/vw/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vw

import (
"errors"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -99,7 +100,7 @@ func (v *API) Status(vin string) (StatusResponse, error) {
err = v.DoJSON(req, &res)
}

if _, ok := err.(request.StatusError); ok {
if se := new(request.StatusError); errors.As(err, &se) {
var rr RolesRights
rr, err = v.RolesRights(vin)

Expand Down
Loading