Skip to content

Commit e50b63a

Browse files
authored
Merge pull request #11 from tri-adam/read-error
Add Func to Read Error
2 parents f3c4cf2 + 10d3e06 commit e50b63a

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

json_response.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,18 @@ func ReadResponse(r io.Reader, v interface{}) error {
110110
_, err := ReadResponsePage(r, v)
111111
return err
112112
}
113+
114+
// ReadError attempts to unmarshal JSON-encoded error details from the supplied reader. It returns
115+
// nil if an error could not be parsed from the response, or if the parsed error was nil.
116+
func ReadError(r io.Reader) error {
117+
var u struct {
118+
Error *Error `json:"error"`
119+
}
120+
if err := json.NewDecoder(r).Decode(&u); err != nil {
121+
return nil
122+
}
123+
if u.Error == nil {
124+
return nil
125+
}
126+
return u.Error
127+
}

json_response_test.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,14 @@ func TestReadResponseNil(t *testing.T) {
284284
}
285285

286286
tests := []struct {
287-
name string
288-
r io.Reader
289-
wantErr bool
290-
wantValue string
287+
name string
288+
r io.Reader
289+
wantErr bool
291290
}{
292-
{"Empty", bytes.NewReader(nil), true, ""},
293-
{"Nil", getResponseBody(nil), false, "blah"},
294-
{"Response", getResponseBody(TestStruct{"blah"}), false, "blah"},
295-
{"Error", getErrorBody("blah", http.StatusNotFound), true, ""},
291+
{"Empty", bytes.NewReader(nil), true},
292+
{"Nil", getResponseBody(nil), false},
293+
{"Response", getResponseBody(TestStruct{"blah"}), false},
294+
{"Error", getErrorBody("blah", http.StatusNotFound), true},
296295
}
297296
for _, tt := range tests {
298297
t.Run(tt.name, func(t *testing.T) {
@@ -303,3 +302,42 @@ func TestReadResponseNil(t *testing.T) {
303302
})
304303
}
305304
}
305+
306+
func TestReadError(t *testing.T) {
307+
type TestStruct struct {
308+
Value string
309+
}
310+
311+
tests := []struct {
312+
name string
313+
r io.Reader
314+
wantErr bool
315+
wantMessage string
316+
wantCode int
317+
}{
318+
{"Empty", bytes.NewReader(nil), false, "", 0},
319+
{"Response", getResponseBody(TestStruct{"blah"}), false, "", 0},
320+
{"Error", getErrorBody("blah", http.StatusNotFound), true, "blah", http.StatusNotFound},
321+
}
322+
for _, tt := range tests {
323+
t.Run(tt.name, func(t *testing.T) {
324+
err := ReadError(tt.r)
325+
if (err != nil) != tt.wantErr {
326+
t.Errorf("ReadError() error = %v, wantErr %v", err, tt.wantErr)
327+
}
328+
329+
if err != nil {
330+
err, ok := err.(*Error)
331+
if !ok {
332+
t.Fatal("invalid error type")
333+
}
334+
if got, want := err.Message, tt.wantMessage; got != want {
335+
t.Errorf("got message %v, want %v", got, want)
336+
}
337+
if got, want := err.Code, tt.wantCode; got != want {
338+
t.Errorf("got code %v, want %v", got, want)
339+
}
340+
}
341+
})
342+
}
343+
}

0 commit comments

Comments
 (0)