-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec_test.go
More file actions
92 lines (78 loc) · 4.28 KB
/
codec_test.go
File metadata and controls
92 lines (78 loc) · 4.28 KB
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
package errors
import (
"bytes"
"database/sql"
"encoding/json"
"errors"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
const (
rawFullError = `{"error":{"code":500,"message":"msg: sql: connection is already closed","status":"INTERNAL","details":[{"@type":"type.googleapis.com/google.rpc.RetryInfo","retryDelay":"1.1s"},{"@type":"type.googleapis.com/google.rpc.ResourceInfo","resourceType":"1","resourceName":"2","owner":"3","description":"4"},{"@type":"type.googleapis.com/google.rpc.BadRequest","fieldViolations":[{"field":"1","description":"2"},{"field":"3","description":"4"}]},{"@type":"type.googleapis.com/google.rpc.PreconditionFailure","violations":[{"type":"1","subject":"2","description":"3"},{"type":"4","subject":"5","description":"6"}]},{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"1","domain":"2","metadata":{"3":"4"}},{"@type":"type.googleapis.com/google.rpc.QuotaFailure","violations":[{"subject":"1","description":"2"},{"subject":"3","description":"4"}]},{"@type":"type.googleapis.com/google.rpc.DebugInfo","stackEntries":["1","2"],"detail":"3"},{"@type":"type.googleapis.com/google.rpc.RequestInfo","requestId":"1","servingData":"2"},{"@type":"type.googleapis.com/google.rpc.Help","links":[{"description":"1","url":"2"},{"description":"3","url":"4"}]},{"@type":"type.googleapis.com/google.rpc.LocalizedMessage","local":"1","message":"2"},{"1":"2","@type":"custom/type"}]}}`
rawNoDebugInfo = `{"error":{"code":500,"message":"msg: sql: connection is already closed","status":"INTERNAL","details":[{"@type":"type.googleapis.com/google.rpc.RetryInfo","retryDelay":"1.1s"},{"@type":"type.googleapis.com/google.rpc.ResourceInfo","resourceType":"1","resourceName":"2","owner":"3","description":"4"},{"@type":"type.googleapis.com/google.rpc.BadRequest","fieldViolations":[{"field":"1","description":"2"},{"field":"3","description":"4"}]},{"@type":"type.googleapis.com/google.rpc.PreconditionFailure","violations":[{"type":"1","subject":"2","description":"3"},{"type":"4","subject":"5","description":"6"}]},{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"1","domain":"2","metadata":{"3":"4"}},{"@type":"type.googleapis.com/google.rpc.QuotaFailure","violations":[{"subject":"1","description":"2"},{"subject":"3","description":"4"}]},{"@type":"type.googleapis.com/google.rpc.RequestInfo","requestId":"1","servingData":"2"},{"@type":"type.googleapis.com/google.rpc.Help","links":[{"description":"1","url":"2"},{"description":"3","url":"4"}]},{"@type":"type.googleapis.com/google.rpc.LocalizedMessage","local":"1","message":"2"},{"1":"2","@type":"custom/type"}]}}`
)
var fullError = &annotated{
cause: sql.ErrConnDone,
code: Internal,
message: msg + ": " + sql.ErrConnDone.Error(),
details: []Any{
retryInfo,
resourceInfo,
badRequest,
preconditionFailure,
errInfo,
quotaFailure,
debugInfo,
requestInfo,
help,
localizedMessage,
any,
},
}
func TestEncode(t *testing.T) {
t.Run("encode", func(t *testing.T) {
encode := func(raw string, mappers ...DetailMapper) {
var buf bytes.Buffer
enc := NewEncoder(json.NewEncoder(&buf))
enc.Mappers = mappers
err := enc.Encode(fullError)
if assert.NoError(t, err) {
assert.Equal(t, raw, strings.TrimSpace(buf.String()))
}
}
encode(rawFullError)
encode(rawNoDebugInfo, HideDebugInfo)
})
t.Run("no encoder", func(t *testing.T) {
enc := NewEncoder(nil)
err := enc.Encode(Internal)
assert.True(t, errors.Is(err, ErrNoEncoder))
})
}
func TestDecode(t *testing.T) {
t.Run("decode", func(t *testing.T) {
r := strings.NewReader(rawFullError)
dec := NewDecoder(json.NewDecoder(r))
err := dec.Decode()
assert.True(t, errors.Is(err, Internal))
assert.True(t, strings.HasSuffix(err.Error(), fullError.cause.Error()))
assert.Len(t, fullError.details, len(err.(*annotated).details))
for i := range details {
ex := fullError.details[i]
ac := err.(*annotated).details[i]
assert.Equal(t, ex.TypeUrl(), ac.TypeUrl())
}
})
t.Run("no decoder", func(t *testing.T) {
dec := NewDecoder(nil)
err := dec.Decode()
assert.True(t, errors.Is(err, ErrNoDecoder))
})
}
func TestCustomType(t *testing.T) {
Register(typeUrlCustom, func() Any { return new(AnyDetail) })
assert.Contains(t, typeProvider, typeUrlCustom)
Register(typeUrlCustom, nil)
assert.NotContains(t, typeProvider, typeUrlCustom)
}