This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GIT-4850: create new errors package and update extensions/docker-desktop
- Loading branch information
1 parent
762ed99
commit ae77e61
Showing
12 changed files
with
194 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package errors | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type Reason string | ||
|
||
const ( | ||
ReasonUnknown Reason = "Unknown" | ||
ReasonLockFailed Reason = "LockFailed" | ||
ReasonMergeFailed Reason = "MergeFailed" | ||
ReasonReadFailed Reason = "ReadFailed" | ||
ReasonIOFailed Reason = "IOFailed" | ||
ReasonMarshallingFailed Reason = "MarshallingFailed" | ||
ReasonEncodeFailed Reason = "EncodeFailed" | ||
ReasonNormalizationFailed Reason = "NormalizationFailed" | ||
ReasonValidationFailed Reason = "ValidationFailed" | ||
ReasonTimeout Reason = "Timeout" | ||
ReasonGeneric Reason = "GenericFailure" | ||
) | ||
|
||
// TODO: implement the errors.Unwrap interface to support errors.Unwrap cleanly | ||
type TanzuError struct { | ||
ErrorDetails ErrorDetails `json:"errorDetails"` | ||
} | ||
|
||
type ErrorDetails struct { | ||
Message string `json:"message,omitempty"` | ||
Reason Reason `json:"reason,omitempty"` | ||
Err error `json:"err"` | ||
} | ||
|
||
func (t TanzuError) Error() string { | ||
return fmt.Sprintf("%s: %s (%s)", t.ErrorDetails.Reason, t.ErrorDetails.Message, t.ErrorDetails.Err) | ||
} | ||
|
||
func generateError(reason Reason, err error, args ...interface{}) TanzuError { | ||
var message string | ||
switch len(args) { | ||
case 0: | ||
message = "" | ||
case 1: | ||
message = args[0].(string) | ||
default: | ||
message = fmt.Sprintf(args[0].(string), args[1:]...) | ||
} | ||
return TanzuError{ | ||
ErrorDetails: ErrorDetails{ | ||
Reason: reason, | ||
Message: message, | ||
Err: err, | ||
}, | ||
} | ||
} | ||
|
||
func NewLockFailed(err error, args ...interface{}) TanzuError { | ||
return generateError(ReasonLockFailed, err, args...) | ||
} | ||
|
||
func NewMergeFailed(err error, args ...interface{}) TanzuError { | ||
return generateError(ReasonMergeFailed, err, args...) | ||
} | ||
|
||
func NewReadFailed(err error, args ...interface{}) TanzuError { | ||
return generateError(ReasonReadFailed, err, args...) | ||
} | ||
|
||
func NewIOFailed(err error, args ...interface{}) TanzuError { | ||
return generateError(ReasonIOFailed, err, args...) | ||
} | ||
|
||
func NewMarshallingFailed(err error, args ...interface{}) TanzuError { | ||
return generateError(ReasonMarshallingFailed, err, args...) | ||
} | ||
|
||
func NewNormalizationFailed(err error, args ...interface{}) TanzuError { | ||
return generateError(ReasonNormalizationFailed, err, args...) | ||
} | ||
|
||
func NewValidationFailed(err error, args ...interface{}) TanzuError { | ||
return generateError(ReasonValidationFailed, err, args...) | ||
} | ||
|
||
func NewTimeout(err error, args ...interface{}) TanzuError { | ||
return generateError(ReasonTimeout, err, args...) | ||
} | ||
|
||
func NewGenericFail(err error, args ...interface{}) TanzuError { | ||
return generateError(ReasonGeneric, err, args...) | ||
} | ||
|
||
func NewEncodeFail(err error, args ...interface{}) TanzuError { | ||
return generateError(ReasonEncodeFailed, err, args...) | ||
} |
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,59 @@ | ||
package errors | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func TestErrors(t *testing.T) { | ||
tests := []struct { | ||
errorFunc func(err error, args ...interface{}) TanzuError | ||
Reason Reason | ||
}{ | ||
{ | ||
NewLockFailed, ReasonLockFailed, | ||
}, | ||
{ | ||
NewMergeFailed, ReasonMergeFailed, | ||
}, | ||
{ | ||
NewReadFailed, ReasonReadFailed, | ||
}, | ||
{ | ||
NewIOFailed, ReasonIOFailed, | ||
}, | ||
{ | ||
NewMarshallingFailed, ReasonMarshallingFailed, | ||
}, | ||
{ | ||
NewNormalizationFailed, ReasonNormalizationFailed, | ||
}, | ||
{ | ||
NewValidationFailed, ReasonValidationFailed, | ||
}, | ||
{ | ||
NewTimeout, ReasonTimeout, | ||
}, | ||
{ | ||
NewGenericFail, ReasonGeneric, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc | ||
name := runtime.FuncForPC(reflect.ValueOf(tc.errorFunc).Pointer()).Name() | ||
t.Run(name, func(t *testing.T) { | ||
err := tc.errorFunc(errors.New("fail"), "some failure. Arg %s", "test") | ||
if err.ErrorDetails.Reason != tc.Reason { | ||
t.Errorf("%s, Expected: %v, got: %v", name, tc.Reason, err.ErrorDetails.Reason) | ||
} | ||
expected := fmt.Sprintf("%s: %s (%s)", tc.Reason, "some failure. Arg test", "fail") | ||
if err.Error() != expected { | ||
t.Errorf("%s, Expected: %s, got: %s", name, expected, err.Error()) | ||
} | ||
}) | ||
} | ||
} |
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,3 @@ | ||
module github.com/vmware-tanzu/community-edition/errors | ||
|
||
go 1.17 |
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 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 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 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
Oops, something went wrong.