Skip to content

Commit

Permalink
used github.com/pkg/errors for http and runtime package (dapr#2078)
Browse files Browse the repository at this point in the history
refers dapr#1954

Co-authored-by: Young Bu Park <[email protected]>
Co-authored-by: Yaron Schneider <[email protected]>
  • Loading branch information
3 people authored Sep 22, 2020
1 parent 2a967a3 commit aeb79e0
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pkg/grpc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ func (a *api) ExecuteStateTransaction(ctx context.Context, in *runtimev1pb.Execu
})

if err != nil {
err = fmt.Errorf("ERR_STATE_TRANSACTION: %s", err)
err = errors.Wrap(err, "ERR_STATE_TRANSACTION")
apiServerLogger.Debug(err)
return &empty.Empty{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/http/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
Expand All @@ -35,6 +34,7 @@ import (
daprt "github.com/dapr/dapr/pkg/testing"
routing "github.com/fasthttp/router"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/valyala/fasthttp"
Expand Down
12 changes: 6 additions & 6 deletions pkg/runtime/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,37 @@ func FromFlags() (*DaprRuntime, error) {

daprHTTP, err := strconv.Atoi(*daprHTTPPort)
if err != nil {
return nil, fmt.Errorf("error parsing dapr-http-port flag: %s", err)
return nil, errors.Wrap(err, "error parsing dapr-http-port flag")
}

daprAPIGRPC, err := strconv.Atoi(*daprAPIGRPCPort)
if err != nil {
return nil, fmt.Errorf("error parsing dapr-grpc-port flag: %s", err)
return nil, errors.Wrap(err, "error parsing dapr-grpc-port flag")
}

profPort, err := strconv.Atoi(*profilePort)
if err != nil {
return nil, fmt.Errorf("error parsing profile-port flag: %s", err)
return nil, errors.Wrap(err, "error parsing profile-port flag")
}

var daprInternalGRPC int
if *daprInternalGRPCPort != "" {
daprInternalGRPC, err = strconv.Atoi(*daprInternalGRPCPort)
if err != nil {
return nil, fmt.Errorf("error parsing dapr-internal-grpc-port: %s", err)
return nil, errors.Wrap(err, "error parsing dapr-internal-grpc-port")
}
} else {
daprInternalGRPC, err = grpc.GetFreePort()
if err != nil {
return nil, fmt.Errorf("failed to get free port for internal grpc server: %s", err)
return nil, errors.Wrap(err, "failed to get free port for internal grpc server")
}
}

var applicationPort int
if *appPort != "" {
applicationPort, err = strconv.Atoi(*appPort)
if err != nil {
return nil, fmt.Errorf("error parsing app-port: %s", err)
return nil, errors.Wrap(err, "error parsing app-port")
}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package runtime

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -33,6 +32,7 @@ import (
daprt "github.com/dapr/dapr/pkg/testing"
"github.com/ghodss/yaml"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -1073,7 +1073,7 @@ func TestOnNewPublishedMessage(t *testing.T) {
var cloudEvent pubsub.CloudEventsEnvelope
json := jsoniter.ConfigFastest
json.Unmarshal(testPubSubMessage.Data, &cloudEvent)
expectedClientError := fmt.Errorf("RETRY status returned from app while processing pub/sub event %v", cloudEvent.ID)
expectedClientError := errors.Errorf("RETRY status returned from app while processing pub/sub event %v", cloudEvent.ID)
assert.Equal(t, expectedClientError.Error(), err.Error())
mockAppChannel.AssertNumberOfCalls(t, "InvokeMethod", 1)
})
Expand Down Expand Up @@ -1113,7 +1113,7 @@ func TestOnNewPublishedMessage(t *testing.T) {
var cloudEvent pubsub.CloudEventsEnvelope
json := jsoniter.ConfigFastest
json.Unmarshal(testPubSubMessage.Data, &cloudEvent)
expectedClientError := fmt.Errorf("unknown status returned from app while processing pub/sub event %v: not_valid", cloudEvent.ID)
expectedClientError := errors.Errorf("unknown status returned from app while processing pub/sub event %v: not_valid", cloudEvent.ID)
assert.Equal(t, expectedClientError.Error(), err.Error())
mockAppChannel.AssertNumberOfCalls(t, "InvokeMethod", 1)
})
Expand All @@ -1135,7 +1135,7 @@ func TestOnNewPublishedMessage(t *testing.T) {
var cloudEvent pubsub.CloudEventsEnvelope
json := jsoniter.ConfigFastest
json.Unmarshal(testPubSubMessage.Data, &cloudEvent)
expectedClientError := fmt.Errorf("retriable error returned from app while processing pub/sub event %v: Internal Error. status code returned: 500", cloudEvent.ID)
expectedClientError := errors.Errorf("retriable error returned from app while processing pub/sub event %v: Internal Error. status code returned: 500", cloudEvent.ID)
assert.Equal(t, expectedClientError.Error(), err.Error())
mockAppChannel.AssertNumberOfCalls(t, "InvokeMethod", 1)
})
Expand Down
3 changes: 1 addition & 2 deletions pkg/runtime/security/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"sync"
Expand Down Expand Up @@ -86,7 +85,7 @@ func (a *authenticator) CreateSignedWorkloadCert(id string) (*SignedCertificate,

config, err := dapr_credentials.TLSConfigFromCertAndKey(a.certChainPem, a.keyPem, TLSServerName, a.trustAnchors)
if err != nil {
return nil, fmt.Errorf("failed to create tls config from cert and key: %s", err)
return nil, errors.Wrap(err, "failed to create tls config from cert and key")
}

unaryClientInterceptor := grpc_retry.UnaryClientInterceptor()
Expand Down
13 changes: 6 additions & 7 deletions pkg/runtime/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"os"

"github.com/dapr/dapr/pkg/credentials"
diag "github.com/dapr/dapr/pkg/diagnostics"
"github.com/dapr/dapr/pkg/logger"
"github.com/dapr/dapr/pkg/sentry/certs"
"github.com/pkg/errors"
)

const (
Expand All @@ -33,15 +32,15 @@ func CertPool(certPem []byte) (*x509.CertPool, error) {
func GetCertChain() (*credentials.CertChain, error) {
trustAnchors := os.Getenv(certs.TrustAnchorsEnvVar)
if trustAnchors == "" {
return nil, fmt.Errorf("couldn't find trust anchors in environment variable %s", certs.TrustAnchorsEnvVar)
return nil, errors.Errorf("couldn't find trust anchors in environment variable %s", certs.TrustAnchorsEnvVar)
}
cert := os.Getenv(certs.CertChainEnvVar)
if cert == "" {
return nil, fmt.Errorf("couldn't find cert chain in environment variable %s", certs.CertChainEnvVar)
return nil, errors.Errorf("couldn't find cert chain in environment variable %s", certs.CertChainEnvVar)
}
key := os.Getenv(certs.CertKeyEnvVar)
if cert == "" {
return nil, fmt.Errorf("couldn't find cert key in environment variable %s", certs.CertKeyEnvVar)
return nil, errors.Errorf("couldn't find cert key in environment variable %s", certs.CertKeyEnvVar)
}
return &credentials.CertChain{
RootCA: []byte(trustAnchors),
Expand Down Expand Up @@ -69,7 +68,7 @@ func generateCSRAndPrivateKey(id string) ([]byte, []byte, error) {
key, err := certs.GenerateECPrivateKey()
if err != nil {
diag.DefaultMonitoring.MTLSInitFailed("prikeygen")
return nil, nil, fmt.Errorf("failed to generate private key: %s", err)
return nil, nil, errors.Wrap(err, "failed to generate private key")
}

encodedKey, err := x509.MarshalECPrivateKey(key)
Expand All @@ -86,7 +85,7 @@ func generateCSRAndPrivateKey(id string) ([]byte, []byte, error) {
csrb, err := x509.CreateCertificateRequest(rand.Reader, &csr, key)
if err != nil {
diag.DefaultMonitoring.MTLSInitFailed("csr")
return nil, nil, fmt.Errorf("failed to create sidecar csr: %s", err)
return nil, nil, errors.Wrap(err, "failed to create sidecar csr")
}
return csrb, keyPem, nil
}
6 changes: 3 additions & 3 deletions utils/host.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package utils

import (
"errors"
"fmt"
"net"
"os"

"github.com/pkg/errors"
)

const (
Expand All @@ -25,7 +25,7 @@ func GetHostAddress() (string, error) {
// Could not find one via a UDP connection, so we fallback to the "old" way: try first non-loopback IPv4:
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", fmt.Errorf("error getting interface IP addresses: %s", err)
return "", errors.Wrap(err, "error getting interface IP addresses")
}

for _, addr := range addrs {
Expand Down

0 comments on commit aeb79e0

Please sign in to comment.