-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmisc.go
More file actions
141 lines (116 loc) · 2.94 KB
/
misc.go
File metadata and controls
141 lines (116 loc) · 2.94 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package sendgrid
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
)
// ErrorResponse is sendgrid error response
type ErrorResponse struct {
Error string `json:"error"`
}
// Err : error
func (t ErrorResponse) Err() error {
if len(t.Error) == 0 {
return nil
}
return errors.New(t.Error)
}
// ErrorsResponse is sendgrid error response
type ErrorsResponse struct {
Errors []*Error `json:"errors"`
}
type RateLimitedError struct {
RetryAfter time.Duration
}
func (e *RateLimitedError) Error() string {
return fmt.Sprintf("sendgrid rate limit exceeded, retry after %s", e.RetryAfter)
}
// Error is sendgrid error
type Error struct {
Field *string `json:"field,omitempty"`
Message *string `json:"message,omitempty"`
}
// Errs : error list
func (t ErrorsResponse) Errs() error {
s := []string{}
for _, err := range t.Errors {
var msg strings.Builder
if err.Field != nil {
msg.WriteString("field: ")
msg.WriteString(*err.Field)
msg.WriteString(", ")
}
msg.WriteString("message: ")
msg.WriteString(*err.Message)
s = append(s, msg.String())
}
if len(s) == 0 {
return nil
}
return errors.New(strings.Join(s, ", "))
}
// StatusCodeError represents an http response error.
// type httpStatusCode interface { HTTPStatusCode() int } to handle it.
type statusCodeError struct {
Code int
Status string
}
func (t statusCodeError) Error() string {
return fmt.Sprintf("sendgrid server error: %s", t.Status)
}
func (t statusCodeError) HTTPStatusCode() int {
return t.Code
}
func checkStatusCode(resp *http.Response, d debug) error {
if resp.StatusCode == http.StatusTooManyRequests {
xRateLimitReset, err := strconv.ParseInt(resp.Header.Get("X-RateLimit-Reset"), 10, 64)
if err != nil {
return err
}
retryAfter := time.Until(time.Unix(xRateLimitReset, 0))
return &RateLimitedError{retryAfter}
}
// return no error if response returns status code 2xx
if resp.StatusCode/100 == 2 {
return nil
}
if err := logResponse(resp, d); err != nil {
return err
}
// {"errors": [{"field": "field name", "message": "error message"}]}
errorsResponse := new(ErrorsResponse)
if err := newJSONParser(errorsResponse)(resp); err == nil {
if errorsResponse.Errs() != nil {
return errorsResponse.Errs()
}
}
// {"error": "error message"}
errorResponse := new(ErrorResponse)
if err := newJSONParser(errorResponse)(resp); err == nil {
if errorResponse.Err() != nil {
return errorResponse.Err()
}
}
return statusCodeError{Code: resp.StatusCode, Status: resp.Status}
}
type responseParser func(*http.Response) error
func newJSONParser(dst interface{}) responseParser {
return func(resp *http.Response) error {
return json.NewDecoder(resp.Body).Decode(dst)
}
}
func logResponse(resp *http.Response, d debug) error {
if d.Debug() {
text, err := httputil.DumpResponse(resp, true)
if err != nil {
return err
}
d.Debugln(string(text))
}
return nil
}