Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
109 changes: 109 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -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...)
}
145 changes: 145 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}