Skip to content

Commit

Permalink
refactor(orc8r): replace deprecated errors module
Browse files Browse the repository at this point in the history
Replace functions from the github.com/pgk/errors package with
`fmt.Errorsf` in /magma/lte. Created in pairing with Moritz Huebner.

Signed-off-by: Sebastian Wolf <sebastian.wolf@tngtech.com>
  • Loading branch information
wolfseb committed May 17, 2022
1 parent 3134bc8 commit 9c4f6d7
Show file tree
Hide file tree
Showing 33 changed files with 217 additions and 222 deletions.
3 changes: 1 addition & 2 deletions lte/cloud/go/services/ha/servicers/southbound/servicer.go
Expand Up @@ -19,7 +19,6 @@ import (
"time"

"github.com/golang/glog"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -59,7 +58,7 @@ func (s *HAServicer) GetEnodebOffloadState(ctx context.Context, req *lte_protos.
}
cfg, err := configurator.LoadEntityConfig(ctx, secondaryGw.GetNetworkId(), lte.CellularGatewayEntityType, secondaryGw.LogicalId, lte_models.EntitySerdes)
if err != nil {
errors.Wrap(err, "unable to load cellular gateway configs to find primary gateway's in its pool")
fmt.Errorf("unable to load cellular gateway configs to find primary gateway's in its pool: %w", err)
return ret, err
}
cellularCfg, ok := cfg.(*lte_models.GatewayCellularConfigs)
Expand Down
19 changes: 9 additions & 10 deletions lte/cloud/go/services/lte/obsidian/handlers/handlers.go
Expand Up @@ -19,7 +19,6 @@ import (
"net/http"

"github.com/labstack/echo"
"github.com/pkg/errors"
"github.com/thoas/go-funk"

"magma/lte/cloud/go/lte"
Expand Down Expand Up @@ -193,7 +192,7 @@ func getGateway(c echo.Context) error {
serdes.Entity,
)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errors.Wrap(err, "failed to load cellular gateway"))
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to load cellular gateway: %w", err))
}

ret := &lte_models.LteGateway{
Expand All @@ -219,7 +218,7 @@ func getGateway(c echo.Context) error {
case lte.APNResourceEntityType:
e, err := configurator.LoadEntity(reqCtx, nid, tk.Type, tk.Key, configurator.EntityLoadCriteria{LoadConfig: true}, serdes.Entity)
if err != nil {
return errors.Wrap(err, "error loading apn resource entity")
return fmt.Errorf("error loading apn resource entity: %w", err)
}
apnResource := (&lte_models.ApnResource{}).FromEntity(e)
ret.ApnResources[string(apnResource.ApnName)] = *apnResource
Expand Down Expand Up @@ -257,7 +256,7 @@ func deleteGateway(c echo.Context) error {
serdes.Entity,
)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errors.Wrap(err, "error loading existing cellular gateway"))
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("error loading existing cellular gateway: %w", err))
}
deletes = append(deletes, gw.Associations.Filter(lte.APNResourceEntityType)...)

Expand Down Expand Up @@ -619,7 +618,7 @@ func updateApnConfiguration(c echo.Context) error {
case err == merrors.ErrNotFound:
return echo.ErrNotFound
case err != nil:
return echo.NewHTTPError(http.StatusInternalServerError, errors.Wrap(err, "failed to load existing APN"))
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to load existing APN: %w", err))
}

