Skip to content

Commit

Permalink
refactor(feg): 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/feg. Created in pairing
with Moritz Huebner.

Signed-off-by: Sebastian Wolf <sebastian.wolf@tngtech.com>
  • Loading branch information
wolfseb committed May 29, 2022
1 parent 3c29ea4 commit 72a6a7b
Show file tree
Hide file tree
Showing 35 changed files with 90 additions and 104 deletions.
2 changes: 1 addition & 1 deletion feg/cloud/go/go.mod
Expand Up @@ -30,7 +30,6 @@ require (
github.com/golang/protobuf v1.5.2
github.com/labstack/echo v3.3.10+incompatible
github.com/magma/augmented-networks/accounting/protos v0.1.1
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.5.1
github.com/stretchr/testify v1.7.1
google.golang.org/grpc v1.43.0
Expand Down Expand Up @@ -81,6 +80,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/olivere/elastic/v7 v7.0.6 // 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/common v0.9.1 // indirect
Expand Down
3 changes: 1 addition & 2 deletions feg/cloud/go/services/feg/obsidian/handlers/handlers.go
Expand Up @@ -18,7 +18,6 @@ import (
"net/http"

"github.com/labstack/echo"
"github.com/pkg/errors"

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

ret := &fegModels.FederationGateway{
Expand Down
7 changes: 3 additions & 4 deletions feg/cloud/go/services/feg/obsidian/models/validate.go
Expand Up @@ -20,7 +20,6 @@ import (

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

"magma/orc8r/cloud/go/services/configurator"
)
Expand All @@ -32,10 +31,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 @@ -49,7 +48,7 @@ func (m *FederatedNetworkConfigs) ValidateModel(ctx context.Context) error {
if !swag.IsZero(nid) {
exists, err := configurator.DoesNetworkExist(ctx, nid)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to search for network %s", nid))
return fmt.Errorf("Failed to search for network %s: %w", nid, err)
}
if !exists {
return fmt.Errorf("Network: %s does not exist", nid)
Expand Down
Expand Up @@ -19,7 +19,6 @@ import (

"github.com/go-openapi/swag"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"

"magma/feg/cloud/go/feg"
feg_mconfig "magma/feg/cloud/go/protos/mconfig"
Expand Down Expand Up @@ -175,7 +174,7 @@ func (s *builderServicer) Build(ctx context.Context, request *builder_protos.Bui
func getFegConfig(gatewayID string, network configurator.Network, graph configurator.EntityGraph) (*models.GatewayFederationConfigs, error) {
fegGW, err := graph.GetEntity(feg.FegGatewayType, gatewayID)
if err != nil && err != merrors.ErrNotFound {
return nil, errors.WithStack(err)
return nil, err
}
// err can only be merrors.ErrNotFound at this point - if it's nil, we'll
// just return the feg gateway config if it exists
Expand Down
3 changes: 1 addition & 2 deletions feg/cloud/go/services/feg_relay/utils/get_all_gateways.go
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/go-openapi/swag"
"github.com/golang/glog"
"github.com/pkg/errors"
"google.golang.org/grpc/metadata"

"magma/feg/cloud/go/feg"
Expand Down Expand Up @@ -93,7 +92,7 @@ func getFegCfg(ctx context.Context, networkID, gatewayID string) (*models.Gatewa
serdes.Entity,
)
if err != nil && err != merrors.ErrNotFound {
return nil, errors.WithStack(err)
return nil, err
}
if err == nil && fegGateway.Config != nil {
return fegGateway.Config.(*models.GatewayFederationConfigs), nil
Expand Down
2 changes: 1 addition & 1 deletion feg/gateway/go.mod
Expand Up @@ -38,7 +38,6 @@ require (
github.com/magma/milenage v1.0.2
github.com/mennanov/fieldmask-utils v0.5.0
github.com/ory/go-acc v0.2.8
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.26.0
Expand Down Expand Up @@ -122,6 +121,7 @@ require (
github.com/ory/viper v1.7.5 // indirect
github.com/pborman/uuid v1.2.0 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/spf13/afero v1.8.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions feg/gateway/services/s8_proxy/servicers/mock_pgw/cb.go
Expand Up @@ -14,10 +14,10 @@ limitations under the License.
package mock_pgw

import (
"errors"
"fmt"
"net"

"github.com/pkg/errors"
"github.com/wmnsk/go-gtp/gtpv2"
"github.com/wmnsk/go-gtp/gtpv2/ie"
"github.com/wmnsk/go-gtp/gtpv2/message"
Expand Down Expand Up @@ -52,7 +52,7 @@ func (mPgw *MockPgw) CreateBearerRequest(req CreateBearerRequest) (chan CBReq, e

sgwTeidC, err := session.GetTEID(gtpv2.IFTypeS5S8SGWGTPC)
if err != nil {
err = errors.Wrap(err, "Error, couldnt find teid con Create Bearer Request")
err = fmt.Errorf("Error, couldnt find teid con Create Bearer Request: %w", err)
return nil, err
}

Expand Down
5 changes: 2 additions & 3 deletions feg/gateway/services/s8_proxy/servicers/mock_pgw/cs.go
Expand Up @@ -19,7 +19,6 @@ import (
"net"
"strings"

"github.com/pkg/errors"
"github.com/wmnsk/go-gtp/gtpv2"
"github.com/wmnsk/go-gtp/gtpv2/ie"
"github.com/wmnsk/go-gtp/gtpv2/message"
Expand Down Expand Up @@ -50,7 +49,7 @@ func (mPgw *MockPgw) getHandleCreateSessionRequest() gtpv2.HandlerFunc {
case *gtpv2.UnknownIMSIError:
// whole new session. just ignore.
default:
return errors.Wrap(err2, "got something unexpected")
return fmt.Errorf("got something unexpected: %w", err2)
}
} else {
fmt.Printf("Existing IMSI during Create Session Request on PGW (%s). Deleting previous session\n", imsi)
Expand Down Expand Up @@ -409,7 +408,7 @@ func (mPgw *MockPgw) getHandleCreateSessionResponseWithMissingIE() gtpv2.Handler
csRspFromPGW := message.NewCreateSessionResponse(
sgwTEID.MustTEID(), msg.Sequence(),
ie.NewCause(gtpv2.CauseRequestAccepted, 0, 0, 0, nil),
//pgwFTEIDc,
// pgwFTEIDc,
ie.NewPDNAddressAllocation("10.1.2.3"),
ie.NewAPNRestriction(gtpv2.APNRestrictionPublic2),
ie.NewBearerContext(
Expand Down
4 changes: 2 additions & 2 deletions feg/gateway/services/s8_proxy/servicers/mock_pgw/db.go
Expand Up @@ -14,10 +14,10 @@ limitations under the License.
package mock_pgw

import (
"errors"
"fmt"
"net"

"github.com/pkg/errors"
"github.com/wmnsk/go-gtp/gtpv2"
"github.com/wmnsk/go-gtp/gtpv2/ie"
"github.com/wmnsk/go-gtp/gtpv2/message"
Expand All @@ -41,7 +41,7 @@ func (mPgw *MockPgw) DeleteBearerRequest(req DeleteBearerRequest) (chan DBReq, e

sgwTeidC, err := session.GetTEID(gtpv2.IFTypeS5S8SGWGTPC)
if err != nil {
err = errors.Wrap(err, "Error, couldnt find teid on Dedicated Bearer Request")
err = fmt.Errorf("Error, couldnt find teid on Dedicated Bearer Request: %w", err)
return nil, err
}

Expand Down
3 changes: 1 addition & 2 deletions feg/gateway/services/s8_proxy/servicers/mock_pgw/ds.go
Expand Up @@ -17,7 +17,6 @@ import (
"fmt"
"net"

"github.com/pkg/errors"
"github.com/wmnsk/go-gtp/gtpv2"
"github.com/wmnsk/go-gtp/gtpv2/ie"
"github.com/wmnsk/go-gtp/gtpv2/message"
Expand All @@ -42,7 +41,7 @@ func (mPgw *MockPgw) getHandleDeleteSessionRequest() gtpv2.HandlerFunc {
// get TEUD
sgwTeidC, err := session.GetTEID(gtpv2.IFTypeS5S8SGWGTPC)
if err != nil {
err = errors.Wrap(err, "Error")
err = fmt.Errorf("Error: %w", err)
return err
}

Expand Down
4 changes: 2 additions & 2 deletions feg/gateway/services/testcore/ocs/mock_ocs/ocs.go
Expand Up @@ -15,6 +15,7 @@ package mock_ocs

import (
"context"
"errors"
"fmt"
"net"
"time"
Expand All @@ -25,7 +26,6 @@ import (
"github.com/fiorix/go-diameter/v4/diam/sm"
"github.com/fiorix/go-diameter/v4/diam/sm/smpeer"
"github.com/golang/glog"
"github.com/pkg/errors"

"magma/feg/cloud/go/protos"
"magma/feg/gateway/diameter"
Expand Down Expand Up @@ -335,7 +335,7 @@ func (srv *OCSDiamServer) AbortSession(
srv.mux.Handle(diam.ASA, asaHandler)
err := sendASR(account.CurrentState, srv.mux.Settings())
if err != nil {
return nil, errors.Wrap(err, "Failed to send Gy ASR")
return nil, fmt.Errorf("Failed to send Gy ASR: %w", err)
}
select {
case asa := <-resp:
Expand Down
4 changes: 2 additions & 2 deletions feg/gateway/services/testcore/pcrf/mock_pcrf/pcrf.go
Expand Up @@ -15,6 +15,7 @@ package mock_pcrf

import (
"context"
"errors"
"fmt"
"net"
"time"
Expand All @@ -25,7 +26,6 @@ import (
"github.com/fiorix/go-diameter/v4/diam/sm"
"github.com/fiorix/go-diameter/v4/diam/sm/smpeer"
"github.com/golang/glog"
"github.com/pkg/errors"

"magma/feg/cloud/go/protos"
"magma/feg/gateway/diameter"
Expand Down Expand Up @@ -292,7 +292,7 @@ func (srv *PCRFServer) AbortSession(
srv.mux.Handle(diam.ASA, asaHandler)
err := sendASR(account.CurrentState, srv.mux.Settings())
if err != nil {
return nil, errors.Wrap(err, "Failed to send Gx ASR")
return nil, fmt.Errorf("Failed to send Gx ASR: %w", err)
}
select {
case asa := <-resp:
Expand Down
15 changes: 7 additions & 8 deletions feg/radius/lib/go/http/auth/mac.go
Expand Up @@ -19,6 +19,7 @@ import (
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"errors"
"fmt"
"hash"
"io"
Expand All @@ -29,8 +30,6 @@ import (
"time"

"fbc/lib/go/http/httputil"

"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -142,22 +141,22 @@ func (m *MACMiddleware) verify(r *http.Request) error {
}
n, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
return errors.Wrapf(err, "parse timestamp: %v", ts)
return fmt.Errorf("parse timestamp: %v: %w", ts, err)
}
if ts := time.Unix(n, 0); abs(time.Since(ts)) > m.TimeSkew {
return errors.Errorf("stable timestamp: %v", ts)
return fmt.Errorf("stable timestamp: %v", ts)
}
h := m.Hash.New()
if err := hashRequest(h, r, ts); err != nil {
return err
}
pk, err := m.KeyGetter()
if err != nil {
return errors.Wrap(err, "failed to get public key")
return fmt.Errorf("failed to get public key: %w", err)
}
sig, err := base64.StdEncoding.DecodeString(sg)
if err != nil {
return errors.Wrap(err, "failed to decode signature header")
return fmt.Errorf("failed to decode signature header: %w", err)
}
if err := rsa.VerifyPKCS1v15(pk, m.Hash, h.Sum(nil), sig); err != nil {
return errors.New("unauthorized")
Expand Down Expand Up @@ -198,12 +197,12 @@ func hashRequest(h hash.Hash, r *http.Request, ts string) error {
b.WriteString(r.URL.RequestURI())
b.WriteString(ts)
if _, err := io.Copy(h, b); err != nil {
return errors.Wrap(err, "failed to copy request info")
return fmt.Errorf("failed to copy request info: %w", err)
}
if r.Body != nil {
b.Reset()
if _, err := io.Copy(io.MultiWriter(h, b), r.Body); err != nil {
return errors.Wrap(err, "failed to copy request body")
return fmt.Errorf("failed to copy request body: %w", err)
}
r.Body = ioutil.NopCloser(b)
}
Expand Down
1 change: 0 additions & 1 deletion feg/radius/lib/go/http/go.mod
Expand Up @@ -11,7 +11,6 @@ require (
fbc/lib/go/log v0.0.0
github.com/google/uuid v1.1.1
github.com/justinas/alice v0.0.0-20171023064455-03f45bd4b7da
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0
go.opencensus.io v0.21.0
go.uber.org/multierr v1.1.0
Expand Down
1 change: 0 additions & 1 deletion feg/radius/lib/go/http/go.sum
Expand Up @@ -17,7 +17,6 @@ github.com/jessevdk/go-flags v1.4.1-0.20181221193153-c0795c8afcf4 h1:xKkUL6QBojw
github.com/justinas/alice v0.0.0-20171023064455-03f45bd4b7da h1:5y58+OCjoHCYB8182mpf/dEsq0vwTKPOo4zGfH0xW9A=
github.com/justinas/alice v0.0.0-20171023064455-03f45bd4b7da/go.mod h1:oLH0CmIaxCGXD67VKGR5AacGXZSMznlmeqM8RzPrcY8=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
7 changes: 3 additions & 4 deletions feg/radius/lib/go/http/server/config.go
Expand Up @@ -14,17 +14,16 @@ limitations under the License.
package server

import (
"fmt"
"io"
"net/http"

// register pprof with default http mux
_ "net/http/pprof"

"fbc/lib/go/http/middleware"
"fbc/lib/go/log"

"github.com/justinas/alice"
"github.com/pkg/errors"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -78,11 +77,11 @@ func (config *Config) createLogger() (*zap.Logger, error) {
}
logger, err = config.Build()
default:
err = errors.Errorf("unknown logger type: %q", lc.Type)
err = fmt.Errorf("unknown logger type: %q", lc.Type)
}

if err != nil {
return nil, errors.Wrap(err, "creating logger")
return nil, fmt.Errorf("creating logger: %w", err)
}

return logger.WithOptions(zap.AddStacktrace(zap.DPanicLevel)), nil
Expand Down
6 changes: 3 additions & 3 deletions feg/radius/lib/go/http/server/server.go
Expand Up @@ -15,6 +15,7 @@ package server

import (
"context"
"fmt"
"io"
"math"
"net"
Expand All @@ -30,7 +31,6 @@ import (
"fbc/lib/go/log"

"github.com/justinas/alice"
"github.com/pkg/errors"
"go.uber.org/multierr"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -105,7 +105,7 @@ func New(config Config, options ...Option) (*Server, error) {
srv := &Server{Server: http.Server{Addr: config.Addr}}
err := srv.Apply(options[:idx]...)
if err != nil {
return nil, errors.Wrap(err, "applying early options")
return nil, fmt.Errorf("applying early options: %w", err)
}

if srv.Logger == nil {
Expand All @@ -119,7 +119,7 @@ func New(config Config, options ...Option) (*Server, error) {
srv.Mux, srv.Handler = config.createServeMux(srv.Logger)
err = srv.Apply(options[idx:]...)
if err != nil {
return nil, errors.Wrap(err, "applying late options")
return nil, fmt.Errorf("applying late options: %w", err)
}

return srv, nil
Expand Down

0 comments on commit 72a6a7b

Please sign in to comment.