forked from mitchellh/go-bnet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
36 lines (29 loc) · 762 Bytes
/
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
package bnet
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// ErrorResponse is the error response structure from the Battle.net API
type ErrorResponse struct {
Response *http.Response
Code string `json:"error"`
Description string `json:"error_description"`
Scope string `json:"scope"`
}
func (r *ErrorResponse) Error() string {
return fmt.Sprintf("%s: %s", r.Code, r.Description)
}
// CheckError checks for an error in the given response.
func CheckError(r *http.Response) error {
if c := r.StatusCode; 200 <= c && c <= 299 {
return nil
}
errorResponse := &ErrorResponse{Response: r}
data, err := ioutil.ReadAll(r.Body)
if err == nil && data != nil {
json.Unmarshal(data, errorResponse)
}
return errorResponse
}