From d8297ddf4151a280590204608ae96e7bd545b8f7 Mon Sep 17 00:00:00 2001 From: Matthias Hanel Date: Tue, 25 Feb 2020 18:05:07 -0500 Subject: [PATCH] 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 --- server/errors.go | 5 ++++ server/errors_test.go | 5 ++-- server/gobackcomp/go113.go | 23 ++++++++++++++ server/gobackcomp/go113_prev.go | 53 +++++++++++++++++++++++++++++++++ server/parser_test.go | 3 +- 5 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 server/gobackcomp/go113.go create mode 100644 server/gobackcomp/go113_prev.go diff --git a/server/errors.go b/server/errors.go index 29794321..3d111e93 100644 --- a/server/errors.go +++ b/server/errors.go @@ -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) +} diff --git a/server/errors_test.go b/server/errors_test.go index 2b39b7ea..76997357 100644 --- a/server/errors_test.go +++ b/server/errors_test.go @@ -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() { diff --git a/server/gobackcomp/go113.go b/server/gobackcomp/go113.go new file mode 100644 index 00000000..b7319a2e --- /dev/null +++ b/server/gobackcomp/go113.go @@ -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) +} diff --git a/server/gobackcomp/go113_prev.go b/server/gobackcomp/go113_prev.go new file mode 100644 index 00000000..2554fff8 --- /dev/null +++ b/server/gobackcomp/go113_prev.go @@ -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 +} diff --git a/server/parser_test.go b/server/parser_test.go index 46480616..fbaa81c9 100644 --- a/server/parser_test.go +++ b/server/parser_test.go @@ -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") } }