-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_error.go
42 lines (37 loc) · 1.11 KB
/
http_error.go
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
// Copyright 2015 Alex Browne. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.
package rest
import (
"fmt"
"io/ioutil"
"net/http"
)
// HTTPError is returned whenever rest gets a non-2xx response from
// the server.
type HTTPError struct {
// URL is the url that the request was sent to
URL string
// Body is the body of the response
Body []byte
// StatusCode is the http status code of the response
StatusCode int
}
// Error satisfies the error interface
func (e HTTPError) Error() string {
return fmt.Sprintf("rest: http request to %s returned status code %d", e.URL, e.StatusCode)
}
// newHTTPError returns an HTTPError based on the given response. It
// may return a different error if there was a problem reading the response
// body.
func newHTTPError(res *http.Response) error {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("rest: Unexpected error reading response body: %s", err.Error())
}
return HTTPError{
URL: res.Request.URL.String(),
Body: body,
StatusCode: res.StatusCode,
}
}