/
error.go
59 lines (51 loc) · 1.21 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
55
56
57
58
59
package osiam
import (
"encoding/json"
)
// OsiamError represents an error response from osiam.
type OsiamError struct {
Error string
Message string
}
// ParseOsiamError creates an OsiamError out of a json
func ParseOsiamError(jsonBody []byte) OsiamError {
m := map[string]interface{}{}
err := json.Unmarshal(jsonBody, &m)
if err != nil {
return OsiamError{
"client_parse_error",
"osiam response is no valid json: " + string(jsonBody),
}
}
e := OsiamError{}
if v, exist := m["error"]; exist {
if vCasted, ok := v.(string); ok {
e.Error = vCasted
}
}
if v, exist := m["message"]; exist {
if vCasted, ok := v.(string); ok {
e.Message = vCasted
}
}
if v, exist := m["error_description"]; exist {
if vCasted, ok := v.(string); ok {
e.Message = vCasted
}
}
if e.Error == "" && e.Message == "" {
return OsiamError{
"client_parse_error",
"not a valid osiam error message: " + string(jsonBody),
}
}
return e
}
// IsLoginError checks, if the grant was invalid
func (e OsiamError) IsLoginError() bool {
return e.Error == "invalid_grant"
}
// IsUnauthorized checks, if the error reason was Unauthorized
func (e OsiamError) IsUnauthorized() bool {
return e.Error == "Unauthorized"
}