-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
errors.go
92 lines (77 loc) · 1.91 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package gotwi
import (
"errors"
"fmt"
"strings"
"github.com/michimani/gotwi/internal/gotwierrors"
"github.com/michimani/gotwi/resources"
)
type GotwiError struct {
err error
OnAPI bool
resources.Non2XXError
}
func wrapErr(e error) *GotwiError {
if e == nil {
return nil
}
if w, ok := e.(*GotwiError); ok {
return w
}
return &GotwiError{err: e}
}
func wrapWithAPIErr(n2xxerr *resources.Non2XXError) *GotwiError {
if n2xxerr == nil {
return nil
}
return &GotwiError{
err: errors.New(non2XXErrorSummary(n2xxerr)),
OnAPI: true,
Non2XXError: *n2xxerr,
}
}
func (e *GotwiError) Error() string {
if e == nil {
return ""
}
if e.err != nil {
return e.err.Error()
}
return gotwierrors.ErrorUndefined
}
func (e *GotwiError) Unwrap() error {
if e == nil {
return nil
}
return e.err
}
func non2XXErrorSummary(e *resources.Non2XXError) string {
if e == nil {
return ""
}
summary := []string{"The Twitter API returned a Response with a status other than 2XX series."}
if e.Status != "" {
summary = append(summary, fmt.Sprintf("httpStatus=\"%s\"", e.Status))
}
if e.StatusCode > 0 {
summary = append(summary, fmt.Sprintf("httpStatusCode=%d", e.StatusCode))
}
if e.Title != "" {
summary = append(summary, fmt.Sprintf("title=\"%s\"", e.Title))
}
if e.Detail != "" {
summary = append(summary, fmt.Sprintf("detail=\"%s\"", e.Detail))
}
ercnt := 1
for _, er := range e.APIErrors {
if er.Message != "" && er.Code > 0 {
detail := er.Code.Detail()
summary = append(summary, fmt.Sprintf("errorCode%d=%d errorText%d=\"%s\" errorDescription%d=\"%s\"", ercnt, er.Code, ercnt, detail.Text, ercnt, detail.Description))
ercnt++
}
}
if e.RateLimitInfo != nil {
summary = append(summary, fmt.Sprintf("rateLimit=%d rateLimitRemaining=%d rateLimitReset=\"%s\"", e.RateLimitInfo.Limit, e.RateLimitInfo.Remaining, e.RateLimitInfo.ResetAt))
}
return strings.Join(summary, " ")
}