Kaynağa Gözat

<fix>(Backward comp): Add build tags to make errors backward compatible

tfreville 5 yıl önce
ebeveyn
işleme
31b0a5b4a4
5 değiştirilmiş dosya ile 117 ekleme ve 79 silme
  1. 0 19
      error.go
  2. 26 0
      error_1_13.go
  3. 68 0
      error_1_13_test.go
  4. 22 0
      error_backward.go
  5. 1 60
      error_test.go

+ 0 - 19
error.go

@@ -47,7 +47,6 @@ package errors
 
 import (
 	"bytes"
-	baseErrors "errors"
 	"fmt"
 	"reflect"
 	"runtime"
@@ -140,24 +139,6 @@ func WrapPrefix(e interface{}, prefix string, skip int) *Error {
 
 }
 
-// Is detects whether the error is equal to a given error. Errors
-// are considered equal by this function if they are matched by errors.Is
-// or if their contained errors are matched through errors.Is
-func Is(e error, original error) bool {
-	if baseErrors.Is(e, original) {
-		return true
-	}
-
-	if e, ok := e.(*Error); ok {
-		return Is(e.Err, original)
-	}
-
-	if original, ok := original.(*Error); ok {
-		return Is(e, original.Err)
-	}
-
-	return false
-}
 
 // Errorf creates a new error with the given message. You can use it
 // as a drop-in replacement for fmt.Errorf() to provide descriptive

+ 26 - 0
error_1_13.go

@@ -0,0 +1,26 @@
+// +build go1.13
+
+package errors
+
+import (
+	baseErrors "errors"
+)
+
+// Is detects whether the error is equal to a given error. Errors
+// are considered equal by this function if they are matched by errors.Is
+// or if their contained errors are matched through errors.Is
+func Is(e error, original error) bool {
+	if baseErrors.Is(e, original) {
+		return true
+	}
+
+	if e, ok := e.(*Error); ok {
+		return Is(e.Err, original)
+	}
+
+	if original, ok := original.(*Error); ok {
+		return Is(e, original.Err)
+	}
+
+	return false
+}

+ 68 - 0
error_1_13_test.go

@@ -0,0 +1,68 @@
+// +build go1.13
+
+package errors
+
+import (
+	"io"
+	"testing"
+)
+
+// This test should work only for go 1.13 and latter
+func TestIs113(t *testing.T) {
+	custErr := errorWithCustomIs{
+		Key: "TestForFun",
+		Err: io.EOF,
+	}
+
+	shouldMatch := errorWithCustomIs{
+		Key: "TestForFun",
+	}
+
+	shouldNotMatch := errorWithCustomIs{Key: "notOk"}
+
+	if !Is(custErr, shouldMatch) {
+		t.Errorf("custErr is not a TestForFun customError")
+	}
+
+	if Is(custErr, shouldNotMatch) {
+		t.Errorf("custErr is a notOk customError")
+	}
+
+	if !Is(custErr, New(shouldMatch)) {
+		t.Errorf("custErr is not a New(TestForFun customError)")
+	}
+
+	if Is(custErr, New(shouldNotMatch)) {
+		t.Errorf("custErr is a New(notOk customError)")
+	}
+
+	if !Is(New(custErr), shouldMatch) {
+		t.Errorf("New(custErr) is not a TestForFun customError")
+	}
+
+	if Is(New(custErr), shouldNotMatch) {
+		t.Errorf("New(custErr) is a notOk customError")
+	}
+
+	if !Is(New(custErr), New(shouldMatch)) {
+		t.Errorf("New(custErr) is not a New(TestForFun customError)")
+	}
+
+	if Is(New(custErr), New(shouldNotMatch)) {
+		t.Errorf("New(custErr) is a New(notOk customError)")
+	}
+}
+
+type errorWithCustomIs struct {
+	Key string
+	Err error
+}
+
+func (ewci errorWithCustomIs) Error() string {
+	return "["+ewci.Key+"]: " + ewci.Err.Error()
+}
+
+func (ewci errorWithCustomIs) Is(target error) bool {
+	matched, ok := target.(errorWithCustomIs)
+	return ok && matched.Key == ewci.Key
+}

+ 22 - 0
error_backward.go

@@ -0,0 +1,22 @@
+// +build !go1.13
+
+package errors
+
+// Is detects whether the error is equal to a given error. Errors	// Is detects whether the error is equal to a given error. Errors
+// are considered equal by this function if they are the same object,	// are considered equal by this function if they are matched by errors.Is
+// or if they both contain the same error inside an errors.Error.	// or if their contained errors are matched through errors.Is
+func Is(e error, original error) bool {
+	if e == original {
+		return true
+	}
+
+	if e, ok := e.(*Error); ok {
+		return Is(e.Err, original)
+	}
+
+	if original, ok := original.(*Error); ok {
+		return Is(e, original.Err)
+	}
+
+	return false
+}

+ 1 - 60
error_test.go

@@ -107,8 +107,8 @@ func TestNew(t *testing.T) {
 	}
 }
 
+// This test should work for any go version
 func TestIs(t *testing.T) {
-
 	if Is(nil, io.EOF) {
 		t.Errorf("nil is an error")
 	}
@@ -128,51 +128,6 @@ func TestIs(t *testing.T) {
 	if Is(io.EOF, fmt.Errorf("io.EOF")) {
 		t.Errorf("io.EOF is fmt.Errorf")
 	}
-
-	custErr := errorWithCustomIs{
-		Key: "TestForFun",
-		Err: io.EOF,
-	}
-
-	shouldMatch := errorWithCustomIs{
-		Key: "TestForFun",
-	}
-
-	shouldNotMatch := errorWithCustomIs{Key: "notOk"}
-
-	if !Is(custErr, shouldMatch) {
-		t.Errorf("custErr is not a TestForFun customError")
-	}
-
-	if Is(custErr, shouldNotMatch) {
-		t.Errorf("custErr is a notOk customError")
-	}
-
-	if !Is(custErr, New(shouldMatch)) {
-		t.Errorf("custErr is not a New(TestForFun customError)")
-	}
-
-	if Is(custErr, New(shouldNotMatch)) {
-		t.Errorf("custErr is a New(notOk customError)")
-	}
-
-	if !Is(New(custErr), shouldMatch) {
-		t.Errorf("New(custErr) is not a TestForFun customError")
-	}
-
-	if Is(New(custErr), shouldNotMatch) {
-		t.Errorf("New(custErr) is a notOk customError")
-	}
-
-	if !Is(New(custErr), New(shouldMatch)) {
-		t.Errorf("New(custErr) is not a New(TestForFun customError)")
-	}
-
-	if Is(New(custErr), New(shouldNotMatch)) {
-		t.Errorf("New(custErr) is a New(notOk customError)")
-	}
-
-
 }
 
 func TestWrapError(t *testing.T) {
@@ -361,17 +316,3 @@ func callersToFrames(callers []uintptr) []runtime.Frame {
 		}
 	}
 }
-
-type errorWithCustomIs struct {
-	Key string
-	Err error
-}
-
-func (ewci errorWithCustomIs) Error() string {
-	return "["+ewci.Key+"]: " + ewci.Err.Error()
-}
-
-func (ewci errorWithCustomIs) Is(target error) bool {
-	matched, ok := target.(errorWithCustomIs)
-	return ok && matched.Key == ewci.Key
-}