Implement Unwrap to errors

Signed-off-by: Kostadin Plachkov <k.n.plachkov@gmail.com>
This commit is contained in:
Kostadin Plachkov 2020-11-15 20:41:37 +01:00
parent af34b94a78
commit aeddf93de0
2 changed files with 91 additions and 0 deletions

View file

@ -10,6 +10,10 @@ func (e errNotFound) Cause() error {
return e.error
}
func (e errNotFound) Unwrap() error {
return e.error
}
// NotFound is a helper to create an error of the class with the same name from any error type
func NotFound(err error) error {
if err == nil || IsNotFound(err) {
@ -26,6 +30,10 @@ func (e errInvalidParameter) Cause() error {
return e.error
}
func (e errInvalidParameter) Unwrap() error {
return e.error
}
// InvalidParameter is a helper to create an error of the class with the same name from any error type
func InvalidParameter(err error) error {
if err == nil || IsInvalidParameter(err) {
@ -42,6 +50,10 @@ func (e errConflict) Cause() error {
return e.error
}
func (e errConflict) Unwrap() error {
return e.error
}
// Conflict is a helper to create an error of the class with the same name from any error type
func Conflict(err error) error {
if err == nil || IsConflict(err) {
@ -58,6 +70,10 @@ func (e errUnauthorized) Cause() error {
return e.error
}
func (e errUnauthorized) Unwrap() error {
return e.error
}
// Unauthorized is a helper to create an error of the class with the same name from any error type
func Unauthorized(err error) error {
if err == nil || IsUnauthorized(err) {
@ -74,6 +90,10 @@ func (e errUnavailable) Cause() error {
return e.error
}
func (e errUnavailable) Unwrap() error {
return e.error
}
// Unavailable is a helper to create an error of the class with the same name from any error type
func Unavailable(err error) error {
if err == nil || IsUnavailable(err) {
@ -90,6 +110,10 @@ func (e errForbidden) Cause() error {
return e.error
}
func (e errForbidden) Unwrap() error {
return e.error
}
// Forbidden is a helper to create an error of the class with the same name from any error type
func Forbidden(err error) error {
if err == nil || IsForbidden(err) {
@ -106,6 +130,10 @@ func (e errSystem) Cause() error {
return e.error
}
func (e errSystem) Unwrap() error {
return e.error
}
// System is a helper to create an error of the class with the same name from any error type
func System(err error) error {
if err == nil || IsSystem(err) {
@ -122,6 +150,10 @@ func (e errNotModified) Cause() error {
return e.error
}
func (e errNotModified) Unwrap() error {
return e.error
}
// NotModified is a helper to create an error of the class with the same name from any error type
func NotModified(err error) error {
if err == nil || IsNotModified(err) {
@ -138,6 +170,10 @@ func (e errNotImplemented) Cause() error {
return e.error
}
func (e errNotImplemented) Unwrap() error {
return e.error
}
// NotImplemented is a helper to create an error of the class with the same name from any error type
func NotImplemented(err error) error {
if err == nil || IsNotImplemented(err) {
@ -154,6 +190,10 @@ func (e errUnknown) Cause() error {
return e.error
}
func (e errUnknown) Unwrap() error {
return e.error
}
// Unknown is a helper to create an error of the class with the same name from any error type
func Unknown(err error) error {
if err == nil || IsUnknown(err) {
@ -170,6 +210,10 @@ func (e errCancelled) Cause() error {
return e.error
}
func (e errCancelled) Unwrap() error {
return e.error
}
// Cancelled is a helper to create an error of the class with the same name from any error type
func Cancelled(err error) error {
if err == nil || IsCancelled(err) {
@ -186,6 +230,10 @@ func (e errDeadline) Cause() error {
return e.error
}
func (e errDeadline) Unwrap() error {
return e.error
}
// Deadline is a helper to create an error of the class with the same name from any error type
func Deadline(err error) error {
if err == nil || IsDeadline(err) {
@ -202,6 +250,10 @@ func (e errDataLoss) Cause() error {
return e.error
}
func (e errDataLoss) Unwrap() error {
return e.error
}
// DataLoss is a helper to create an error of the class with the same name from any error type
func DataLoss(err error) error {
if err == nil || IsDataLoss(err) {

View file

@ -22,6 +22,9 @@ func TestNotFound(t *testing.T) {
if cause := e.(causal).Cause(); cause != errTest {
t.Fatalf("causual should be errTest, got: %v", cause)
}
if !errors.Is(e, errTest) {
t.Fatalf("expected not found error to match errTest")
}
}
func TestConflict(t *testing.T) {
@ -35,6 +38,9 @@ func TestConflict(t *testing.T) {
if cause := e.(causal).Cause(); cause != errTest {
t.Fatalf("causual should be errTest, got: %v", cause)
}
if !errors.Is(e, errTest) {
t.Fatalf("expected conflict error to match errTest")
}
}
func TestForbidden(t *testing.T) {
@ -48,6 +54,9 @@ func TestForbidden(t *testing.T) {
if cause := e.(causal).Cause(); cause != errTest {
t.Fatalf("causual should be errTest, got: %v", cause)
}
if !errors.Is(e, errTest) {
t.Fatalf("expected forbidden error to match errTest")
}
}
func TestInvalidParameter(t *testing.T) {
@ -61,6 +70,9 @@ func TestInvalidParameter(t *testing.T) {
if cause := e.(causal).Cause(); cause != errTest {
t.Fatalf("causual should be errTest, got: %v", cause)
}
if !errors.Is(e, errTest) {
t.Fatalf("expected invalid argument error to match errTest")
}
}
func TestNotImplemented(t *testing.T) {
@ -74,6 +86,9 @@ func TestNotImplemented(t *testing.T) {
if cause := e.(causal).Cause(); cause != errTest {
t.Fatalf("causual should be errTest, got: %v", cause)
}
if !errors.Is(e, errTest) {
t.Fatalf("expected not implemented error to match errTest")
}
}
func TestNotModified(t *testing.T) {
@ -87,6 +102,9 @@ func TestNotModified(t *testing.T) {
if cause := e.(causal).Cause(); cause != errTest {
t.Fatalf("causual should be errTest, got: %v", cause)
}
if !errors.Is(e, errTest) {
t.Fatalf("expected not modified error to match errTest")
}
}
func TestUnauthorized(t *testing.T) {
@ -100,6 +118,9 @@ func TestUnauthorized(t *testing.T) {
if cause := e.(causal).Cause(); cause != errTest {
t.Fatalf("causual should be errTest, got: %v", cause)
}
if !errors.Is(e, errTest) {
t.Fatalf("expected unauthorized error to match errTest")
}
}
func TestUnknown(t *testing.T) {
@ -113,6 +134,9 @@ func TestUnknown(t *testing.T) {
if cause := e.(causal).Cause(); cause != errTest {
t.Fatalf("causual should be errTest, got: %v", cause)
}
if !errors.Is(e, errTest) {
t.Fatalf("expected unknown error to match errTest")
}
}
func TestCancelled(t *testing.T) {
@ -126,6 +150,9 @@ func TestCancelled(t *testing.T) {
if cause := e.(causal).Cause(); cause != errTest {
t.Fatalf("causual should be errTest, got: %v", cause)
}
if !errors.Is(e, errTest) {
t.Fatalf("expected cancelled error to match errTest")
}
}
func TestDeadline(t *testing.T) {
@ -139,6 +166,9 @@ func TestDeadline(t *testing.T) {
if cause := e.(causal).Cause(); cause != errTest {
t.Fatalf("causual should be errTest, got: %v", cause)
}
if !errors.Is(e, errTest) {
t.Fatalf("expected deadline error to match errTest")
}
}
func TestDataLoss(t *testing.T) {
@ -152,6 +182,9 @@ func TestDataLoss(t *testing.T) {
if cause := e.(causal).Cause(); cause != errTest {
t.Fatalf("causual should be errTest, got: %v", cause)
}
if !errors.Is(e, errTest) {
t.Fatalf("expected data loss error to match errTest")
}
}
func TestUnavailable(t *testing.T) {
@ -165,6 +198,9 @@ func TestUnavailable(t *testing.T) {
if cause := e.(causal).Cause(); cause != errTest {
t.Fatalf("causual should be errTest, got: %v", cause)
}
if !errors.Is(e, errTest) {
t.Fatalf("expected unavaillable error to match errTest")
}
}
func TestSystem(t *testing.T) {
@ -178,4 +214,7 @@ func TestSystem(t *testing.T) {
if cause := e.(causal).Cause(); cause != errTest {
t.Fatalf("causual should be errTest, got: %v", cause)
}
if !errors.Is(e, errTest) {
t.Fatalf("expected system error to match errTest")
}
}