client.*Response.Error   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
dl 0
loc 6
c 0
b 0
f 0
rs 10
nop 0
1
package client
2
3
import (
4
	"bytes"
5
	"errors"
6
	"net/http"
7
	"strconv"
8
)
9
10
// Response captures the http response
11
type Response struct {
12
	HTTPResponse *http.Response
13
	Body         *[]byte
14
}
15
16
// Error ensures that the response can be decoded into a string inc ase it's an error response
17
func (r *Response) Error() error {
18
	switch r.HTTPResponse.StatusCode {
19
	case 200, 201, 202, 204, 205:
20
		return nil
21
	default:
22
		return errors.New(r.errorMessage())
23
	}
24
}
25
26
func (r *Response) errorMessage() string {
27
	var buf bytes.Buffer
28
	buf.WriteString(strconv.Itoa(r.HTTPResponse.StatusCode))
29
	buf.WriteString(": ")
30
	buf.WriteString(http.StatusText(r.HTTPResponse.StatusCode))
31
	buf.WriteString(", Body: ")
32
	buf.Write(*r.Body)
33
34
	return buf.String()
35
}
36