Completed
Pull Request — master (#5)
by
unknown
02:01
created

devto.non2xx   A

Complexity

Conditions 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nop 1
dl 0
loc 1
ccs 1
cts 1
cp 1
crap 1
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 1
func non2xx(res *http.Response) bool { return res.StatusCode/100 != 2 }
11
12
// attempt to deserialize the error response; if it succeeds,
13
// the error will be an ErrorResponse, otherwise it will be
14
// an error indicating that the error response could not be
15
// deserialized.
16
func unmarshalErrorResponse(res *http.Response) error {
17 1
	var e ErrorResponse
18 1
	if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
19
		return fmt.Errorf(
20
			`unexpected error deserializing %d response: "%v"`,
21
			res.StatusCode,
22
			err,
23
		)
24
	}
25 1
	return &e
26
}
27
28
func decodeResponse(r *http.Response) []byte {
29
	c, err := ioutil.ReadAll(r.Body)
30
	if err != nil {
31
		return []byte("")
32
	}
33
	defer r.Body.Close()
34
	return c
35
}
36