forked from jarias/stormpath-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
54 lines (46 loc) · 1.26 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package stormpath
import (
"encoding/json"
"net/http"
)
//Error maps a Stormpath API JSON error object which implements Go error interface
type Error struct {
RequestID string
Status int `json:"status"`
Code int `json:"code"`
Message string `json:"message"`
DeveloperMessage string `json:"developerMessage"`
MoreInfo string `json:"moreInfo"`
OAuth2Error string `json:"error"`
}
func (e Error) Error() string {
return e.String()
}
func (e Error) String() string {
return e.Message
}
func handleResponseError(req *http.Request, resp *http.Response, err error) error {
//Error from the request execution
if err != nil {
if req != nil {
Logger.Printf("[ERROR] %s [%s]", err, req.URL.String())
}
return err
}
//Check for Stormpath specific errors
if resp.StatusCode != http.StatusOK &&
resp.StatusCode != http.StatusAccepted &&
resp.StatusCode != http.StatusNoContent &&
resp.StatusCode != http.StatusCreated &&
resp.StatusCode != http.StatusFound {
spError := &Error{}
err := json.NewDecoder(resp.Body).Decode(spError)
if err != nil {
return err
}
spError.RequestID = resp.Header.Get("Stormpath-Request-Id")
return *spError
}
//No errors from the request execution
return nil
}