-
Notifications
You must be signed in to change notification settings - Fork 8
/
errors.go
68 lines (57 loc) · 1.84 KB
/
errors.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
60
61
62
63
64
65
66
67
68
package freee
import "encoding/json"
const (
UnauthorizedCodeInvalidAccessToken = "invalid_access_token"
UnauthorizedCodeExpiredAccessToken = "expired_access_token"
UnauthorizedCodeUserDoNotHavePermission = "user_do_not_have_permission"
UnauthorizedCodeCompanyNotFound = "company_not_found"
UnauthorizedCodeFreeePlanLimit = "freee_plan_limit"
UnauthorizedCodeSourceIPAddressLimit = "source_ip_address_limit"
)
type UnauthorizedError struct {
Message string `json:"message"`
Code string `json:"code"`
}
type Error struct {
StatusCode int
RawError string
IsAuthorizationRequired bool
}
func (e *Error) Error() string {
return e.RawError
}
func (e *Error) Messages() []string {
messages, _ := ExtractFreeeErrorMessage(e.RawError)
return messages
}
type FreeErrorMessageDetail struct {
Messages []string `json:"messages"`
}
type FreeeErrorMessage struct {
ErrorDescription string `json:"error_description"`
Message string `json:"message"`
Messages []string `json:"messages"`
ErrorDetails []FreeErrorMessageDetail `json:"errors"`
}
func ExtractFreeeErrorMessage(errorString string) ([]string, error) {
var messages []string
var errorMessage FreeeErrorMessage
if err := json.Unmarshal([]byte(errorString), &errorMessage); err != nil {
return messages, err
}
if errorMessage.ErrorDescription != "" {
messages = append(messages, errorMessage.ErrorDescription)
}
if errorMessage.Message != "" {
messages = append(messages, errorMessage.Message)
}
for _, msg := range errorMessage.Messages {
messages = append(messages, msg)
}
for _, errorDetail := range errorMessage.ErrorDetails {
if len(errorDetail.Messages) > 0 {
messages = append(messages, errorDetail.Messages...)
}
}
return messages, nil
}