-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
40 lines (34 loc) · 821 Bytes
/
error.go
File metadata and controls
40 lines (34 loc) · 821 Bytes
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
package gobaclient
import (
"encoding/json"
"errors"
"net/http"
)
// ErrorResponse represents the server's response to a failed request.
type ErrorResponse struct {
Error error `json:"error,omitempty"`
}
// UnmarshalJSON unmarshals data into resp.
func (resp *ErrorResponse) UnmarshalJSON(data []byte) error {
var raw struct {
Error string `json:"error,omitempty"`
}
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
if err := raw.Error; err != "" {
resp.Error = errors.New(err)
}
return nil
}
// checkResponse verifies the server's response.
func checkResponse(resp http.Response) error {
if resp.StatusCode == http.StatusOK {
return nil
}
var response ErrorResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return err
}
return response.Error
}