err = configurator.CreateOrUpdateEntityConfig(reqCtx, networkID, lte.APNEntityType, apnName, payload.ApnConfiguration, serdes.Entity)
Expand Down Expand Up @@ -670,7 +669,7 @@ func AddNetworkWideSubscriberRuleName(c echo.Context) error {
}
err := addToNetworkSubscriberConfig(c.Request().Context(), networkID, params[0], "")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errors.Wrap(err, "Failed to update config"))
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("Failed to update config: %w", err))
}
return c.NoContent(http.StatusCreated)
}
Expand All @@ -686,7 +685,7 @@ func AddNetworkWideSubscriberBaseName(c echo.Context) error {
}
err := addToNetworkSubscriberConfig(c.Request().Context(), networkID, "", params[0])
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errors.Wrap(err, "Failed to update config"))
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("Failed to update config: %w", err))
}
return c.NoContent(http.StatusCreated)
}
Expand All @@ -702,7 +701,7 @@ func RemoveNetworkWideSubscriberRuleName(c echo.Context) error {
}
err := removeFromNetworkSubscriberConfig(c.Request().Context(), networkID, params[0], "")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errors.Wrap(err, "Failed to update config"))
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("Failed to update config: %w", err))
}
return c.NoContent(http.StatusNoContent)
}
Expand All @@ -718,7 +717,7 @@ func RemoveNetworkWideSubscriberBaseName(c echo.Context) error {
}
err := removeFromNetworkSubscriberConfig(c.Request().Context(), networkID, "", params[0])
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errors.Wrap(err, "Failed to update config"))
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("Failed to update config: %w", err))
}
return c.NoContent(http.StatusNoContent)
}
Expand Down Expand Up @@ -883,7 +882,7 @@ func updateGatewayPoolHandler(c echo.Context) error {
// 404 if pool doesn't exist
exists, err := configurator.DoesEntityExist(reqCtx, networkID, lte.CellularGatewayPoolEntityType, gatewayPoolID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errors.Wrap(err, "Error while checking if gateway pool exists"))
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("Error while checking if gateway pool exists: %w", err))
}
if !exists {
return echo.ErrNotFound
Expand Down
3 changes: 1 addition & 2 deletions lte/cloud/go/services/lte/obsidian/models/conversion.go
Expand Up @@ -19,7 +19,6 @@ import (
"sort"

"github.com/go-openapi/swag"
"github.com/pkg/errors"
"github.com/thoas/go-funk"

"magma/lte/cloud/go/lte"
Expand Down Expand Up @@ -307,7 +306,7 @@ func (m *MutableLteGateway) getAPNResourceChanges(
oldIDs := existingGateway.Associations.Filter(lte.APNResourceEntityType).Keys()
oldByAPN, err := LoadAPNResources(ctx, existingGateway.NetworkID, oldIDs)
if err != nil {
return nil, nil, errors.Wrap(err, "error loading existing APN resources")
return nil, nil, fmt.Errorf("error loading existing APN resources: %w", err)
}
oldResources := oldByAPN.GetByID()
newResources := m.ApnResources.GetByID()
Expand Down
49 changes: 24 additions & 25 deletions lte/cloud/go/services/lte/obsidian/models/validate.go
Expand Up @@ -18,14 +18,13 @@ import (
"fmt"
"net"

"magma/lte/cloud/go/lte"
"magma/orc8r/cloud/go/services/configurator"

oerrors "github.com/go-openapi/errors"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/go-openapi/validate"
"github.com/pkg/errors"

"magma/lte/cloud/go/lte"
"magma/orc8r/cloud/go/services/configurator"
)

func (m *LteNetwork) ValidateModel(context.Context) error {
Expand Down Expand Up @@ -83,10 +82,10 @@ func (m FegNetworkID) ValidateModel(ctx context.Context) error {
if !swag.IsZero(m) {
exists, err := configurator.DoesNetworkExist(ctx, string(m))
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to search for network %s", string(m)))
return fmt.Errorf("Failed to search for network %s: %w", string(m), err)
}
if !exists {
return errors.New(fmt.Sprintf("Network: %s does not exist", string(m)))
return fmt.Errorf("Network: %s does not exist", string(m))
}
}
return nil
Expand All @@ -105,7 +104,7 @@ func (m *NetworkEpcConfigs) ValidateModel(context.Context) error {

for name := range m.SubProfiles {
if name == "" {
return errors.New("profile name should be non-empty")
return fmt.Errorf("profile name should be non-empty")
}
}
return nil
Expand Down Expand Up @@ -139,9 +138,9 @@ func (m *NetworkRanConfigs) ValidateModel(context.Context) error {
fddConfigSet := m.FddConfig != nil

if tddConfigSet && fddConfigSet {
return errors.New("only one of TDD or FDD configs can be set")
return fmt.Errorf("only one of TDD or FDD configs can be set")
} else if !tddConfigSet && !fddConfigSet {
return errors.New("either TDD or FDD configs must be set")
return fmt.Errorf("either TDD or FDD configs must be set")
}

earfcnDl := m.getEarfcnDl()
Expand All @@ -151,14 +150,14 @@ func (m *NetworkRanConfigs) ValidateModel(context.Context) error {
}

if tddConfigSet && band.Mode != lte.TDDMode {
return errors.Errorf("band %d not a TDD band", band.ID)
return fmt.Errorf("band %d not a TDD band", band.ID)
}
if fddConfigSet {
if band.Mode != lte.FDDMode {
return errors.Errorf("band %d not a FDD band", band.ID)
return fmt.Errorf("band %d not a FDD band", band.ID)
}
if !band.EarfcnULInRange(m.FddConfig.Earfcnul) {
return errors.Errorf("EARFCNUL=%d invalid for band %d (%d, %d)", m.FddConfig.Earfcnul, band.ID, band.StartEarfcnUl, band.StartEarfcnDl)
return fmt.Errorf("EARFCNUL=%d invalid for band %d (%d, %d)", m.FddConfig.Earfcnul, band.ID, band.StartEarfcnUl, band.StartEarfcnDl)
}
}

Expand All @@ -182,26 +181,26 @@ func (m *NetworkEpcConfigsMobility) validateMobility() error {
// TODO: Add validation for DHCP once is added to EPC config

if mobilityNatConfigSet && mobilityStaticConfigSet {
return errors.New("only one of the mobility IP allocation modes can be set")
return fmt.Errorf("only one of the mobility IP allocation modes can be set")
}

if mobilityNatConfigSet {
if m.IPAllocationMode != NATAllocationMode {
return errors.New("invalid config set for NAT allocation mode")
return fmt.Errorf("invalid config set for NAT allocation mode")
}

if err := validateIPBlocks(m.Nat.IPBlocks); err != nil {
return errors.New("invalid IP block on config")
return fmt.Errorf("invalid IP block on config")
}
}
if mobilityStaticConfigSet {
if m.IPAllocationMode != StaticAllocationMode {
return errors.New("invalid config set for STATIC allocation mode")
return fmt.Errorf("invalid config set for STATIC allocation mode")
}

for _, ipBlocks := range m.Static.IPBlocksByTac {
if err := validateIPBlocks(ipBlocks); err != nil {
return errors.New("invalid IP block on config")
return fmt.Errorf("invalid IP block on config")
}
}
}
Expand Down Expand Up @@ -288,25 +287,25 @@ func (m *GatewayEpcConfigs) ValidateModel(context.Context) error {
if m.DNSPrimary != "" {
ip := net.ParseIP(m.DNSPrimary)
if ip == nil {
return errors.New("Invalid primary DNS address")
return fmt.Errorf("Invalid primary DNS address")
} else if ip.To4() == nil {
return errors.New("Only IPv4 is supported currently for DNS")
return fmt.Errorf("Only IPv4 is supported currently for DNS")
}
}

if m.DNSSecondary != "" {
secIp := net.ParseIP(m.DNSSecondary)
if secIp == nil {
return errors.New("Invalid secondary DNS address")
return fmt.Errorf("Invalid secondary DNS address")
} else if secIp.To4() == nil {
return errors.New("Only IPv4 is supported currently for DNS")
return fmt.Errorf("Only IPv4 is supported currently for DNS")
}
}

if m.IPV6DNSAddr != "" {
ip := net.ParseIP(string(m.IPV6DNSAddr))
if ip == nil {
return errors.New("Invalid IPV6 DNS address")
return fmt.Errorf("Invalid IPV6 DNS address")
}
}
return nil
Expand Down Expand Up @@ -391,18 +390,18 @@ func (m *EnodebConfig) validateEnodebConfig() error {
unmanagedConfigSet := m.UnmanagedConfig != nil

if managedConfigSet && unmanagedConfigSet {
return errors.New("only one of the eNodeb config types can be set")
return fmt.Errorf("only one of the eNodeb config types can be set")
}

if managedConfigSet {
if m.ConfigType != ManagedConfigType {
return errors.New("invalid type set for managed config")
return fmt.Errorf("invalid type set for managed config")
}

}
if unmanagedConfigSet {
if m.ConfigType != UnmanagedConfigType {
return errors.New("invalid type set for unmanaged config")
return fmt.Errorf("invalid type set for unmanaged config")
}
}

Expand Down
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"

"github.com/golang/glog"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -103,12 +102,12 @@ func setEnodebState(ctx context.Context, networkID string, states state_types.St
}
gwEnt, err := configurator.LoadEntityForPhysicalID(ctx, st.ReporterID, configurator.EntityLoadCriteria{}, serdes.Entity)
if err != nil {
stateErrors[id] = errors.Wrap(err, "error loading gatewayID")
stateErrors[id] = fmt.Errorf("error loading gatewayID: %w", err)
continue
}
err = lte_api.SetEnodebState(ctx, networkID, gwEnt.Key, id.DeviceID, serializedState)
if err != nil {
stateErrors[id] = errors.Wrap(err, "error setting enodeb state")
stateErrors[id] = fmt.Errorf("error setting enodeb state: %w", err)
continue
}
glog.V(2).Infof("successfully stored ENB state for eNB SN: %s, gatewayID: %s:w", id.DeviceID, gwEnt.Key)
Expand Down
4 changes: 2 additions & 2 deletions lte/cloud/go/services/lte/servicers/protected/lookup.go
Expand Up @@ -15,8 +15,8 @@ package servicers

import (
"context"
"fmt"

"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -62,7 +62,7 @@ func (l *lookupServicer) SetEnodebState(ctx context.Context, req *protos.SetEnod
}

func makeErr(err error, wrap string) error {
e := errors.Wrap(err, wrap)
e := fmt.Errorf(wrap+": %w", err)
code := codes.Internal
if err == merrors.ErrNotFound {
code = codes.NotFound
Expand Down
15 changes: 9 additions & 6 deletions lte/cloud/go/services/lte/storage/enodeb_state_lookup.go
Expand Up @@ -15,9 +15,9 @@ package storage

import (
"database/sql"
"fmt"

"github.com/Masterminds/squirrel"
"github.com/pkg/errors"

"magma/orc8r/cloud/go/sqorc"
"magma/orc8r/lib/go/merrors"
Expand Down Expand Up @@ -66,7 +66,10 @@ func (l *enodebStateLookup) Initialize() error {
Unique(nidCol, gidCol, enbSnCol, enbStateCol).
RunWith(tx).
Exec()
return nil, errors.Wrap(err, "initialize enodeb state lookup table")
if err != nil {
return nil, fmt.Errorf("initialize enodeb state lookup table: %w", err)
}
return nil, nil
}
_, err := sqorc.ExecInTx(l.db, nil, nil, txFn)
return err
Expand All @@ -81,22 +84,22 @@ func (l *enodebStateLookup) GetEnodebState(networkID string, gatewayID string, e
RunWith(tx).
Query()
if err != nil {
return nil, errors.Wrapf(err, "select EnodebState for gatewayID, enodebSN %v, %v", gatewayID, enodebSN)
return nil, fmt.Errorf("select EnodebState for gatewayID, enodebSN %v, %v: %w", gatewayID, enodebSN, err)
}
defer sqorc.CloseRowsLogOnError(rows, "GetEnodebState")

var serializedState []byte
for rows.Next() {
err = rows.Scan(&serializedState)
if err != nil {
return nil, errors.Wrap(err, "select EnodebState for (gatewayID, enodebSN), SQL row scan error")
return nil, fmt.Errorf("select EnodebState for (gatewayID, enodebSN), SQL row scan error: %w", err)
}
// always return the first record as all cols are unique
break
}
err = rows.Err()
if err != nil {
return nil, errors.Wrap(err, "select EnodebState for (gatewayID, enodebSN), SQL rows error")
return nil, fmt.Errorf("select EnodebState for (gatewayID, enodebSN), SQL rows error: %w", err)
}
return serializedState, nil
}
Expand Down Expand Up @@ -127,7 +130,7 @@ func (l *enodebStateLookup) SetEnodebState(networkID string, gatewayID string, e
RunWith(sc).
Exec()
if err != nil {
return nil, errors.Wrapf(err, "insert EnodbeState %+v", enodebState)
return nil, fmt.Errorf("insert EnodbeState %+v: %w", enodebState, err)
}
return nil, nil
}
Expand Down

0 comments on commit 9c4f6d7

Please sign in to comment.