forked from connectrpc/connect-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
151 lines (141 loc) · 4.22 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2021-2024 The Connect 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.
package connect
import (
"errors"
"fmt"
"strings"
"testing"
"time"
"connectrpc.com/connect/internal/assert"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/emptypb"
)
func TestErrorNilUnderlying(t *testing.T) {
t.Parallel()
err := NewError(CodeUnknown, nil)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), CodeUnknown.String())
assert.Equal(t, err.Code(), CodeUnknown)
assert.Zero(t, err.Details())
detail, detailErr := NewErrorDetail(&emptypb.Empty{})
assert.Nil(t, detailErr)
err.AddDetail(detail)
assert.Equal(t, len(err.Details()), 1)
assert.Equal(t, err.Details()[0].Type(), "google.protobuf.Empty")
err.Meta().Set("Foo", "bar")
assert.Equal(t, err.Meta().Get("Foo"), "bar")
assert.Equal(t, CodeOf(err), CodeUnknown)
}
func TestErrorFormatting(t *testing.T) {
t.Parallel()
assert.Equal(
t,
NewError(CodeUnavailable, errors.New("")).Error(),
CodeUnavailable.String(),
)
got := NewError(CodeUnavailable, errors.New("Foo")).Error()
assert.True(t, strings.Contains(got, CodeUnavailable.String()))
assert.True(t, strings.Contains(got, "Foo"))
}
func TestErrorCode(t *testing.T) {
t.Parallel()
err := fmt.Errorf(
"another: %w",
NewError(CodeUnavailable, errors.New("foo")),
)
connectErr, ok := asError(err)
assert.True(t, ok)
assert.Equal(t, connectErr.Code(), CodeUnavailable)
}
func TestCodeOf(t *testing.T) {
t.Parallel()
assert.Equal(
t,
CodeOf(NewError(CodeUnavailable, errors.New("foo"))),
CodeUnavailable,
)
assert.Equal(t, CodeOf(errors.New("foo")), CodeUnknown)
}
func TestErrorDetails(t *testing.T) {
t.Parallel()
second := durationpb.New(time.Second)
detail, err := NewErrorDetail(second)
assert.Nil(t, err)
connectErr := NewError(CodeUnknown, errors.New("error with details"))
assert.Zero(t, connectErr.Details())
connectErr.AddDetail(detail)
assert.Equal(t, len(connectErr.Details()), 1)
unmarshaled, err := connectErr.Details()[0].Value()
assert.Nil(t, err)
assert.Equal(t, unmarshaled, proto.Message(second))
secondBin, err := proto.Marshal(second)
assert.Nil(t, err)
assert.Equal(t, detail.Bytes(), secondBin)
}
func TestErrorIs(t *testing.T) {
t.Parallel()
// errors.New and fmt.Errorf return *errors.errorString. errors.Is
// considers two *errors.errorStrings equal iff they have the same address.
err := errors.New("oh no")
assert.False(t, errors.Is(err, errors.New("oh no")))
assert.True(t, errors.Is(err, err))
// Our errors should have the same semantics. Note that we'd need to extend
// the ErrorDetail interface to support value equality.
connectErr := NewError(CodeUnavailable, err)
assert.False(t, errors.Is(connectErr, NewError(CodeUnavailable, err)))
assert.True(t, errors.Is(connectErr, connectErr))
}
func TestTypeNameFromURL(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
url string
typeName string
}{
{
name: "no-prefix",
url: "foo.bar.Baz",
typeName: "foo.bar.Baz",
},
{
name: "standard-prefix",
url: defaultAnyResolverPrefix + "foo.bar.Baz",
typeName: "foo.bar.Baz",
},
{
name: "different-hostname",
url: "abc.com/foo.bar.Baz",
typeName: "foo.bar.Baz",
},
{
name: "additional-path-elements",
url: defaultAnyResolverPrefix + "abc/def/foo.bar.Baz",
typeName: "foo.bar.Baz",
},
{
name: "full-url",
url: "https://abc.com/abc/def/foo.bar.Baz",
typeName: "foo.bar.Baz",
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, typeNameFromURL(testCase.url), testCase.typeName)
})
}
}