Skip to content

Commit 14e5b81

Browse files
authored
Add usable fields to error queue (#167)
* Add more context to error response * Some cosmetic changes * Bump minor version * Use correct formatter based on data type * Resolve review comments --------- Signed-off-by: Md Soharab Ansari <[email protected]>
1 parent 373463e commit 14e5b81

File tree

9 files changed

+88
-23
lines changed

9 files changed

+88
-23
lines changed

Diff for: aws-kinesis-http-connector/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.13
1+
v0.14

Diff for: aws-sqs-http-connector/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.14
1+
v0.15

Diff for: common/util.go

+80-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package common
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"io"
57
"net/http"
68
"os"
79
"strconv"
@@ -14,16 +16,35 @@ import (
1416
"go.uber.org/zap"
1517
)
1618

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+
)
2748

2849
// ParseConnectorMetadata parses connector side common fields and returns as ConnectorMetadata or returns error
2950
func ParseConnectorMetadata() (ConnectorMetadata, error) {
@@ -87,13 +108,14 @@ func HandleHTTPRequest(message string, headers http.Header, data ConnectorMetada
87108
}
88109
}
89110

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+
}
92117
}
93118

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-
}
97119
return resp, nil
98120
}
99121

@@ -122,3 +144,46 @@ func GetAwsConfig() (*aws.Config, error) {
122144
}
123145
return nil, errors.New("no aws configuration specified")
124146
}
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+
}

Diff for: gcp-pubsub-http-connector/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.9
1+
v0.10

Diff for: kafka-http-connector/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.15
1+
v0.16

Diff for: nats-jetstream-http-connector/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.7
1+
v0.8

Diff for: nats-streaming-http-connector/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.16
1+
v0.17

Diff for: rabbitmq-http-connector/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.13
1+
v0.14

Diff for: redis-http-connector/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.6
1+
v0.7

0 commit comments

Comments
 (0)