Skip to content

Commit

Permalink
Remove special build tag to test errors 1.13 functions. Go 1.13 or hi…
Browse files Browse the repository at this point in the history
…gher is now required
  • Loading branch information
marioizquierdo committed Jun 16, 2021
1 parent f2e3117 commit 82820bb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 66 deletions.
48 changes: 0 additions & 48 deletions errors_1_13_test.go

This file was deleted.

61 changes: 43 additions & 18 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 82820bb

Please sign in to comment.