Skip to content

Commit

Permalink
Move runtime/{init_error.go,retriable_error.go} into separate share…
Browse files Browse the repository at this point in the history
…d package (dapr#6323)

* Move runtime/init_error.go into separate package

Signed-off-by: joshvanl <[email protected]>

* Move retryable error to errors package

Signed-off-by: joshvanl <[email protected]>

* Adds tests to ensure init and retriable implement error

Signed-off-by: joshvanl <[email protected]>

* golanci-lint

Signed-off-by: joshvanl <[email protected]>

---------

Signed-off-by: joshvanl <[email protected]>
Co-authored-by: Loong Dai <[email protected]>
Co-authored-by: Mukundan Sundararajan <[email protected]>
Co-authored-by: Alessandro (Ale) Segala <[email protected]>
  • Loading branch information
4 people authored Jun 1, 2023
1 parent 5aba3c9 commit d5f9625
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 56 deletions.
3 changes: 2 additions & 1 deletion pkg/runtime/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/dapr/dapr/pkg/operator/client"
operatorV1 "github.com/dapr/dapr/pkg/proto/operator/v1"
resiliencyConfig "github.com/dapr/dapr/pkg/resiliency"
rterrors "github.com/dapr/dapr/pkg/runtime/errors"
"github.com/dapr/dapr/pkg/runtime/security"
"github.com/dapr/dapr/pkg/validation"
"github.com/dapr/dapr/utils"
Expand Down Expand Up @@ -404,7 +405,7 @@ func FromFlags(args []string) (*DaprRuntime, error) {
// Initialize metrics only if MetricSpec is enabled.
if globalConfig.Spec.MetricSpec.Enabled {
if mErr := diag.InitMetrics(runtimeConfig.ID, namespace, globalConfig.Spec.MetricSpec.Rules); mErr != nil {
log.Errorf(NewInitError(InitFailure, "metrics", mErr).Error())
log.Errorf(rterrors.NewInit(rterrors.InitFailure, "metrics", mErr).Error())
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/runtime/init_error.go → pkg/runtime/errors/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package runtime
package errors

import (
"fmt"
Expand Down Expand Up @@ -43,8 +43,8 @@ func (e *InitError) Unwrap() error {
return e.err
}

// NewInitError returns an InitError wrapping an existing context error.
func NewInitError(kind InitErrorKind, entity string, err error) *InitError {
// NewInit returns an InitError wrapping an existing context error.
func NewInit(kind InitErrorKind, entity string, err error) *InitError {
return &InitError{
err: err,
kind: kind,
Expand Down
20 changes: 20 additions & 0 deletions pkg/runtime/errors/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package errors

import "testing"

func TestInitError(t *testing.T) {
var _ error = new(InitError)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package runtime
package errors

type RetriableError struct {
err error
Expand All @@ -32,8 +32,8 @@ func (e *RetriableError) Unwrap() error {
return e.err
}

// NewRetriableError returns a RetriableError wrapping an existing context error.
func NewRetriableError(err error) *RetriableError {
// NewRetriable returns a RetriableError wrapping an existing context error.
func NewRetriable(err error) *RetriableError {
return &RetriableError{
err: err,
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/runtime/errors/retriable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package errors

import "testing"

func TestRetriableError(t *testing.T) {
var _ error = new(RetriableError)
}
79 changes: 40 additions & 39 deletions pkg/runtime/runtime.go

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions pkg/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import (
runtimev1pb "github.com/dapr/dapr/pkg/proto/runtime/v1"
"github.com/dapr/dapr/pkg/resiliency"
"github.com/dapr/dapr/pkg/runtime/compstore"
rterrors "github.com/dapr/dapr/pkg/runtime/errors"
runtimePubsub "github.com/dapr/dapr/pkg/runtime/pubsub"
"github.com/dapr/dapr/pkg/runtime/security"
"github.com/dapr/dapr/pkg/scopes"
Expand Down Expand Up @@ -365,7 +366,7 @@ func TestDoProcessComponent(t *testing.T) {

// assert
assert.Error(t, err, "expected an error")
assert.Equal(t, err.Error(), NewInitError(InitComponentFailure, "testlock (lock.mockLock/v1)", assert.AnError).Error(), "expected error strings to match")
assert.Equal(t, err.Error(), rterrors.NewInit(rterrors.InitComponentFailure, "testlock (lock.mockLock/v1)", assert.AnError).Error(), "expected error strings to match")
})

t.Run("test error when lock version invalid", func(t *testing.T) {
Expand All @@ -388,7 +389,7 @@ func TestDoProcessComponent(t *testing.T) {

// assert
assert.Error(t, err, "expected an error")
assert.Equal(t, err.Error(), NewInitError(CreateComponentFailure, "testlock (lock.mockLock/v3)", fmt.Errorf("couldn't find lock store lock.mockLock/v3")).Error())
assert.Equal(t, err.Error(), rterrors.NewInit(rterrors.CreateComponentFailure, "testlock (lock.mockLock/v3)", fmt.Errorf("couldn't find lock store lock.mockLock/v3")).Error())
})

t.Run("test error when lock prefix strategy invalid", func(t *testing.T) {
Expand Down Expand Up @@ -466,7 +467,7 @@ func TestDoProcessComponent(t *testing.T) {

// assert
assert.Error(t, err, "expected an error")
assert.Equal(t, err.Error(), NewInitError(InitComponentFailure, "testpubsub (pubsub.mockPubSub/v1)", assert.AnError).Error(), "expected error strings to match")
assert.Equal(t, err.Error(), rterrors.NewInit(rterrors.InitComponentFailure, "testpubsub (pubsub.mockPubSub/v1)", assert.AnError).Error(), "expected error strings to match")
})

t.Run("test invalid category component", func(t *testing.T) {
Expand Down Expand Up @@ -862,7 +863,7 @@ func TestInitState(t *testing.T) {

// assert
assert.Error(t, err, "expected error")
assert.Equal(t, err.Error(), NewInitError(InitComponentFailure, "testpubsub (state.mockState/v1)", assert.AnError).Error(), "expected error strings to match")
assert.Equal(t, err.Error(), rterrors.NewInit(rterrors.InitComponentFailure, "testpubsub (state.mockState/v1)", assert.AnError).Error(), "expected error strings to match")
})

t.Run("test init state store, encryption not enabled", func(t *testing.T) {
Expand Down Expand Up @@ -3184,7 +3185,7 @@ func TestOnNewPublishedMessage(t *testing.T) {
// assert
var cloudEvent map[string]interface{}
json.Unmarshal(testPubSubMessage.data, &cloudEvent)
expectedClientError := fmt.Errorf("RETRY status returned from app while processing pub/sub event %v: %w", cloudEvent["id"].(string), NewRetriableError(nil))
expectedClientError := fmt.Errorf("RETRY status returned from app while processing pub/sub event %v: %w", cloudEvent["id"].(string), rterrors.NewRetriable(nil))
assert.Equal(t, expectedClientError.Error(), err.Error())
mockAppChannel.AssertNumberOfCalls(t, "InvokeMethod", 1)
})
Expand Down Expand Up @@ -3280,7 +3281,7 @@ func TestOnNewPublishedMessage(t *testing.T) {
err := rt.publishMessageHTTP(context.Background(), testPubSubMessage)

// assert
expectedError := fmt.Errorf("error returned from app channel while sending pub/sub event to app: %w", NewRetriableError(invokeError))
expectedError := fmt.Errorf("error returned from app channel while sending pub/sub event to app: %w", rterrors.NewRetriable(invokeError))
assert.Equal(t, expectedError.Error(), err.Error(), "expected errors to match")
mockAppChannel.AssertNumberOfCalls(t, "InvokeMethod", 1)
})
Expand Down Expand Up @@ -3322,7 +3323,7 @@ func TestOnNewPublishedMessage(t *testing.T) {
var cloudEvent map[string]interface{}
json.Unmarshal(testPubSubMessage.data, &cloudEvent)
errMsg := fmt.Sprintf("retriable error returned from app while processing pub/sub event %v, topic: %v, body: Internal Error. status code returned: 500", cloudEvent["id"].(string), cloudEvent["topic"])
expectedClientError := NewRetriableError(errors.New(errMsg))
expectedClientError := rterrors.NewRetriable(errors.New(errMsg))
assert.Equal(t, expectedClientError.Error(), err.Error())
mockAppChannel.AssertNumberOfCalls(t, "InvokeMethod", 1)
})
Expand Down Expand Up @@ -3394,7 +3395,7 @@ func TestOnNewPublishedMessageGRPC(t *testing.T) {
expectedError: fmt.Errorf(
"error returned from app while processing pub/sub event %v: %w",
testPubSubMessage.cloudEvent[pubsub.IDField],
NewRetriableError(status.Error(codes.Unknown, assert.AnError.Error())),
rterrors.NewRetriable(status.Error(codes.Unknown, assert.AnError.Error())),
),
},
{
Expand All @@ -3419,7 +3420,7 @@ func TestOnNewPublishedMessageGRPC(t *testing.T) {
expectedError: fmt.Errorf(
"RETRY status returned from app while processing pub/sub event %v: %w",
testPubSubMessage.cloudEvent[pubsub.IDField],
NewRetriableError(nil),
rterrors.NewRetriable(nil),
),
},
{
Expand All @@ -3435,7 +3436,7 @@ func TestOnNewPublishedMessageGRPC(t *testing.T) {
"unknown status returned from app while processing pub/sub event %v, status: %v, err: %w",
testPubSubMessage.cloudEvent[pubsub.IDField],
runtimev1pb.TopicEventResponse_TopicEventResponseStatus(99),
NewRetriableError(nil),
rterrors.NewRetriable(nil),
),
},
{
Expand Down

0 comments on commit d5f9625

Please sign in to comment.