1
1
package common
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
6
+ "io"
5
7
"net/http"
6
8
"os"
7
9
"strconv"
@@ -14,16 +16,35 @@ import (
14
16
"go.uber.org/zap"
15
17
)
16
18
17
- // ConnectorMetadata contains common fields used by connectors
18
- type ConnectorMetadata struct {
19
- Topic string
20
- ResponseTopic string
21
- ErrorTopic string
22
- HTTPEndpoint string
23
- MaxRetries int
24
- ContentType string
25
- SourceName string
26
- }
19
+ type (
20
+ // ConnectorMetadata contains common fields used by connectors
21
+ ConnectorMetadata struct {
22
+ Topic string
23
+ ResponseTopic string
24
+ ErrorTopic string
25
+ HTTPEndpoint string
26
+ MaxRetries int
27
+ ContentType string
28
+ SourceName string
29
+ }
30
+
31
+ FunctionHTTPRequest struct {
32
+ Message string
33
+ HTTPEndpoint string
34
+ Headers http.Header
35
+ }
36
+
37
+ FunctionHTTPResponse struct {
38
+ ResponseBody string
39
+ StatusCode int
40
+ ErrorString string
41
+ }
42
+
43
+ FunctionErrorDetails struct {
44
+ FunctionHTTPRequest FunctionHTTPRequest
45
+ FunctionHTTPResponse FunctionHTTPResponse
46
+ }
47
+ )
27
48
28
49
// ParseConnectorMetadata parses connector side common fields and returns as ConnectorMetadata or returns error
29
50
func ParseConnectorMetadata () (ConnectorMetadata , error ) {
@@ -87,13 +108,14 @@ func HandleHTTPRequest(message string, headers http.Header, data ConnectorMetada
87
108
}
88
109
}
89
110
90
- if resp == nil {
91
- return nil , fmt .Errorf ("every function invocation retry failed; final retry gave empty response. http_endpoint: %v, source: %v" , data .HTTPEndpoint , data .SourceName )
111
+ if resp == nil || resp .StatusCode < 200 || resp .StatusCode > 300 {
112
+ errResp := NewFunctionErrorDetails (message , data .HTTPEndpoint , headers )
113
+ err := errResp .UpdateResponseDetails (resp , data )
114
+ if err != nil {
115
+ return nil , err
116
+ }
92
117
}
93
118
94
- if resp .StatusCode < 200 || resp .StatusCode > 300 {
95
- return nil , fmt .Errorf ("request returned failure: %v. http_endpoint: %v, source: %v" , resp .StatusCode , data .HTTPEndpoint , data .SourceName )
96
- }
97
119
return resp , nil
98
120
}
99
121
@@ -122,3 +144,46 @@ func GetAwsConfig() (*aws.Config, error) {
122
144
}
123
145
return nil , errors .New ("no aws configuration specified" )
124
146
}
147
+
148
+ func NewFunctionErrorDetails (message , httpEndpoint string , headers http.Header ) FunctionErrorDetails {
149
+ return FunctionErrorDetails {
150
+ FunctionHTTPRequest : FunctionHTTPRequest {
151
+ Message : message ,
152
+ HTTPEndpoint : httpEndpoint ,
153
+ Headers : headers ,
154
+ },
155
+ FunctionHTTPResponse : FunctionHTTPResponse {
156
+ ResponseBody : "" ,
157
+ StatusCode : http .StatusInternalServerError ,
158
+ ErrorString : "" ,
159
+ },
160
+ }
161
+ }
162
+
163
+ func (errResp * FunctionErrorDetails ) UpdateResponseDetails (resp * http.Response , data ConnectorMetadata ) error {
164
+ if resp == nil {
165
+ errResp .FunctionHTTPResponse .ErrorString = fmt .Sprintf ("every function invocation retry failed; final retry gave empty response. http_endpoint: %s, source: %s" , data .HTTPEndpoint , data .SourceName )
166
+ errorBytes , err := json .Marshal (errResp )
167
+ if err != nil {
168
+ return fmt .Errorf ("failed marshalling error response. http_endpoint: %s, source: %s" , data .HTTPEndpoint , data .SourceName )
169
+ }
170
+ return errors .New (string (errorBytes ))
171
+ }
172
+
173
+ if resp .StatusCode < 200 || resp .StatusCode > 300 {
174
+ body , err := io .ReadAll (resp .Body )
175
+ if err != nil {
176
+ return fmt .Errorf ("failed reading response body. http_endpoint: %s, source: %s" , data .HTTPEndpoint , data .SourceName )
177
+ }
178
+ errResp .FunctionHTTPResponse .ResponseBody = string (body )
179
+ errResp .FunctionHTTPResponse .StatusCode = resp .StatusCode
180
+ errResp .FunctionHTTPResponse .ErrorString = fmt .Sprintf ("request returned failure: %d. http_endpoint: %s, source: %s" , resp .StatusCode , data .HTTPEndpoint , data .SourceName )
181
+ errorBytes , err := json .Marshal (errResp )
182
+ if err != nil {
183
+ return fmt .Errorf ("failed marshalling error response. http_endpoint: %s, source: %s" , data .HTTPEndpoint , data .SourceName )
184
+ }
185
+ return errors .New (string (errorBytes ))
186
+ }
187
+
188
+ return nil
189
+ }
0 commit comments