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.Errorf` 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 5908b71 commit ca1c648
Show file tree
Hide file tree
Showing 40 changed files with 264 additions and 276 deletions.
2 changes: 1 addition & 1 deletion lte/cloud/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ require (
github.com/labstack/echo v3.3.10+incompatible
github.com/lib/pq v1.2.0
github.com/olivere/elastic/v7 v7.0.6
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.5.1
github.com/prometheus/common v0.9.1
github.com/stretchr/testify v1.7.0
Expand Down Expand Up @@ -92,6 +91,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/procfs v0.0.8 // indirect
Expand Down
4 changes: 1 addition & 3 deletions lte/cloud/go/services/ha/servicers/southbound/servicer.go
Original file line number Diff line number Diff line change
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,8 +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")
return ret, err
return ret, fmt.Errorf("unable to load cellular gateway configs to find primary gateway's in its pool: %w", err)
}
cellularCfg, ok := cfg.(*lte_models.GatewayCellularConfigs)
if !ok {
Expand Down
19 changes: 9 additions & 10 deletions lte/cloud/go/services/lte/obsidian/handlers/handlers.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
43 changes: 21 additions & 22 deletions lte/cloud/go/services/lte/obsidian/models/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"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"
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
16 changes: 8 additions & 8 deletions lte/cloud/go/services/lte/protos/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,34 @@
package protos

import (
"github.com/pkg/errors"
"fmt"
)

func (m *GetEnodebStateRequest) Validate() error {
if m.NetworkId == "" {
return errors.New("network ID cannot be empty")
return fmt.Errorf("network ID cannot be empty")
}
if m.GatewayId == "" {
return errors.New("gateway ID cannot be empty")
return fmt.Errorf("gateway ID cannot be empty")
}
if m.EnodebSn == "" {
return errors.New("enodeb SN cannot be empty")
return fmt.Errorf("enodeb SN cannot be empty")
}
return nil
}

func (m *SetEnodebStateRequest) Validate() error {
if m.NetworkId == "" {
return errors.New("network ID cannot be empty")
return fmt.Errorf("network ID cannot be empty")
}
if m.GatewayId == "" {
return errors.New("gateway ID cannot be empty")
return fmt.Errorf("gateway ID cannot be empty")
}
if m.EnodebSn == "" {
return errors.New("enodeb SN cannot be empty")
return fmt.Errorf("enodeb SN cannot be empty")
}
if len(m.SerializedState) == 0 {
return errors.New("serialized enodeb state cannot be empty")
return fmt.Errorf("serialized enodeb state cannot be empty")
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/go-openapi/swag"
"github.com/golang/glog"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"github.com/thoas/go-funk"

"magma/feg/cloud/go/feg"
Expand Down Expand Up @@ -248,23 +247,23 @@ func (s *builderServicer) Build(ctx context.Context, request *builder_protos.Bui

func validateConfigs(nwConfig *lte_models.NetworkCellularConfigs, gwConfig *lte_models.GatewayCellularConfigs) error {
if nwConfig == nil {
return errors.New("Cellular network config is nil")
return fmt.Errorf("Cellular network config is nil")
}
if gwConfig == nil {
return errors.New("Cellular gateway config is nil")
return fmt.Errorf("Cellular gateway config is nil")
}

if gwConfig.Ran == nil {
return errors.New("Gateway RAN config is nil")
return fmt.Errorf("Gateway RAN config is nil")
}
if gwConfig.Epc == nil {
return errors.New("Gateway EPC config is nil")
return fmt.Errorf("Gateway EPC config is nil")
}
if nwConfig.Ran == nil {
return errors.New("Network RAN config is nil")
return fmt.Errorf("Network RAN config is nil")
}
if nwConfig.Epc == nil {
return errors.New("Network EPC config is nil")
return fmt.Errorf("Network EPC config is nil")
}
return nil
}
Expand Down Expand Up @@ -322,7 +321,7 @@ func getPipelineDServicesConfig(networkServices []string) ([]lte_mconfig.Pipelin
for _, service := range networkServices {
mc, found := networkServicesByName[service]
if !found {
return nil, errors.Errorf("unknown network service name %s", service)
return nil, fmt.Errorf("unknown network service name %s", service)
}
apps = append(apps, mc)
}
Expand Down
Original file line number Diff line number Diff line change
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

0 comments on commit ca1c648

Please sign in to comment.