@@ -28,21 +28,21 @@ type (
28
28
SourceName string
29
29
}
30
30
31
- Request struct {
31
+ FunctionHTTPRequest struct {
32
32
Message string
33
33
HTTPEndpoint string
34
34
Headers http.Header
35
35
}
36
36
37
- Response struct {
37
+ FunctionHTTPResponse struct {
38
38
ResponseBody string
39
39
StatusCode int
40
40
ErrorString string
41
41
}
42
42
43
- ErrorResponse struct {
44
- Request Request
45
- Response Response
43
+ FunctionErrorDetails struct {
44
+ FunctionHTTPRequest FunctionHTTPRequest
45
+ FunctionHTTPResponse FunctionHTTPResponse
46
46
}
47
47
)
48
48
@@ -108,42 +108,14 @@ func HandleHTTPRequest(message string, headers http.Header, data ConnectorMetada
108
108
}
109
109
}
110
110
111
- errResp := ErrorResponse {
112
- Request : Request {
113
- Message : message ,
114
- HTTPEndpoint : data .HTTPEndpoint ,
115
- Headers : headers ,
116
- },
117
- Response : Response {
118
- ResponseBody : "" ,
119
- StatusCode : http .StatusInternalServerError ,
120
- ErrorString : "" ,
121
- },
122
- }
123
-
124
- if resp == nil {
125
- errResp .Response .ErrorString = fmt .Sprintf ("every function invocation retry failed; final retry gave empty response. http_endpoint: %s, source: %s" , data .HTTPEndpoint , data .SourceName )
126
- errorBytes , err := json .Marshal (errResp )
111
+ if resp == nil || resp .StatusCode < 200 || resp .StatusCode > 300 {
112
+ errResp := NewFunctionErrorDetails (message , data .HTTPEndpoint , headers )
113
+ err := errResp .UpdateResponseDetails (resp , data )
127
114
if err != nil {
128
- return nil , fmt . Errorf ( "failed marshalling error response. http_endpoint: %s, source: %s" , data . HTTPEndpoint , data . SourceName )
115
+ return nil , err
129
116
}
130
- return nil , errors .New (string (errorBytes ))
131
117
}
132
118
133
- if resp .StatusCode < 200 || resp .StatusCode > 300 {
134
- body , err := io .ReadAll (resp .Body )
135
- if err != nil {
136
- return nil , fmt .Errorf ("failed reading response body. http_endpoint: %s, source: %s" , data .HTTPEndpoint , data .SourceName )
137
- }
138
- errResp .Response .ResponseBody = string (body )
139
- errResp .Response .StatusCode = resp .StatusCode
140
- errResp .Response .ErrorString = fmt .Sprintf ("request returned failure: %d. http_endpoint: %s, source: %s" , resp .StatusCode , data .HTTPEndpoint , data .SourceName )
141
- errorBytes , err := json .Marshal (errResp )
142
- if err != nil {
143
- return nil , fmt .Errorf ("failed marshalling error response. http_endpoint: %s, source: %s" , data .HTTPEndpoint , data .SourceName )
144
- }
145
- return nil , errors .New (string (errorBytes ))
146
- }
147
119
return resp , nil
148
120
}
149
121
@@ -172,3 +144,46 @@ func GetAwsConfig() (*aws.Config, error) {
172
144
}
173
145
return nil , errors .New ("no aws configuration specified" )
174
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