forked from connectrpc/connect-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
109 lines (100 loc) · 3.33 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
// Copyright 2021-2023 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))
}