Add forbidden error

Signed-off-by: Christopher Crone <christopher.crone@docker.com>
This commit is contained in:
Christopher Crone 2020-05-14 09:53:49 +02:00
parent d46398dbef
commit 96907f13e1
2 changed files with 21 additions and 0 deletions

View File

@ -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)

View File

@ -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")))
}