-
Notifications
You must be signed in to change notification settings - Fork 6
/
errors.go
99 lines (92 loc) · 3.21 KB
/
errors.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
// Copyright 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 internal
import (
"errors"
v1 "connectrpc.com/conformance/internal/gen/proto/go/connectrpc/conformance/v1"
"connectrpc.com/connect"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
// ConvertErrorToConnectError converts the given error to a Connect error
// If err is nil, function will also return nil. If err is not
// of type connect.Error, a Connect error of code Unknown is returned.
func ConvertErrorToConnectError(err error) *connect.Error {
if err == nil {
return nil
}
connectErr := new(connect.Error)
if !errors.As(err, &connectErr) {
connectErr = connect.NewError(connect.CodeUnknown, err)
}
return connectErr
}
// ConvertErrorToProtoError converts the given error to a proto Error
// If err is nil, function will also return nil. If err is not
// of type connect.Error, a code representing Unknown is returned.
func ConvertErrorToProtoError(err error) *v1.Error {
if err == nil {
return nil
}
connectErr := new(connect.Error)
if !errors.As(err, &connectErr) {
return &v1.Error{
Code: int32(connect.CodeUnknown),
Message: proto.String(err.Error()),
}
}
return ConvertConnectToProtoError(connectErr)
}
// ConvertConnectToProtoError converts the given Connect error to a
// proto Error message. If err is nil, the function will also
// return nil.
func ConvertConnectToProtoError(err *connect.Error) *v1.Error {
if err == nil {
return nil
}
protoErr := &v1.Error{
Code: int32(err.Code()),
Message: proto.String(err.Message()),
}
details := make([]*anypb.Any, 0, len(err.Details()))
for _, detail := range err.Details() {
details = append(details, &anypb.Any{
// Connect Go strips the prefix from the type when calling Type()
// but anypb.MarshalFrom adds the prefix explicitly. Since Protoyaml
// uses anypb.MarshalFrom when reading an Any type from a yaml file,
// it must be explicitly added back here so that we can successfully
// compare the expected response from the yaml file into what
// Connect Go returns.
TypeUrl: DefaultAnyResolverPrefix + detail.Type(),
Value: detail.Bytes(),
})
}
protoErr.Details = details
return protoErr
}
// ConvertProtoToConnectError creates a Connect error from the given proto Error message.
func ConvertProtoToConnectError(err *v1.Error) *connect.Error {
if err == nil {
return nil
}
connectErr := connect.NewError(connect.Code(err.Code), errors.New(err.GetMessage()))
for _, detail := range err.Details {
connectDetail, err := connect.NewErrorDetail(detail)
if err != nil {
return connect.NewError(connect.CodeInternal, err)
}
connectErr.AddDetail(connectDetail)
}
return connectErr
}