Adding a go version compatibility package.

Currently enables Errors.Is functionality as ErrorsIs.
Using this functionality in errors.go and unit tests.

Signed-off-by: Matthias Hanel <mh@synadia.com>
This commit is contained in:
Matthias Hanel
2020-02-25 18:05:07 -05:00
parent c18a5a2582
commit d8297ddf41
5 changed files with 84 additions and 5 deletions

View File

@@ -16,6 +16,7 @@ package server
import (
"errors"
"fmt"
"github.com/nats-io/nats-server/v2/server/gobackcomp"
)
var (
@@ -246,3 +247,7 @@ func UnpackIfErrorCtx(err error) string {
}
return err.Error()
}
func IsErr(err, target error) bool {
return gobackcomp.ErrorsIs(err, target)
}

View File

@@ -14,7 +14,6 @@
package server
import (
"errors"
"strings"
"testing"
)
@@ -29,7 +28,7 @@ func TestErrCtx(t *testing.T) {
if e == ErrWrongGateway {
t.Fatalf("%v and %v can't be compared this way", e, ErrWrongGateway)
}
if !errors.Is(e, ErrWrongGateway) {
if !IsErr(e, ErrWrongGateway) {
t.Fatalf("%s and %s ", e, ErrWrongGateway)
}
if UnpackIfErrorCtx(ErrWrongGateway) != ErrWrongGateway.Error() {
@@ -56,7 +55,7 @@ func TestErrCtxWrapped(t *testing.T) {
if e == ErrWrongGateway {
t.Fatalf("%v and %v can't be compared this way", e, ErrWrongGateway)
}
if !errors.Is(e, ErrWrongGateway) {
if !IsErr(e, ErrWrongGateway) {
t.Fatalf("%s and %s ", e, ErrWrongGateway)
}
if UnpackIfErrorCtx(ErrWrongGateway) != ErrWrongGateway.Error() {

View File

@@ -0,0 +1,23 @@
// Copyright 2020 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build go1.13
package gobackcomp
import "errors"
// implements: go 1.13 errors.Is(err, target error) bool
func ErrorsIs(err, target error) bool {
return errors.Is(err, target)
}

View File

@@ -0,0 +1,53 @@
// Copyright 2020 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !go1.13
package gobackcomp
// implements: go 1.13 errors.Unwrap(err error) error
func errorsUnwrap(err error) error {
u, ok := err.(interface {
Unwrap() error
})
if !ok {
return nil
}
return u.Unwrap()
}
// implements: go 1.13 errors.Is(err, target error) bool
func ErrorsIs(err, target error) bool {
// this is an outright copy of go 1.13 errors.Is(err, target error) bool
// removed isComparable
if target == nil {
return err == target
}
for {
if err == target {
return true
}
if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
return true
}
// TODO: consider supporing target.Is(err). This would allow
// user-definable predicates, but also may allow for coping with sloppy
// APIs, thereby making it easier to get away with them.
if err = errorsUnwrap(err); err == nil {
return false
}
}
return false
}

View File

@@ -15,7 +15,6 @@ package server
import (
"bytes"
"errors"
"testing"
)
@@ -583,7 +582,7 @@ func TestMaxControlLine(t *testing.T) {
pub := []byte("PUB foo.bar 11\r")
err := c.parse(pub)
if !errors.Is(err, ErrMaxControlLine) {
if !IsErr(err, ErrMaxControlLine) {
t.Fatalf("Expected an error parsing longer than expected control line")
}
}