From 3b7c8a7df6a0776c53c2ab0d18292a0cbd1281ca Mon Sep 17 00:00:00 2001 From: Darwayne Date: Sun, 17 Apr 2022 20:25:07 -0400 Subject: [PATCH] adds some helper functions to simplify creating & wrapping errors with initial behavior --- errors.go | 17 +++--- helpers.go | 109 ++++++++++++++++++++++++++++++++++++ helpers_test.go | 145 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 264 insertions(+), 7 deletions(-) create mode 100644 helpers.go create mode 100644 helpers_test.go diff --git a/errors.go b/errors.go index df619ea..4e37f77 100644 --- a/errors.go +++ b/errors.go @@ -53,13 +53,16 @@ func New(message string, opts ...OptsFunc) error { } var ( - _ AccessDenier = (*multiKindErr)(nil) - _ Conflicter = (*multiKindErr)(nil) - _ Exister = (*multiKindErr)(nil) - _ NotFounder = (*multiKindErr)(nil) - _ RateLimiter = (*multiKindErr)(nil) - _ TooLarge = (*multiKindErr)(nil) - _ error = (*multiKindErr)(nil) + _ AccessDenier = (*multiKindErr)(nil) + _ Conflicter = (*multiKindErr)(nil) + _ Exister = (*multiKindErr)(nil) + _ NotFounder = (*multiKindErr)(nil) + _ RateLimiter = (*multiKindErr)(nil) + _ TooLarge = (*multiKindErr)(nil) + _ TooManyRequester = (*multiKindErr)(nil) + _ Temporarily = (*multiKindErr)(nil) + _ Unauthorizable = (*multiKindErr)(nil) + _ error = (*multiKindErr)(nil) ) type Opts struct { diff --git a/helpers.go b/helpers.go new file mode 100644 index 0000000..ab919a9 --- /dev/null +++ b/helpers.go @@ -0,0 +1,109 @@ +package errutil + +// NewAccessDenied creates a new error and gives it AccessDenier behavior +func NewAccessDenied(msg string, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithAccessDenied()) + return New(msg, opts...) +} + +// WrapAccessDenied wraps an error and gives it AccessDenier behavior +func WrapAccessDenied(err error, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithAccessDenied()) + return Wrap(err, opts...) +} + +// NewExists creates a new error and gives it Exister behavior +func NewExists(msg string, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithExists()) + return New(msg, opts...) +} + +// WrapExists wraps an error and gives it Exister behavior +func WrapExists(err error, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithExists()) + return Wrap(err, opts...) +} + +// NewConflict creates a new error and gives it Conflicter behavior +func NewConflict(msg string, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithConflict()) + return New(msg, opts...) +} + +// WrapConflict wraps an error and gives it Conflicter behavior +func WrapConflict(err error, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithConflict()) + return Wrap(err, opts...) +} + +// NewNotFound creates a new error and gives it NotFounder behavior +func NewNotFound(msg string, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithNotFound()) + return New(msg, opts...) +} + +// WrapNotFound wraps an error and gives it NotFounder behavior +func WrapNotFound(err error, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithNotFound()) + return Wrap(err, opts...) +} + +// NewRateLimit creates a new error and gives it RateLimiter behavior +func NewRateLimit(msg string, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithRateLimit()) + return New(msg, opts...) +} + +// WrapRateLimit wraps an error and gives it RateLimiter behavior +func WrapRateLimit(err error, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithRateLimit()) + return Wrap(err, opts...) +} + +// NewTemporary creates a new error and gives it Temporarily behavior +func NewTemporary(msg string, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithTemporary()) + return New(msg, opts...) +} + +// WrapTemporary wraps an error and gives it Temporarily behavior +func WrapTemporary(err error, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithTemporary()) + return Wrap(err, opts...) +} + +// NewTooLarge creates a new error and gives it TooLarge behavior +func NewTooLarge(msg string, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithTooLarge()) + return New(msg, opts...) +} + +// WrapTooLarge wraps an error and gives it TooLarge behavior +func WrapTooLarge(err error, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithTooLarge()) + return Wrap(err, opts...) +} + +// NewTooManyRequests creates a new error and gives it TooManyRequester behavior +func NewTooManyRequests(msg string, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithTooManyRequests()) + return New(msg, opts...) +} + +// WrapTooManyRequests wraps an error and gives it TooManyRequester behavior +func WrapTooManyRequests(err error, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithTooManyRequests()) + return Wrap(err, opts...) +} + +// NewUnauthorized creates a new error and gives it Unauthorizable behavior +func NewUnauthorized(msg string, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithUnauthorized()) + return New(msg, opts...) +} + +// WrapUnauthorized wraps an error and gives it Unauthorizable behavior +func WrapUnauthorized(err error, opts ...OptsFunc) error { + opts = append(opts, WithStackTrace(1), WithUnauthorized()) + return Wrap(err, opts...) +} diff --git a/helpers_test.go b/helpers_test.go new file mode 100644 index 0000000..b25b2c4 --- /dev/null +++ b/helpers_test.go @@ -0,0 +1,145 @@ +package errutil + +import "testing" + +func TestAccessDenied(t *testing.T) { + t.Run("New", func(t *testing.T) { + err := NewAccessDenied("hey") + assertTrue(t, IsAccessDenied(err)) + t.Logf("%+v", err) + }) + + t.Run("Wrap", func(t *testing.T) { + err := NewAccessDenied("hey") + err = WrapAccessDenied(err) + assertTrue(t, IsAccessDenied(err)) + t.Logf("%+v", err) + }) +} + +func TestExists(t *testing.T) { + t.Run("New", func(t *testing.T) { + err := NewExists("hey") + assertTrue(t, IsExist(err)) + t.Logf("%+v", err) + }) + + t.Run("Wrap", func(t *testing.T) { + err := NewExists("hey") + err = WrapExists(err) + assertTrue(t, IsExist(err)) + t.Logf("%+v", err) + }) +} + +func TestConflict(t *testing.T) { + t.Run("New", func(t *testing.T) { + err := NewConflict("hey") + assertTrue(t, IsConflict(err)) + t.Logf("%+v", err) + }) + + t.Run("Wrap", func(t *testing.T) { + err := NewConflict("hey") + err = WrapConflict(err) + assertTrue(t, IsConflict(err)) + t.Logf("%+v", err) + }) +} + +func TestNotFound(t *testing.T) { + t.Run("New", func(t *testing.T) { + err := NewNotFound("hey") + assertTrue(t, IsNotFound(err)) + t.Logf("%+v", err) + }) + + t.Run("Wrap", func(t *testing.T) { + err := NewNotFound("hey") + err = WrapNotFound(err) + assertTrue(t, IsNotFound(err)) + t.Logf("%+v", err) + }) +} + +func TestRateLimit(t *testing.T) { + t.Run("New", func(t *testing.T) { + err := NewRateLimit("hey") + assertTrue(t, IsRateLimit(err)) + t.Logf("%+v", err) + }) + + t.Run("Wrap", func(t *testing.T) { + err := NewRateLimit("hey") + err = WrapRateLimit(err) + assertTrue(t, IsRateLimit(err)) + t.Logf("%+v", err) + }) +} + +func TestTemporary(t *testing.T) { + t.Run("New", func(t *testing.T) { + err := NewTemporary("hey") + assertTrue(t, IsTemporary(err)) + t.Logf("%+v", err) + }) + + t.Run("Wrap", func(t *testing.T) { + err := NewTemporary("hey") + err = WrapTemporary(err) + assertTrue(t, IsTemporary(err)) + t.Logf("%+v", err) + }) +} + +func TestTooLarge(t *testing.T) { + t.Run("New", func(t *testing.T) { + err := NewTooLarge("hey") + assertTrue(t, IsTooLarge(err)) + t.Logf("%+v", err) + }) + + t.Run("Wrap", func(t *testing.T) { + err := NewTooLarge("hey") + err = WrapTooLarge(err) + assertTrue(t, IsTooLarge(err)) + t.Logf("%+v", err) + }) +} + +func TestTooManyRequests(t *testing.T) { + t.Run("New", func(t *testing.T) { + err := NewTooManyRequests("hey") + assertTrue(t, IsTooManyRequests(err)) + t.Logf("%+v", err) + }) + + t.Run("Wrap", func(t *testing.T) { + err := NewTooManyRequests("hey") + err = WrapTooManyRequests(err) + assertTrue(t, IsTooManyRequests(err)) + t.Logf("%+v", err) + }) +} + +func TestUnauthorized(t *testing.T) { + t.Run("New", func(t *testing.T) { + err := NewUnauthorized("hey") + assertTrue(t, IsUnauthorized(err)) + t.Logf("%+v", err) + }) + + t.Run("Wrap", func(t *testing.T) { + err := NewUnauthorized("hey") + err = WrapUnauthorized(err) + assertTrue(t, IsUnauthorized(err)) + t.Logf("%+v", err) + }) +} + +func assertTrue(t *testing.T, val bool) { + t.Helper() + if !val { + t.Fatal("expected true") + } +}