-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror.go
58 lines (52 loc) · 1.09 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
package zhipu
type APIError struct {
Code string `json:"code"`
Message string `json:"message"`
}
func (e APIError) Error() string {
return e.Message
}
type APIErrorResponse struct {
APIError `json:"error"`
}
func (e APIErrorResponse) Error() string {
return e.APIError.Error()
}
// GetAPIErrorCode returns the error code of an API error.
func GetAPIErrorCode(err error) string {
if err == nil {
return ""
}
if e, ok := err.(APIError); ok {
return e.Code
}
if e, ok := err.(APIErrorResponse); ok {
return e.Code
}
if e, ok := err.(*APIError); ok && e != nil {
return e.Code
}
if e, ok := err.(*APIErrorResponse); ok && e != nil {
return e.Code
}
return ""
}
// GetAPIErrorMessage returns the error message of an API error.
func GetAPIErrorMessage(err error) string {
if err == nil {
return ""
}
if e, ok := err.(APIError); ok {
return e.Message
}
if e, ok := err.(APIErrorResponse); ok {
return e.Message
}
if e, ok := err.(*APIError); ok && e != nil {
return e.Message
}
if e, ok := err.(*APIErrorResponse); ok && e != nil {
return e.Message
}
return err.Error()
}