diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-03-18 13:29:36 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-03-18 14:17:02 +0300 |
commit | 1275584eccfa21f6a9b92d4eba5e914a3232659e (patch) | |
tree | d084c6e897d48bec9fb04f10885b498857f955ba | |
parent | db8b5d43582182fd743ef2a1fdf84b07efd8f786 (diff) |
helper: Add error helpers for `codes.Aborted`
Add two new helpers `helper.ErrAborted()` and `helper.ErrAbortedf()` to
generate gRPC errors with an `codes.Aborted` error code.
-rw-r--r-- | internal/helper/error.go | 9 | ||||
-rw-r--r-- | internal/helper/error_test.go | 10 |
2 files changed, 19 insertions, 0 deletions
diff --git a/internal/helper/error.go b/internal/helper/error.go index 81f25ddcc..5196513e0 100644 --- a/internal/helper/error.go +++ b/internal/helper/error.go @@ -48,6 +48,9 @@ func ErrPermissionDenied(err error) error { return wrapError(codes.PermissionDen // ErrAlreadyExists wraps err with codes.AlreadyExists, unless err is already a gRPC error. func ErrAlreadyExists(err error) error { return wrapError(codes.AlreadyExists, err) } +// ErrAborted wraps err with codes.Aborted, unless err is already a gRPC error. +func ErrAborted(err error) error { return wrapError(codes.Aborted, err) } + // wrapError wraps the given error with the error code unless it's already a gRPC error. If given // nil it will return nil. func wrapError(code codes.Code, err error) error { @@ -99,6 +102,12 @@ func ErrAlreadyExistsf(format string, a ...interface{}) error { return formatError(codes.AlreadyExists, format, a...) } +// ErrAbortedf wraps a formatted error with codes.Aborted, unless the formatted error is a wrapped +// gRPC error. +func ErrAbortedf(format string, a ...interface{}) error { + return formatError(codes.Aborted, format, a...) +} + // formatError will create a new error from the given format string. If the error string contains a // %w verb and its corresponding error has a gRPC error code, then the returned error will keep this // gRPC error code instead of using the one provided as an argument. diff --git a/internal/helper/error_test.go b/internal/helper/error_test.go index 05985a824..3b07f1570 100644 --- a/internal/helper/error_test.go +++ b/internal/helper/error_test.go @@ -60,6 +60,11 @@ func TestError(t *testing.T) { errorf: ErrAlreadyExists, code: codes.AlreadyExists, }, + { + desc: "Aborted", + errorf: ErrAborted, + code: codes.Aborted, + }, } { t.Run(tc.desc, func(t *testing.T) { // tc.code and our canary test code must not @@ -133,6 +138,11 @@ func testErrorfFormat(t *testing.T, errorFormat, errorFormatEqual string) { errorf: ErrUnavailablef, code: codes.Unavailable, }, + { + desc: "ErrAbortedf", + errorf: ErrAbortedf, + code: codes.Aborted, + }, } { t.Run(tc.desc, func(t *testing.T) { require.NotEqual(t, tc.code, inputGRPCCode, "canary test code and tc.code may not be the same") |