-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We want to use it more in ramen, to remove bugus errors for logs. To encourage use this this pattern and make it easier to use correctly, add util.IsOperationInProgress() helper. Improve the test to use the new IsOperationInProgress() and test also wrapped OperationInProgress error. Signed-off-by: Nir Soffer <nsoffer@redhat.com>
- Loading branch information
Showing
7 changed files
with
66 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-FileCopyrightText: The RamenDR authors | ||
// SPDX-License-Identifier: Apache-2.0package util | ||
|
||
package util | ||
|
||
import "errors" | ||
|
||
// OperationInProgress error is return when an operation is in progress and we wait for the desired state. The error | ||
// string should describe the operation for logging the error. | ||
type OperationInProgress string | ||
|
||
func (e OperationInProgress) Error() string { return string(e) } | ||
|
||
// Called by errors.Is() to match target. | ||
func (OperationInProgress) Is(target error) bool { | ||
_, ok := target.(OperationInProgress) | ||
|
||
return ok | ||
} | ||
|
||
// IsOperationInProgress returns true if err or error wrapped by it is an OperationInProgress error. | ||
func IsOperationInProgress(err error) bool { | ||
return errors.Is(err, OperationInProgress("")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-FileCopyrightText: The RamenDR authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package util_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/ramendr/ramen/internal/controller/util" | ||
) | ||
|
||
var _ = Describe("OperationInProgress", func() { | ||
Context("comparing errors", func() { | ||
err := util.OperationInProgress("this operation") | ||
|
||
It("is not equal", func() { | ||
Expect(err == util.OperationInProgress("other operation")).To(Equal(false)) | ||
}) | ||
It("match error", func() { | ||
Expect(util.IsOperationInProgress(err)).To(Equal(true)) | ||
}) | ||
It("match wrapped error", func() { | ||
wrapped := fmt.Errorf("wrapping operation in progress: %w", err) | ||
Expect(util.IsOperationInProgress(wrapped)).To(Equal(true)) | ||
}) | ||
It("does not match other errors", func() { | ||
Expect(util.IsOperationInProgress(errors.New("other error"))).To(Equal(false)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters