This repository was archived by the owner on Feb 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.go
More file actions
211 lines (165 loc) · 3.48 KB
/
message.go
File metadata and controls
211 lines (165 loc) · 3.48 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package httpe
import (
"encoding/json"
"fmt"
)
type Result struct {
Code string `json:"code"`
Payload any `json:"payload"`
}
type SuccessResponse struct {
Result Result `json:"result"`
}
func (er SuccessResponse) MarshalJSON() ([]byte, error) {
type r SuccessResponse
var resp r = r(er)
return json.Marshal(resp)
}
type ErrorResponse struct {
ErrorStruct struct {
Code string `json:"code"`
Description error `json:"description"`
} `json:"error"`
}
func (e ErrorResponse) Error() string {
return e.ErrorStruct.Description.Error()
}
type Code string
type Message struct {
Code Code `json:"code"`
Metadata map[string]string `json:"metadata"`
Payload any `json:"payload"`
}
func (m Message) Unwrap() error {
e, ok := m.Payload.(ErrorResponse)
if !ok {
return nil
}
var currentErr error = e.ErrorStruct.Description
for {
if currentErr == nil {
return nil
}
httpErr, okErr := currentErr.(statusError)
if okErr {
return httpErr
}
e, ok := currentErr.(interface {
Unwrap() error
Error() string
})
if !ok {
return e
}
currentErr = e.Unwrap()
}
}
func (m Message) Error() string {
e, ok := m.Payload.(ErrorResponse)
if !ok {
return ""
}
return e.Error()
}
func newMessage(code Code, payload any) (Message, error) {
return Message{
Code: code,
Metadata: make(map[string]string),
Payload: payload,
}, nil
}
func (m Message) AddMetadata(key, value string) {
m.Metadata[key] = value
}
func (m Message) MarshalJSON() ([]byte, error) {
type pack Message
p := pack(m)
return json.Marshal(p)
}
func NewErrorMessage(code Code, message string, httpStatusCode int) []byte {
msg := NewErrorMessageRaw(code, message, httpStatusCode)
out, _ := msg.MarshalJSON()
return out
}
type fakeErr struct {
payload any
status int
}
func (e fakeErr) StatusCode() int {
return e.status
}
func (e fakeErr) Unwrap() error {
return statusError{
Err: e,
Status: e.status,
}
}
func (e fakeErr) GetError() error {
b, _ := e.MarshalJSON()
return fmt.Errorf("%s", string(b))
}
func (e fakeErr) MarshalJSON() ([]byte, error) {
return json.Marshal(e.payload)
}
func (e fakeErr) Error() string {
b, _ := json.Marshal(e.payload)
return string(b)
}
func NewErrorMessageRaw(code Code, errMessage any, httpStatusCode int) Message {
var msgErr StatusError
switch v := errMessage.(type) {
case string:
msgErr = statusError{
Err: fmt.Errorf("%s", string(v)),
Status: httpStatusCode,
}
case error:
msgErr = statusError{
Err: v,
Status: httpStatusCode,
}
default:
msgErr = fakeErr{
payload: errMessage,
status: httpStatusCode,
}
}
errMsg := ErrorResponse{
ErrorStruct: struct {
Code string `json:"code"`
Description error `json:"description"`
}{
Code: string(code),
Description: msgErr,
},
}
msg, _ := newMessage(code, errMsg)
return msg
}
func NewSuccessMessageRaw(code Code, payload any) Message {
sucessMsg := SuccessResponse{
Result: struct {
Code string `json:"code"`
Payload any `json:"payload"`
}{
Code: "success",
Payload: payload,
},
}
msg, _ := newMessage(code, sucessMsg)
return msg
}
func NewSuccessMessage(code Code, payload any) []byte {
sucessMsg := SuccessResponse{
Result: struct {
Code string `json:"code"`
Payload any `json:"payload"`
}{
Code: "success",
Payload: payload,
},
}
msg, _ := newMessage(code, sucessMsg)
out, _ := msg.MarshalJSON()
return out
}