Skip to content

Commit 4611f2b

Browse files
committed
Resolve review comments
Signed-off-by: Md Soharab Ansari <[email protected]>
1 parent 32530f8 commit 4611f2b

File tree

1 file changed

+52
-37
lines changed

1 file changed

+52
-37
lines changed

Diff for: common/util.go

+52-37
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ type (
2828
SourceName string
2929
}
3030

31-
Request struct {
31+
FunctionHTTPRequest struct {
3232
Message string
3333
HTTPEndpoint string
3434
Headers http.Header
3535
}
3636

37-
Response struct {
37+
FunctionHTTPResponse struct {
3838
ResponseBody string
3939
StatusCode int
4040
ErrorString string
4141
}
4242

43-
ErrorResponse struct {
44-
Request Request
45-
Response Response
43+
FunctionErrorDetails struct {
44+
FunctionHTTPRequest FunctionHTTPRequest
45+
FunctionHTTPResponse FunctionHTTPResponse
4646
}
4747
)
4848

@@ -108,42 +108,14 @@ func HandleHTTPRequest(message string, headers http.Header, data ConnectorMetada
108108
}
109109
}
110110

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)
127114
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
129116
}
130-
return nil, errors.New(string(errorBytes))
131117
}
132118

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-
}
147119
return resp, nil
148120
}
149121

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

Comments
 (0)