Skip to content

Commit

Permalink
Used errors.New instead of fmt.Errorf
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrytfleung committed Jan 17, 2024
1 parent e34cd3e commit 058dce4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
15 changes: 7 additions & 8 deletions extension/solarwindsapmsettingsextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package solarwindsapmsettingsextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/solarwindsapmsettingsextension"

import (
"fmt"
"errors"
"strconv"
"strings"
"time"
Expand All @@ -18,25 +18,24 @@ type Config struct {

func (cfg *Config) Validate() error {
if len(cfg.Endpoint) == 0 {
return fmt.Errorf("endpoint must not be empty")
return errors.New("endpoint must not be empty")
}
endpointArr := strings.Split(cfg.Endpoint, ":")
if len(endpointArr) != 2 {
return fmt.Errorf("endpoint should be in \"<host>:<port>\" format")
return errors.New("endpoint should be in \"<host>:<port>\" format")
}
if _, err := strconv.Atoi(endpointArr[1]); err != nil {
return fmt.Errorf("the <port> portion of endpoint has to be an integer")
return errors.New("the <port> portion of endpoint has to be an integer")
}
if len(cfg.Key) == 0 {
return fmt.Errorf("key must not be empty")
return errors.New("key must not be empty")
}
keyArr := strings.Split(cfg.Key, ":")
if len(keyArr) != 2 {
return fmt.Errorf("key should be in \"<token>:<service_name>\" format")
return errors.New("key should be in \"<token>:<service_name>\" format")
}

if _, err := time.ParseDuration(cfg.Interval); err != nil {
return fmt.Errorf("interval has to be a duration string. Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"")
return errors.New("interval has to be a duration string. Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"")
}
return nil
}
12 changes: 6 additions & 6 deletions extension/solarwindsapmsettingsextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package solarwindsapmsettingsextension

import (
"fmt"
"errors"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -19,38 +19,38 @@ func TestValidate(t *testing.T) {
{
name: "nothing",
cfg: &Config{},
err: fmt.Errorf("endpoint must not be empty"),
err: errors.New("endpoint must not be empty"),
},
{
name: "empty key",
cfg: &Config{
Endpoint: "host:12345",
},
err: fmt.Errorf("key must not be empty"),
err: errors.New("key must not be empty"),
},
{
name: "invalid endpoint",
cfg: &Config{
Endpoint: "invalid",
Key: "token:name",
},
err: fmt.Errorf("endpoint should be in \"<host>:<port>\" format"),
err: errors.New("endpoint should be in \"<host>:<port>\" format"),
},
{
name: "invalid endpoint format but port is not an integer",
cfg: &Config{
Endpoint: "host:abc",
Key: "token:name",
},
err: fmt.Errorf("the <port> portion of endpoint has to be an integer"),
err: errors.New("the <port> portion of endpoint has to be an integer"),
},
{
name: "invalid key",
cfg: &Config{
Endpoint: "host:12345",
Key: "invalid",
},
err: fmt.Errorf("key should be in \"<token>:<service_name>\" format"),
err: errors.New("key should be in \"<token>:<service_name>\" format"),
},
}
for _, tc := range tests {
Expand Down

0 comments on commit 058dce4

Please sign in to comment.