Skip to content

Commit 8c74e58

Browse files
committedDec 17, 2024··
feat: do not duplicate error message
1 parent 72397c2 commit 8c74e58

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed
 

‎client_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ func TestServiceCreateErrorsRetries(t *testing.T) {
134134
{
135135
Name: "with errors field, list",
136136
ResponseBody: `{"message": "Something went wrong", "errors": ["oh!", "no!"]}`,
137-
ErrorExpect: `[500 ServiceCreate]: Something went wrong: ["oh!","no!"]`,
137+
ErrorExpect: `[500 ServiceCreate]: Something went wrong (["oh!","no!"])`,
138138
RetryMax: 1,
139139
CallsExpect: 2,
140140
},
141141
{
142142
Name: "with errors field, string",
143143
ResponseBody: `{"message": "Something went wrong", "errors": "wow!"}`,
144-
ErrorExpect: `[500 ServiceCreate]: Something went wrong: "wow!"`,
144+
ErrorExpect: `[500 ServiceCreate]: Something went wrong ("wow!")`,
145145
RetryMax: 1,
146146
CallsExpect: 2,
147147
},

‎error.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,24 @@ type Error struct {
2323

2424
// Error concatenates all the fields.
2525
func (e Error) Error() string {
26-
// Must not use `%q` here which will escape every quote in the string,
27-
// which might break external substring checks
28-
msg := fmt.Sprintf(`[%d %s]: %s`, e.Status, e.OperationID, e.Message)
29-
if e.Errors == nil {
30-
return msg
31-
}
26+
msg := e.Message
27+
if e.Errors != nil {
28+
// Appends Errors if they don't contain the message
29+
// Otherwise, it will be duplicated
30+
b, err := json.Marshal(e.Errors)
31+
errMsg := string(b)
32+
if err != nil {
33+
errMsg = err.Error()
34+
}
3235

33-
errMerged, err := json.Marshal(e.Errors)
34-
if err != nil {
35-
errMerged = []byte(err.Error())
36+
if !strings.Contains(errMsg, msg) {
37+
msg = fmt.Sprintf("%s (%s)", msg, errMsg)
38+
}
3639
}
3740

38-
return fmt.Sprintf(`%s: %s`, msg, errMerged)
41+
// Must not use `%q` here which will escape every quote in the string,
42+
// which might break external substring checks
43+
return fmt.Sprintf(`[%d %s]: %s`, e.Status, e.OperationID, msg)
3944
}
4045

4146
// IsNotFound returns true if the specified error has status 404

‎error_test.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func TestFromResponse(t *testing.T) {
132132
}
133133
]
134134
}`),
135-
expectStringer: `[404 UserAuth]: Account does not exist: [{"error_code":"account_not_found","message":"Account does not exist","status":404}]`,
135+
expectStringer: `[404 UserAuth]: Account does not exist`,
136136
expectBytes: nil,
137137
expectErr: Error{
138138
Message: "Account does not exist",
@@ -179,6 +179,33 @@ func TestFromResponse(t *testing.T) {
179179
Status: http.StatusBadRequest,
180180
},
181181
},
182+
{
183+
name: "add errors, because message is different",
184+
operationID: "UserAuth",
185+
statusCode: http.StatusNotFound,
186+
body: []byte(`{
187+
"message": "Oh no!",
188+
"errors": [
189+
{
190+
"error_code": "account_not_found",
191+
"message": "Account does not exist",
192+
"status": 404
193+
}
194+
]
195+
}`),
196+
expectStringer: `[404 UserAuth]: Oh no! ([{"error_code":"account_not_found","message":"Account does not exist","status":404}])`,
197+
expectBytes: nil,
198+
expectErr: Error{
199+
Message: "Oh no!",
200+
Errors: []any{map[string]any{
201+
"error_code": "account_not_found",
202+
"message": "Account does not exist",
203+
"status": float64(404),
204+
}},
205+
OperationID: "UserAuth",
206+
Status: http.StatusNotFound,
207+
},
208+
},
182209
}
183210

184211
for _, opt := range cases {

0 commit comments

Comments
 (0)
Please sign in to comment.