File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,16 @@ type Request struct {
29
29
Body []byte
30
30
}
31
31
32
+ // RestError is a struct for an error handling.
33
+ type RestError struct {
34
+ Response * Response
35
+ }
36
+
37
+ // Error is the implementation of the error interface.
38
+ func (e * RestError ) Error () string {
39
+ return e .Response .Body
40
+ }
41
+
32
42
// DefaultClient is used if no custom HTTP client is defined
33
43
var DefaultClient = & Client {HTTPClient : http .DefaultClient }
34
44
Original file line number Diff line number Diff line change @@ -164,3 +164,27 @@ func TestCustomHTTPClient(t *testing.T) {
164
164
t .Error ("We did not receive the Timeout error" )
165
165
}
166
166
}
167
+
168
+ func TestRestError (t * testing.T ) {
169
+ headers := make (map [string ][]string )
170
+ headers ["Content-Type" ] = []string {"application/json" }
171
+
172
+ response := & Response {
173
+ StatusCode : 400 ,
174
+ Body : `{"result": "failure"}` ,
175
+ Headers : headers ,
176
+ }
177
+
178
+ restErr := & RestError {Response : response }
179
+
180
+ var err error
181
+ err = restErr
182
+
183
+ if _ , ok := err .(* RestError ); ! ok {
184
+ t .Error ("RestError does not satisfiy the error interface." )
185
+ }
186
+
187
+ if err .Error () != `{"result": "failure"}` {
188
+ t .Error ("Invalid error message." )
189
+ }
190
+ }
You can’t perform that action at this time.
0 commit comments