diff --git a/errors_1_13_test.go b/errors_1_13_test.go deleted file mode 100644 index eb56fe47..00000000 --- a/errors_1_13_test.go +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2018 Twitch Interactive, Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"). You may not -// use this file except in compliance with the License. A copy of the License is -// located at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// or in the "license" file accompanying this file. This file 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 twirp_test - -import ( - "errors" - "fmt" - "testing" - - "github.com/twitchtv/twirp" -) - -type myError string - -func (e myError) Error() string { - return string(e) -} - -func TestInternalErrorWith_Unwrap(t *testing.T) { - myErr := myError("myError") - wrErr := fmt.Errorf("wrapped: %w", myErr) // double wrap - twerr := twirp.InternalErrorWith(wrErr) - - if !errors.Is(twerr, myErr) { - t.Errorf("expected errors.Is to match the error wrapped by twirp.InternalErrorWith") - } - - var errTarget myError - if !errors.As(twerr, &errTarget) { - t.Errorf("expected errors.As to match the error wrapped by twirp.InternalErrorWith") - } - if errTarget.Error() != myErr.Error() { - t.Errorf("invalid value for errTarget.Error(). have=%q, want=%q", errTarget.Error(), myErr.Error()) - } -} diff --git a/errors_test.go b/errors_test.go index 8711e08d..c77b5879 100644 --- a/errors_test.go +++ b/errors_test.go @@ -15,12 +15,13 @@ package twirp import ( "encoding/json" + "errors" "fmt" "net/http/httptest" "sync" "testing" - "github.com/pkg/errors" + pkgerrors "github.com/pkg/errors" ) func TestWithMetaRaces(t *testing.T) { @@ -43,15 +44,53 @@ func TestWithMetaRaces(t *testing.T) { } } -func TestErrorCause(t *testing.T) { - rootCause := errors.New("this is only a test") +func TestPkgErrorCause(t *testing.T) { + rootCause := pkgerrors.New("this is only a test") twerr := InternalErrorWith(rootCause) - cause := errors.Cause(twerr) + cause := pkgerrors.Cause(twerr) if cause != rootCause { t.Errorf("got wrong cause for err. have=%q, want=%q", cause, rootCause) } } +func TestWrapError(t *testing.T) { + rootCause := errors.New("cause") + twerr := NewError(NotFound, "it ain't there") + err := WrapError(twerr, rootCause) + cause := pkgerrors.Cause(err) + if cause != rootCause { + t.Errorf("got wrong cause. got=%q, want=%q", cause, rootCause) + } + wantMsg := "twirp error not_found: it ain't there" + if gotMsg := err.Error(); gotMsg != wantMsg { + t.Errorf("got wrong error text. got=%q, want=%q", gotMsg, wantMsg) + } +} + +type myError string + +func (e myError) Error() string { + return string(e) +} + +func TestInternalErrorWith_Unwrap(t *testing.T) { + myErr := myError("myError") + wrErr := fmt.Errorf("wrapped: %w", myErr) // double wrap + twerr := InternalErrorWith(wrErr) + + if !errors.Is(twerr, myErr) { + t.Errorf("expected errors.Is to match the error wrapped by twirp.InternalErrorWith") + } + + var errTarget myError + if !errors.As(twerr, &errTarget) { + t.Errorf("expected errors.As to match the error wrapped by twirp.InternalErrorWith") + } + if errTarget.Error() != myErr.Error() { + t.Errorf("invalid value for errTarget.Error(). have=%q, want=%q", errTarget.Error(), myErr.Error()) + } +} + type errorResponeWriter struct { *httptest.ResponseRecorder } @@ -132,17 +171,3 @@ func TestWriteError_WithNonTwirpError(t *testing.T) { return } } - -func TestWrapError(t *testing.T) { - rootCause := errors.New("cause") - twerr := NewError(NotFound, "it ain't there") - err := WrapError(twerr, rootCause) - cause := errors.Cause(err) - if cause != rootCause { - t.Errorf("got wrong cause. got=%q, want=%q", cause, rootCause) - } - wantMsg := "twirp error not_found: it ain't there" - if gotMsg := err.Error(); gotMsg != wantMsg { - t.Errorf("got wrong error text. got=%q, want=%q", gotMsg, wantMsg) - } -}