Skip to content

Commit

Permalink
Merge pull request #129 from orozery/nit
Browse files Browse the repository at this point in the history
Wrap errors correctly
  • Loading branch information
orozery authored Oct 30, 2023
2 parents f061dd2 + 298d6ad commit 9186707
Show file tree
Hide file tree
Showing 23 changed files with 78 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ linters:
- durationcheck
- errcheck
- errname
# - errorlint
- errorlint
- exportloopref
- ginkgolinter
- gocritic
Expand Down
2 changes: 1 addition & 1 deletion cmd/cl-adm/cmd/create/create_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (o *PeerOptions) createGWCTL() error {
// Run the 'create peer' subcommand.
func (o *PeerOptions) Run() error {
if _, err := idna.Lookup.ToASCII(o.Name); err != nil {
return fmt.Errorf("peer name is not a valid DNS name: %v", err)
return fmt.Errorf("peer name is not a valid DNS name: %w", err)
}

if err := verifyNotExists(o.Name); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cl-adm/templates/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func CreateDockerRunScripts(args map[string]interface{}, outDir string) error {
var dockerRunScript bytes.Buffer
t := template.Must(template.New("").Parse(dockerRunTemplate))
if err := t.Execute(&dockerRunScript, args); err != nil {
return fmt.Errorf("cannot create docker run script off template: %v", err)
return fmt.Errorf("cannot create docker run script off template: %w", err)
}

outPath := filepath.Join(outDir, config.DockerRunFile)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cl-adm/templates/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func CreateK8SConfig(args map[string]interface{}, outDir string) error {
var k8sConfig bytes.Buffer
t := template.Must(template.New("").Parse(k8sTemplate))
if err := t.Execute(&k8sConfig, args); err != nil {
return fmt.Errorf("cannot create k8s configuration off template: %v", err)
return fmt.Errorf("cannot create k8s configuration off template: %w", err)
}

outPath := filepath.Join(outDir, config.K8SYamlFile)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cl-dataplane/app/envoy.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (o *Options) runEnvoy(peerName, dataplaneID string) error {
var envoyConf bytes.Buffer
t := template.Must(template.New("").Parse(envoyConfigurationTemplate))
if err := t.Execute(&envoyConf, envoyConfArgs); err != nil {
return fmt.Errorf("cannot create Envoy configuration off template: %v", err)
return fmt.Errorf("cannot create Envoy configuration off template: %w", err)
}

args := []string{
Expand Down
4 changes: 2 additions & 2 deletions cmd/cl-dataplane/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (o *Options) Run() error {
if o.LogFile != "" {
f, err := os.OpenFile(o.LogFile, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return fmt.Errorf("unable to open log file: %v", err)
return fmt.Errorf("unable to open log file: %w", err)
}

defer func() {
Expand All @@ -84,7 +84,7 @@ func (o *Options) Run() error {
// set log level
logLevel, err := log.ParseLevel(o.LogLevel)
if err != nil {
return fmt.Errorf("unable to set log level: %v", err)
return fmt.Errorf("unable to set log level: %w", err)
}
log.SetLevel(logLevel)

Expand Down
6 changes: 3 additions & 3 deletions cmd/cl-go-dataplane/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (o *Options) runGoDataplane(peerName, dataplaneID string, parsedCertData *u
tlsConfig := parsedCertData.ClientConfig(cpapi.GRPCServerName(peerName))
xdsClient := dpclient.NewXDSClient(dataplane, controlplaneTarget, tlsConfig)
err := xdsClient.Run()
return fmt.Errorf("xDS Client stopped: %v", err)
return fmt.Errorf("xDS Client stopped: %w", err)
}

// Run the dataplane.
Expand All @@ -101,7 +101,7 @@ func (o *Options) Run() error {
if o.LogFile != "" {
f, err := os.OpenFile(o.LogFile, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return fmt.Errorf("unable to open log file: %v", err)
return fmt.Errorf("unable to open log file: %w", err)
}

defer func() {
Expand All @@ -116,7 +116,7 @@ func (o *Options) Run() error {
// set log level
logLevel, err := log.ParseLevel(o.LogLevel)
if err != nil {
return fmt.Errorf("unable to set log level: %v", err)
return fmt.Errorf("unable to set log level: %w", err)
}
log.SetLevel(logLevel)

Expand Down
6 changes: 3 additions & 3 deletions cmd/gwctl/subcommand/gwctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (o *stateGetOptions) run() error {

sJSON, err := json.MarshalIndent(d, "", " ")
if err != nil {
return fmt.Errorf("error: %v", err.Error())
return fmt.Errorf("error: %w", err)
}

fmt.Println(string(sJSON))
Expand Down Expand Up @@ -166,11 +166,11 @@ func (o *allGetOptions) run() error {
fmt.Printf("%s:\n", name)
d, err := o.List()
if err != nil {
return fmt.Errorf("error: %v", err.Error())
return fmt.Errorf("error: %w", err)
}
sJSON, err := json.Marshal(d)
if err != nil {
return fmt.Errorf("error: %v", err.Error())
return fmt.Errorf("error: %w", err)
}
fmt.Println(string(sJSON))
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controlplane/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (cp *Instance) AuthorizeEgress(req *EgressAuthorizationRequest) (*EgressAut

serverResp, err := client.Authorize(&api.AuthorizationRequest{Service: req.Import})
if err != nil {
return nil, fmt.Errorf("unable to get access token from peer: %v", err)
return nil, fmt.Errorf("unable to get access token from peer: %w", err)
}

resp := &EgressAuthorizationResponse{
Expand Down Expand Up @@ -152,13 +152,13 @@ func (cp *Instance) AuthorizeIngress(req *IngressAuthorizationRequest) (*Ingress
Claim(api.ExportNameJWTClaim, export.Name).
Build()
if err != nil {
return nil, fmt.Errorf("unable to generate access token: %v", err)
return nil, fmt.Errorf("unable to generate access token: %w", err)
}

// sign access token
signed, err := jwt.Sign(token, jwtSignatureAlgorithm, cp.jwkSignKey)
if err != nil {
return nil, fmt.Errorf("unable to sign access token: %v", err)
return nil, fmt.Errorf("unable to sign access token: %w", err)
}
resp.AccessToken = string(signed)

Expand Down
4 changes: 2 additions & 2 deletions pkg/controlplane/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type remoteServerAuthorizationResponse struct {
func (c *client) Authorize(req *api.AuthorizationRequest) (*remoteServerAuthorizationResponse, error) {
body, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("unable to serialize authorization request: %v", err)
return nil, fmt.Errorf("unable to serialize authorization request: %w", err)
}

var serverResp *jsonapi.Response
Expand Down Expand Up @@ -96,7 +96,7 @@ func (c *client) Authorize(req *api.AuthorizationRequest) (*remoteServerAuthoriz

var authResp api.AuthorizationResponse
if err := json.Unmarshal(serverResp.Body, &authResp); err != nil {
return nil, fmt.Errorf("unable to parse server response: %v", err)
return nil, fmt.Errorf("unable to parse server response: %w", err)
}

resp.Allowed = true
Expand Down
18 changes: 9 additions & 9 deletions pkg/controlplane/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (cp *Instance) CreateImport(imp *cpstore.Import) error {

port, err := cp.ports.Lease(imp.Port)
if err != nil {
return fmt.Errorf("cannot generate listening port: %v", err)
return fmt.Errorf("cannot generate listening port: %w", err)
}

imp.Port = port
Expand Down Expand Up @@ -534,7 +534,7 @@ func (cp *Instance) GetXDSListenerManager() cache.Cache {
func (cp *Instance) init() error {
// generate the JWK key
if err := cp.generateJWK(); err != nil {
return fmt.Errorf("unable to generate JWK key: %v", err)
return fmt.Errorf("unable to generate JWK key: %w", err)
}

// add peers
Expand Down Expand Up @@ -591,17 +591,17 @@ func (cp *Instance) generateJWK() error {
// generate RSA key-pair
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return fmt.Errorf("unable to generate RSA keys: %v", err)
return fmt.Errorf("unable to generate RSA keys: %w", err)
}

jwkSignKey, err := jwk.New(rsaKey)
if err != nil {
return fmt.Errorf("unable to create JWK signing key: %v", err)
return fmt.Errorf("unable to create JWK signing key: %w", err)
}

jwkVerifyKey, err := jwk.New(rsaKey.PublicKey)
if err != nil {
return fmt.Errorf("unable to create JWK verifing key: %v", err)
return fmt.Errorf("unable to create JWK verifing key: %w", err)
}

cp.jwkSignKey = jwkSignKey
Expand All @@ -615,19 +615,19 @@ func NewInstance(peerTLS *util.ParsedCertData, storeManager store.Manager, platf

peers, err := cpstore.NewPeers(storeManager)
if err != nil {
return nil, fmt.Errorf("cannot load peers from store: %v", err)
return nil, fmt.Errorf("cannot load peers from store: %w", err)
}
logger.Infof("Loaded %d peers.", peers.Len())

exports, err := cpstore.NewExports(storeManager)
if err != nil {
return nil, fmt.Errorf("cannot load exports from store: %v", err)
return nil, fmt.Errorf("cannot load exports from store: %w", err)
}
logger.Infof("Loaded %d exports.", exports.Len())

imports, err := cpstore.NewImports(storeManager)
if err != nil {
return nil, fmt.Errorf("cannot load imports from store: %v", err)
return nil, fmt.Errorf("cannot load imports from store: %w", err)
}
logger.Infof("Loaded %d imports.", imports.Len())

Expand All @@ -639,7 +639,7 @@ func NewInstance(peerTLS *util.ParsedCertData, storeManager store.Manager, platf

acPolicies, err := cpstore.NewAccessPolicies(storeManager)
if err != nil {
return nil, fmt.Errorf("cannot load access policies from store: %v", err)
return nil, fmt.Errorf("cannot load access policies from store: %w", err)
}
logger.Infof("Loaded %d access policies.", acPolicies.Len())

Expand Down
8 changes: 4 additions & 4 deletions pkg/controlplane/server/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c *Controller) Run() error {
// start server listeners
for listenAddress, server := range c.servers {
if err := server.Listen(listenAddress); err != nil {
return fmt.Errorf("unable to create listener for server '%s' on %s: %v",
return fmt.Errorf("unable to create listener for server '%s' on %s: %w",
server.Name(), listenAddress, err)
}
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func (c *Controller) Run() error {
for server, err := range c.errors {
if err != nil {
errs = append(errs, fmt.Errorf(
"error running server '%s': %v", server.Name(), err))
"error running server '%s': %w", server.Name(), err))
}
}
return errors.Join(errs...)
Expand All @@ -135,7 +135,7 @@ func (c *Controller) Stop() error {
for _, server := range c.servers {
if err := server.Stop(); err != nil {
errs = append(errs, fmt.Errorf(
"unable to stop server '%s': %v", server.Name(), err))
"unable to stop server '%s': %w", server.Name(), err))
}
}

Expand All @@ -150,7 +150,7 @@ func (c *Controller) GracefulStop() error {
for _, server := range c.servers {
if err := server.GracefulStop(); err != nil {
errs = append(errs, fmt.Errorf(
"unable to gracefully stop server '%s': %v", server.Name(), err))
"unable to gracefully stop server '%s': %w", server.Name(), err))
}
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/controlplane/server/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type peerHandler struct {
func (h *peerHandler) Decode(data []byte) (any, error) {
var peer api.Peer
if err := json.Unmarshal(data, &peer); err != nil {
return nil, fmt.Errorf("cannot decode peer: %v", err)
return nil, fmt.Errorf("cannot decode peer: %w", err)
}

if peer.Name == "" {
Expand Down Expand Up @@ -140,7 +140,7 @@ type exportHandler struct {
func (h *exportHandler) Decode(data []byte) (any, error) {
var export api.Export
if err := json.Unmarshal(data, &export); err != nil {
return nil, fmt.Errorf("cannot decode export: %v", err)
return nil, fmt.Errorf("cannot decode export: %w", err)
}

if export.Name == "" {
Expand Down Expand Up @@ -207,7 +207,7 @@ type importHandler struct {
func (h *importHandler) Decode(data []byte) (any, error) {
var imp api.Import
if err := json.Unmarshal(data, &imp); err != nil {
return nil, fmt.Errorf("cannot decode import: %v", err)
return nil, fmt.Errorf("cannot decode import: %w", err)
}

if imp.Name == "" {
Expand Down Expand Up @@ -280,7 +280,7 @@ type bindingHandler struct {
func (h *bindingHandler) Decode(data []byte) (any, error) {
var binding api.Binding
if err := json.Unmarshal(data, &binding); err != nil {
return nil, fmt.Errorf("cannot decode binding: %v", err)
return nil, fmt.Errorf("cannot decode binding: %w", err)
}

if binding.Spec.Import == "" {
Expand Down Expand Up @@ -335,7 +335,7 @@ type accessPolicyHandler struct {
func (h *accessPolicyHandler) Decode(data []byte) (any, error) {
var policy api.Policy
if err := json.Unmarshal(data, &policy); err != nil {
return nil, fmt.Errorf("cannot decode access policy: %v", err)
return nil, fmt.Errorf("cannot decode access policy: %w", err)
}

if len(policy.Spec.Blob) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/dataplane/client/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (x *XDSClient) Run() error {
for resource, err := range x.errors {
if err != nil {
errs = append(errs, fmt.Errorf(
"error running fetcher '%s': %v", resource, err))
"error running fetcher '%s': %w", resource, err))
}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/dataplane/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (d *Dataplane) StartSNIServer(dataplaneServerAddress string) error {
d.logger.Infof("SNI proxy starting at %s.", dataplaneListenAddress)
err := sniProxy.Listen(dataplaneListenAddress)
if err != nil {
return fmt.Errorf("unable to create listener for server on %s: %v",
return fmt.Errorf("unable to create listener for server on %s: %w",
dataplaneListenAddress, err)
}
return sniProxy.Serve()
Expand Down Expand Up @@ -129,16 +129,16 @@ func (d *Dataplane) hijackConn(w http.ResponseWriter) (net.Conn, error) {
// Hijack the connection
peerConn, _, err := hj.Hijack()
if err != nil {
return nil, fmt.Errorf("hijacking failed: %v", err)
return nil, fmt.Errorf("hijacking failed: %w", err)
}

if err = peerConn.SetDeadline(time.Time{}); err != nil {
return nil, fmt.Errorf("failed to clear deadlines on connection: %v", err)
return nil, fmt.Errorf("failed to clear deadlines on connection: %w", err)
}

if _, err := peerConn.Write([]byte{}); err != nil {
_ = peerConn.Close() // close the connection ignoring errors
return nil, fmt.Errorf("failed to write to connection: %v", err)
return nil, fmt.Errorf("failed to write to connection: %w", err)
}

fmt.Fprintf(peerConn, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n")
Expand Down
4 changes: 2 additions & 2 deletions pkg/store/kv/bolt/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func Open(path string) (*Store, error) {
// open
db, err := bbolt.Open(path, 0666, nil)
if err != nil {
return nil, fmt.Errorf("unable to open store: %v", err)
return nil, fmt.Errorf("unable to open store: %w", err)
}

// create the single bucket we use (if does not exist)
Expand All @@ -115,7 +115,7 @@ func Open(path string) (*Store, error) {
return err
})
if err != nil {
return nil, fmt.Errorf("unable to create bucket: %v", err)
return nil, fmt.Errorf("unable to create bucket: %w", err)
}

return &Store{
Expand Down
8 changes: 4 additions & 4 deletions pkg/store/kv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (s *ObjectStore) Create(name string, value any) error {
// serialize
encoded, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("unable to serialize object: %v", err)
return fmt.Errorf("unable to serialize object: %w", err)
}

// persist to store
Expand All @@ -69,13 +69,13 @@ func (s *ObjectStore) Update(name string, mutator func(any) any) error {
// de-serialize old value
decoded := reflect.New(s.objectType).Interface()
if err := json.Unmarshal(value, decoded); err != nil {
return nil, fmt.Errorf("unable to decode value for object '%s': %v", name, err)
return nil, fmt.Errorf("unable to decode value for object '%s': %w", name, err)
}

// serialize mutated value
encoded, err := json.Marshal(mutator(decoded))
if err != nil {
return nil, fmt.Errorf("unable to serialize mutated object '%s': %v", name, err)
return nil, fmt.Errorf("unable to serialize mutated object '%s': %w", name, err)
}

return encoded, nil
Expand Down Expand Up @@ -106,7 +106,7 @@ func (s *ObjectStore) GetAll() ([]any, error) {

decoded := reflect.New(s.objectType).Interface()
if err := json.Unmarshal(value, decoded); err != nil {
return fmt.Errorf("unable to decode object for key %v: %v", key, err)
return fmt.Errorf("unable to decode object for key %v: %w", key, err)
}

objects = append(objects, decoded)
Expand Down
Loading

0 comments on commit 9186707

Please sign in to comment.