-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
154 lines (141 loc) · 3.76 KB
/
Copy patherror.go
File metadata and controls
154 lines (141 loc) · 3.76 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
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
152
153
154
package mbz
import (
"encoding/json"
"fmt"
"io"
"net/http"
"slices"
"connectrpc.com/connect"
mbzv1 "github.com/way-platform/mbz-go/proto/gen/go/wayplatform/connect/mbz/v1"
"google.golang.org/protobuf/proto"
)
// ResponseError represents a Mercedes-Benz API response error.
type ResponseError struct {
// StatusCode is the HTTP status code of the response.
StatusCode int
// Body is the raw response body.
Body []byte
// Path is the API path that returned the error.
Path string
}
func newResponseError(httpResponse *http.Response) error {
body, err := io.ReadAll(httpResponse.Body)
if err != nil {
body = fmt.Appendf(nil, "failed to read response body: %s", err)
}
respErr := &ResponseError{
StatusCode: httpResponse.StatusCode,
Body: body,
}
if httpResponse.Request != nil && httpResponse.Request.URL != nil {
respErr.Path = httpResponse.Request.URL.Path
}
return respErr.connectError()
}
func (e *ResponseError) Error() string {
if len(e.Body) > 0 {
return fmt.Sprintf("http %d: %s", e.StatusCode, string(e.Body))
}
return fmt.Sprintf("http %d", e.StatusCode)
}
func (e *ResponseError) connectError() error {
connErr := connect.NewError(httpStatusToConnectCode(e.StatusCode), e)
if detail := e.errorDetail(); detail != nil {
d, err := connect.NewErrorDetail(detail)
if err == nil {
connErr.AddDetail(d)
}
}
return connErr
}
func (e *ResponseError) errorDetail() proto.Message {
var raw problemDetailJSON
if err := json.Unmarshal(e.Body, &raw); err != nil {
return nil
}
if raw.Type == "" && raw.Title == "" && raw.Detail == "" {
return nil
}
problemType := apiEnumValueToProto(raw.Type)
b := mbzv1.ProblemDetail_builder{
Type: &problemType,
Title: optString(raw.Title),
Detail: optString(raw.Detail),
Instance: optString(raw.Instance),
Status: optInt32(raw.status()),
}
if raw.Type != "" {
b.TypeUri = &raw.Type
}
return b.Build()
}
type problemDetailJSON struct {
Type string `json:"type"`
Title string `json:"title"`
Detail string `json:"detail"`
Instance string `json:"instance"`
Status int32 `json:"status"`
StatusCode int32 `json:"statusCode"`
}
func (p *problemDetailJSON) status() int32 {
if p.Status != 0 {
return p.Status
}
return p.StatusCode
}
func apiEnumValueToProto(typeURI string) mbzv1.ProblemDetail_Type {
if typeURI == "" {
return mbzv1.ProblemDetail_TYPE_UNSPECIFIED
}
enumDesc := mbzv1.ProblemDetail_TYPE_UNSPECIFIED.Descriptor()
values := enumDesc.Values()
for i := range values.Len() {
valueDesc := values.Get(i)
opts := valueDesc.Options()
if proto.HasExtension(opts, mbzv1.E_ApiEnumValue) {
apiValues := proto.GetExtension(opts, mbzv1.E_ApiEnumValue).([]string)
if slices.Contains(apiValues, typeURI) {
return mbzv1.ProblemDetail_Type(valueDesc.Number())
}
}
}
return mbzv1.ProblemDetail_TYPE_UNRECOGNIZED
}
func optString(s string) *string {
if s == "" {
return nil
}
return &s
}
func optInt32(v int32) *int32 {
if v == 0 {
return nil
}
return &v
}
func httpStatusToConnectCode(statusCode int) connect.Code {
switch statusCode {
case http.StatusBadRequest:
return connect.CodeInvalidArgument
case http.StatusUnauthorized:
return connect.CodeUnauthenticated
case http.StatusForbidden:
return connect.CodePermissionDenied
case http.StatusNotFound:
return connect.CodeNotFound
case http.StatusConflict:
return connect.CodeAlreadyExists
case http.StatusTooManyRequests:
return connect.CodeResourceExhausted
case http.StatusNotImplemented:
return connect.CodeUnimplemented
case http.StatusServiceUnavailable:
return connect.CodeUnavailable
case http.StatusGatewayTimeout:
return connect.CodeDeadlineExceeded
case http.StatusInternalServerError:
return connect.CodeInternal
default:
return connect.CodeUnknown
}
}