-
Notifications
You must be signed in to change notification settings - Fork 297
/
common.go
48 lines (40 loc) · 1.27 KB
/
common.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
//go:generate mockgen --build_flags=--mod=mod -destination=../../../mocks/services/streammanager/common/mock_streammanager.go -package mock_streammanager github.com/rudderlabs/rudder-server/services/streammanager/common StreamProducer
package common
import (
"encoding/json"
"io"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/awserr"
)
type StreamProducer interface {
io.Closer
Produce(jsonData json.RawMessage, destConfig interface{}) (int, string, string)
}
type Opts struct {
Timeout time.Duration
}
func mapErrorMessageToStatusCode(errorMessage string, defaultStatusCode int) int {
if strings.Contains(errorMessage, "Throttling") {
// aws returns "ThrottlingException"
// for throttling requests server will retry
return 429
}
if strings.Contains(errorMessage, "RequestExpired") {
// Retryable
return 500
}
return defaultStatusCode
}
func ParseAWSError(err error) (statusCode int, respStatus, responseMessage string) {
statusCode = 500
respStatus = "Failure"
responseMessage = err.Error()
if reqErr, ok := err.(awserr.RequestFailure); ok {
responseMessage = reqErr.Error()
respStatus = reqErr.Code()
statusCode = reqErr.StatusCode()
}
statusCode = mapErrorMessageToStatusCode(responseMessage, statusCode)
return statusCode, respStatus, responseMessage
}