Skip to content

Commit

Permalink
refactor: Replace github.com/pkg/errors with errors (#218)
Browse files Browse the repository at this point in the history
Closes #217

Signed-off-by: Jack Chen <jack@iotechsys.com>
  • Loading branch information
jackchenjc authored Aug 14, 2023
1 parent dc3db31 commit 49917a2
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 58 deletions.
3 changes: 0 additions & 3 deletions Attribution.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ https://github.com/mitchellh/reflectwalk/blob/master/LICENSE
mitchellh/go-homedir (MIT) https://github.com/mitchellh/go-homedir
https://github.com/mitchellh/go-homedir/blob/master/LICENSE

pkg/errors (BSD-2) https://github.com/pkg/errors
https://github.com/pkg/errors/blob/master/LICENSE

fxamacker/cbor/v2 (MIT) https://github.com/fxamacker/cbor/v2
https://github.com/fxamacker/cbor/blob/master/LICENSE

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.1.0-dev.2
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
)

Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144T
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
16 changes: 8 additions & 8 deletions internal/inventory/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package inventoryapp

import (
"context"
"errors"
"fmt"
"io"
"os"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/edgexfoundry/app-functions-sdk-go/v3/pkg"
"github.com/edgexfoundry/app-functions-sdk-go/v3/pkg/interfaces"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -79,20 +79,20 @@ func (app *InventoryApp) Initialize() error {
app.lc.Info("Starting.")

if err = app.service.LoadCustomConfig(&app.config, customKey); err != nil {
return errors.Wrap(err, "failed to load custom configuration")
return fmt.Errorf("failed to load custom configuration: %w", err)
}

if err = app.config.AppCustom.AppSettings.Validate(); err != nil {
return errors.Wrap(err, "failed to validate custom config")
return fmt.Errorf("failed to validate custom config: %w", err)
}

if err = app.service.ListenForCustomConfigChanges(&app.config.AppCustom, customKey, app.processConfigUpdates); err != nil {
return errors.Wrap(err, "failed to listen for custom config changes")
return fmt.Errorf("failed to listen for custom config changes: %w", err)
}

app.publisher, err = app.service.AddBackgroundPublisher(1)
if err != nil {
return errors.Wrap(err, "failed to add BackgroundPublisher")
return fmt.Errorf("failed to add BackgroundPublisher: %w", err)
}

app.defaultGrp = llrp.NewReaderGroup()
Expand All @@ -105,7 +105,7 @@ func (app *InventoryApp) Initialize() error {

response, err := app.service.DeviceClient().DevicesByServiceName(context.Background(), dsName, 0, -1)
if err != nil {
return errors.Wrapf(err, "failed to get existing device names for device service name %s", dsName)
return fmt.Errorf("failed to get existing device names for device service name %s: %w", dsName, err)
}

app.lc.Debugf("Found %d devices", len(response.Devices))
Expand Down Expand Up @@ -163,11 +163,11 @@ func (app *InventoryApp) RunUntilCancelled() error {
err := app.service.SetDefaultFunctionsPipeline(
app.processEdgeXEvent)
if err != nil {
return errors.Wrap(err, "failed to build pipeline")
return fmt.Errorf("failed to build pipeline: %w", err)
}

if err = app.service.Run(); err != nil {
return errors.Wrap(err, "failed to run pipeline")
return fmt.Errorf("failed to run pipeline: %w", err)
}

// let task loop complete
Expand Down
6 changes: 3 additions & 3 deletions internal/inventory/app/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package inventoryapp
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand All @@ -24,7 +25,6 @@ import (

"github.com/edgexfoundry/app-functions-sdk-go/v3/pkg/interfaces"
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos"
"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -312,7 +312,7 @@ func (app *InventoryApp) publishEvents(events []inventory.Event) error {
addRequest := requests.NewAddEventRequest(edgeXEvent)
payload, err := json.Marshal(addRequest)
if err != nil {
return errors.Wrap(err, "unable to marshal inventory event(s) to publish")
return fmt.Errorf("unable to marshal inventory event(s) to publish: %w", err)
}

// Need a Context to have values for the placeholders in the configured topic
Expand All @@ -322,7 +322,7 @@ func (app *InventoryApp) publishEvents(events []inventory.Event) error {
context.AddValue(interfaces.SOURCENAME, edgeXEvent.SourceName)

if err := app.publisher.Publish(payload, context); err != nil {
return errors.Wrap(err, "unable to publish inventory event(s)")
return fmt.Errorf("unable to publish inventory event(s): %w", err)
}

return nil
Expand Down
3 changes: 1 addition & 2 deletions internal/inventory/app/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -62,7 +61,7 @@ func (app *InventoryApp) addRoutes() error {

func (app *InventoryApp) addRoute(path, method string, f http.HandlerFunc) error {
if err := app.service.AddRoute(path, f, method); err != nil {
return errors.Wrapf(err, "failed to add route, path=%s, method=%s", path, method)
return fmt.Errorf("failed to add route, path=%s, method=%s: %w", path, method, err)
}
return nil
}
Expand Down
9 changes: 5 additions & 4 deletions internal/inventory/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
package inventory

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

// ApplicationSettings is a struct that defines the ApplicationSettings section of the
Expand Down Expand Up @@ -78,15 +79,15 @@ func NewServiceConfig() ServiceConfig {
// or the first validation error it encounters.
func (as ApplicationSettings) Validate() error {
if as.DepartedThresholdSeconds == 0 {
return errors.Wrap(ErrOutOfRange, "DepartedThresholdSeconds must be >0")
return fmt.Errorf("DepartedThresholdSeconds must be >0: %w", ErrOutOfRange)
}

if as.DepartedCheckIntervalSeconds == 0 {
return errors.Wrap(ErrOutOfRange, "DepartedCheckIntervalSeconds must be >0")
return fmt.Errorf("DepartedCheckIntervalSeconds must be >0: %w", ErrOutOfRange)
}

if as.AgeOutHours == 0 {
return errors.Wrap(ErrOutOfRange, "AgeOutHours must be >0")
return fmt.Errorf("AgeOutHours must be >0: %w", ErrOutOfRange)
}

return nil
Expand Down
31 changes: 13 additions & 18 deletions internal/llrp/behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package llrp
import (
"bytes"
"fmt"
"github.com/pkg/errors"
"sort"
"strings"
)
Expand Down Expand Up @@ -76,10 +75,10 @@ var (

func errMissingCapInfo(name string, path ...string) error {
if len(path) != 0 {
return errors.Wrapf(ErrMissingCapInfo, "missing LLRP %s from %s",
name, strings.Join(path, "."))
return fmt.Errorf("missing LLRP %s from %s: %w",
name, strings.Join(path, "."), ErrMissingCapInfo)
}
return errors.Wrapf(ErrMissingCapInfo, "missing LLRP %s", name)
return fmt.Errorf("missing LLRP %s: %w", name, ErrMissingCapInfo)
}

// BasicDevice holds details of an LLRP device
Expand Down Expand Up @@ -474,9 +473,8 @@ func (d *BasicDevice) Transmit(b Behavior) (*RFTransmitter, error) {
// First, find the highest power at or below the Target.
pwrIdx, pwr := d.findPower(b.Power.Max)
if pwr > b.Power.Max {
return nil, errors.Wrapf(ErrUnsatisfiable,
"target power (%.2f dBm) is lower than the lowest supported (%.2f dBm)",
float32(b.Power.Max)/100.0, float32(pwr)/100.0)
return nil, fmt.Errorf("target power (%.2f dBm) is lower than the lowest supported (%.2f dBm): %w",
float32(b.Power.Max)/100.0, float32(pwr)/100.0, ErrUnsatisfiable)
}

// In hopping regulatory regions, we assume the power is legal for all frequencies.
Expand All @@ -499,9 +497,8 @@ func (d *BasicDevice) Transmit(b Behavior) (*RFTransmitter, error) {
}
}

return nil, errors.Wrapf(ErrUnsatisfiable,
"no frequency permits the desired power level (%.2f dBm)",
float32(b.Power.Max)/100.0)
return nil, fmt.Errorf("no frequency permits the desired power level (%.2f dBm): %w",
float32(b.Power.Max)/100.0, ErrUnsatisfiable)
}

// findPower returns the device's best match to a given power level,
Expand Down Expand Up @@ -664,9 +661,8 @@ type Environment struct {
func (d *BasicDevice) NewROSpec(b Behavior, e Environment) (*ROSpec, error) {
if b.GPITrigger != nil && (b.GPITrigger.Port == 0 ||
d.nGPIs == 0 || b.GPITrigger.Port > d.nGPIs) {
return nil, errors.Wrapf(ErrUnsatisfiable,
"behavior uses a GPI Trigger with invalid Port "+
"(%d not in [1, %d])", b.GPITrigger.Port, d.nGPIs)
return nil, fmt.Errorf("behavior uses a GPI Trigger with invalid Port "+
"(%d not in [1, %d]): %w", b.GPITrigger.Port, d.nGPIs, ErrUnsatisfiable)
}

transmit, err := d.Transmit(b)
Expand Down Expand Up @@ -850,9 +846,8 @@ func (d *BasicDevice) NewROSpec(b Behavior, e Environment) (*ROSpec, error) {
func (d *ImpinjDevice) NewROSpec(b Behavior, e Environment) (*ROSpec, error) {
if b.GPITrigger != nil && (b.GPITrigger.Port == 0 ||
d.nGPIs == 0 || b.GPITrigger.Port > d.nGPIs) {
return nil, errors.Wrapf(ErrUnsatisfiable,
"behavior uses a GPI Trigger with invalid Port "+
"(%d not in [1, %d])", b.GPITrigger.Port, d.nGPIs)
return nil, fmt.Errorf("behavior uses a GPI Trigger with invalid Port "+
"(%d not in [1, %d]): %w", b.GPITrigger.Port, d.nGPIs, ErrUnsatisfiable)
}

transmit, err := d.Transmit(b)
Expand Down Expand Up @@ -978,7 +973,7 @@ var (

func (s ScanType) MarshalText() ([]byte, error) {
if !(0 <= int(s) && int(s) < len(scanStrs)) {
return nil, errors.Errorf("unknown ScanType: %v", s)
return nil, fmt.Errorf("unknown ScanType: %v", s)
}
return scanStrs[s], nil
}
Expand All @@ -991,5 +986,5 @@ func (s *ScanType) UnmarshalText(text []byte) error {
}
}

return errors.Errorf("unknown ScanType: %q", string(text))
return fmt.Errorf("unknown ScanType: %q", string(text))
}
27 changes: 14 additions & 13 deletions internal/llrp/device_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ package llrp
import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"time"

"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/interfaces"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
"github.com/pkg/errors"
)

// These are the names of deviceResource and deviceCommands
Expand Down Expand Up @@ -62,7 +63,7 @@ func (ds DSClient) NewReader(device string) (TagReader, error) {
}

if devCap.GeneralDeviceCapabilities == nil {
return nil, errors.Errorf("missing general capabilities for %q", device)
return nil, fmt.Errorf("missing general capabilities for %q", device)
}

var tr TagReader
Expand Down Expand Up @@ -111,7 +112,7 @@ func (ds DSClient) GetCapabilities(device string) (*GetReaderCapabilitiesRespons
}

if err != nil {
return nil, errors.Wrap(err, "device info request failed")
return nil, fmt.Errorf("device info request failed: %w", err)
}

caps := &GetReaderCapabilitiesResponse{}
Expand All @@ -121,11 +122,11 @@ func (ds DSClient) GetCapabilities(device string) (*GetReaderCapabilitiesRespons
// in order to get this into the reader capabilities struct we need to first marshal it back to JSON
data, err := json.Marshal(reading.ObjectValue)
if err != nil {
return nil, errors.Wrap(err, "marshal failed for reading object value (reader capabilities)")
return nil, fmt.Errorf("marshal failed for reading object value (reader capabilities): %w", err)
}
err = json.Unmarshal(data, &caps)
if err != nil {
return nil, errors.Wrap(err, "unmarshal failed for reader capabilities")
return nil, fmt.Errorf("unmarshal failed for reader capabilities: %w", err)
}
break
}
Expand All @@ -142,13 +143,13 @@ func (ds DSClient) GetCapabilities(device string) (*GetReaderCapabilitiesRespons
func (ds DSClient) SetConfig(device string, conf *SetReaderConfig) error {
confData, err := json.Marshal(conf)
if err != nil {
return errors.Wrap(err, "failed to marshal SetReaderConfig message")
return fmt.Errorf("failed to marshal SetReaderConfig message: %w", err)
}

var configMapData map[string]interface{}
err = json.Unmarshal(confData, &configMapData)
if err != nil {
return errors.Wrap(err, "failed to unmarshal SetReaderConfig message")
return fmt.Errorf("failed to unmarshal SetReaderConfig message: %w", err)
}

commandData := map[string]interface{}{
Expand All @@ -159,7 +160,7 @@ func (ds DSClient) SetConfig(device string, conf *SetReaderConfig) error {

_, err = ds.cmdClient.IssueSetCommandByNameWithObject(context.Background(), device, configDevCmd, commandData)
if err != nil {
return errors.WithMessage(err, "failed to set reader config")
return fmt.Errorf("failed to set reader config: %v", err)
}
return nil
}
Expand All @@ -168,13 +169,13 @@ func (ds DSClient) SetConfig(device string, conf *SetReaderConfig) error {
func (ds DSClient) AddROSpec(device string, spec *ROSpec) error {
roData, err := json.Marshal(spec)
if err != nil {
return errors.Wrap(err, "failed to marshal ROSpec")
return fmt.Errorf("failed to marshal ROSpec: %w", err)
}

var roMapData map[string]interface{}
err = json.Unmarshal(roData, &roMapData)
if err != nil {
return errors.Wrap(err, "failed to unmarshal ROSpec")
return fmt.Errorf("failed to unmarshal ROSpec: %w", err)
}

commandData := map[string]interface{}{
Expand All @@ -185,7 +186,7 @@ func (ds DSClient) AddROSpec(device string, spec *ROSpec) error {

_, err = ds.cmdClient.IssueSetCommandByNameWithObject(context.Background(), device, addCmd, commandData)
if err != nil {
return errors.WithMessage(err, "failed to add ROSpec")
return fmt.Errorf("failed to add ROSpec: %v", err)
}
return nil
}
Expand Down Expand Up @@ -230,7 +231,7 @@ func (ds DSClient) modifyROSpecState(roCmd, device string, id uint32) error {

_, err := ds.cmdClient.IssueSetCommandByName(context.Background(), device, roCmd, data)
if err != nil {
return errors.WithMessage(err, "failed to "+roCmd)
return fmt.Errorf("failed to "+roCmd+": %v", err)
}

return nil
Expand All @@ -247,7 +248,7 @@ func (d *ImpinjDevice) EnableCustomExt(device string, ds DSClient) error {
// the Device profile for impinj has a defaultvalue, so passing empty map
_, err := ds.cmdClient.IssueSetCommandByName(context.Background(), device, enableImpinjCmd, data)
if err != nil {
return errors.WithMessage(err, "failed to enable Impinj extensions")
return fmt.Errorf("failed to enable Impinj extensions: %v", err)
}

return nil
Expand Down
Loading

0 comments on commit 49917a2

Please sign in to comment.