From 96907f13e17aca2af9e697d24801824f46af25c2 Mon Sep 17 00:00:00 2001 From: Christopher Crone Date: Thu, 14 May 2020 09:53:49 +0200 Subject: [PATCH] Add forbidden error Signed-off-by: Christopher Crone --- errdefs/errors.go | 7 +++++++ errdefs/errors_test.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/errdefs/errors.go b/errdefs/errors.go index ff944d1c1..1bd21d9f8 100644 --- a/errdefs/errors.go +++ b/errdefs/errors.go @@ -36,6 +36,8 @@ var ( ErrNotFound = errors.New("not found") // ErrAlreadyExists is returned when an object already exists ErrAlreadyExists = errors.New("already exists") + // ErrForbidden is returned when an operation is not permitted + ErrForbidden = errors.New("forbidden") // ErrUnknown is returned when the error type is unmapped ErrUnknown = errors.New("unknown") ) @@ -50,6 +52,11 @@ func IsAlreadyExistsError(err error) bool { return errors.Is(err, ErrAlreadyExists) } +// IsForbiddenError returns true if the unwrapped error is ErrForbidden +func IsForbiddenError(err error) bool { + return errors.Is(err, ErrForbidden) +} + // IsUnknownError returns true if the unwrapped error is ErrUnknown func IsUnknownError(err error) bool { return errors.Is(err, ErrUnknown) diff --git a/errdefs/errors_test.go b/errdefs/errors_test.go index a2c2e48e8..29f0684d5 100644 --- a/errdefs/errors_test.go +++ b/errdefs/errors_test.go @@ -47,3 +47,17 @@ func TestIsAlreadyExists(t *testing.T) { require.False(t, IsAlreadyExistsError(errors.New("another error"))) } + +func TestIsForbidden(t *testing.T) { + err := errors.Wrap(ErrForbidden, `object "name"`) + require.True(t, IsForbiddenError(err)) + + require.False(t, IsForbiddenError(errors.New("another error"))) +} + +func TestIsUnknown(t *testing.T) { + err := errors.Wrap(ErrUnknown, `object "name"`) + require.True(t, IsUnknownError(err)) + + require.False(t, IsUnknownError(errors.New("another error"))) +}