Passed
Push — master ( a2c608...ad6bf9 )
by Victor Hugo
59s
created

devto.unmarshalErrorResponse   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 8
nop 1
dl 0
loc 10
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
package devto
2
3
import (
4
	"encoding/json"
5
	"fmt"
6
	"io/ioutil"
7
	"net/http"
8
)
9
10
// nonSuccessfulResponse indicates that the status code for
11
// the HTTP response passed in was not one of the "success"
12
// (2XX) status codes
13 1
func nonSuccessfulResponse(res *http.Response) bool { return res.StatusCode/100 != 2 }
14
15
// attempt to deserialize the error response; if it succeeds,
16
// the error will be an ErrorResponse, otherwise it will be
17
// an error indicating that the error response could not be
18
// deserialized.
19
func unmarshalErrorResponse(res *http.Response) error {
20 1
	var e ErrorResponse
21 1
	if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
22
		return fmt.Errorf(
23
			`unexpected error deserializing %d response: "%v"`,
24
			res.StatusCode,
25
			err,
26
		)
27
	}
28 1
	return &e
29
}
30
31
func decodeResponse(r *http.Response) []byte {
32
	c, err := ioutil.ReadAll(r.Body)
33
	if err != nil {
34
		return []byte("")
35
	}
36
	defer r.Body.Close()
37
	return c
38
}
39