-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
58 lines (48 loc) · 1.2 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package btp
import (
"bytes"
"fmt"
"io"
"net/http"
)
// Error an error type containing the http response from API
type Error struct {
body []byte
resp *http.Response
}
// NewError is a function to initialize the Error type. This function will be useful
// for unit testing and mocking purposes in the client side
// to test their behavior by the API response.
func NewError(body []byte, resp *http.Response) Error {
return Error{
body: body,
resp: resp,
}
}
// Error the error string for this error
func (e Error) Error() string {
msg := string(e.body)
if msg == "" {
msg = http.StatusText(e.Status())
}
return fmt.Sprintf("%d: %s", e.resp.StatusCode, msg)
}
// Body is the Body of the HTTP response
func (e Error) Body() io.ReadCloser {
return io.NopCloser(bytes.NewBuffer(e.body))
}
// Headers the HTTP headers returned from API
func (e Error) Headers() http.Header {
return e.resp.Header
}
// Status the HTTP status code returned from API
func (e Error) Status() int {
return e.resp.StatusCode
}
// OptionsError is an error type for invalid option argument.
type OptionsError struct {
opts interface{}
}
func (e *OptionsError) Error() string {
return fmt.Sprintf("invalid options: %v", e.opts)
}