Created
October 8, 2020 16:11
-
-
Save tsandall/deeaff535f9eaae5a55559067611473e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/topdown/http.go b/topdown/http.go | |
index 856e790c..ae0c69af 100644 | |
--- a/topdown/http.go | |
+++ b/topdown/http.go | |
@@ -79,70 +79,55 @@ func builtinHTTPSend(bctx BuiltinContext, args []*ast.Term, iter func(*ast.Term) | |
return handleBuiltinErr(ast.HTTPSend.Name, bctx.Location, err) | |
} | |
- return builtinHTTPSendHelper(bctx, req, raiseError, iter) | |
-} | |
+ result, err := getHTTPResponse(bctx, req) | |
+ if err != nil { | |
+ if raiseError { | |
+ return handleHTTPSendErr(bctx, err) | |
+ } | |
-func builtinHTTPSendHelper(bctx BuiltinContext, req ast.Object, raiseError bool, iter func(*ast.Term) error) (err error) { | |
- httpSendErrType := false | |
+ obj := ast.NewObject() | |
+ obj.Insert(ast.StringTerm("error"), ast.StringTerm(err.Error())) | |
+ obj.Insert(ast.StringTerm("status_code"), ast.IntNumberTerm(0)) | |
+ result = ast.NewTerm(obj) | |
+ } | |
- defer func() { | |
- if err != nil { | |
- if raiseError { | |
- if httpSendErrType { | |
- err = handleHTTPSendErr(bctx, err) | |
- } | |
- } else { | |
- result := ast.NewObject() | |
- | |
- if httpSendErrType { | |
- err = handleHTTPSendErr(bctx, err) | |
- } | |
- | |
- result.Insert(ast.StringTerm("error"), ast.StringTerm(err.Error())) | |
- result.Insert(ast.StringTerm("status_code"), ast.NumberTerm(json.Number("0"))) | |
- err = iter(ast.NewTerm(result)) | |
- } | |
- } | |
- }() | |
+ return iter(result) | |
+} | |
+ | |
+func getHTTPResponse(bctx BuiltinContext, req ast.Object) (*ast.Term, error) { | |
bctx.Metrics.Timer(httpSendLatencyMetricKey).Start() | |
var reqExecutor httpRequestExecutor | |
- reqExecutor, err = newHTTPRequestExecutor(bctx, req) | |
+ reqExecutor, err := newHTTPRequestExecutor(bctx, req) | |
if err != nil { | |
- httpSendErrType = true | |
- return err | |
+ return nil, err | |
} | |
// check if cache already has a response for this query | |
var resp ast.Value | |
resp, err = reqExecutor.CheckCache() | |
if err != nil { | |
- httpSendErrType = true | |
- return err | |
+ return nil, err | |
} | |
if resp == nil { | |
var httpResp *http.Response | |
httpResp, err = reqExecutor.ExecuteHTTPRequest() | |
if err != nil { | |
- httpSendErrType = true | |
- return err | |
+ return nil, err | |
} | |
// add result to cache | |
resp, err = reqExecutor.InsertIntoCache(httpResp) | |
if err != nil { | |
- httpSendErrType = true | |
- return err | |
+ return nil, err | |
} | |
} | |
bctx.Metrics.Timer(httpSendLatencyMetricKey).Stop() | |
- err = iter(ast.NewTerm(resp)) | |
- | |
- return err | |
+ return ast.NewTerm(resp), nil | |
} | |
func init() { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment