Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report errors in output instead of panicking #3

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions pkg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type result struct {
StatusCode int `json:"statusCode"`
Latency time.Duration `json:"latency"`
Request *request `json:"request"`
err error
ErrorMsg string `json:"error"`
}

func startClientWorkers(numWorkers int, requests <-chan *request, results chan<- *result, dryRun bool, timeout int) {
Expand All @@ -49,38 +49,38 @@ func doHttpRequest(client *http.Client, requests <-chan *request, results chan<-
latencyStart := time.Now()

if dryRun {
sendResult(req, &http.Response{}, latencyStart, results)
sendResult(req, &http.Response{}, latencyStart, "", results)
} else {
go func() {
httpReq, err := req.httpRequest()

if err != nil {
results <- &result{err: err}
sendResult(req, &http.Response{}, latencyStart, err.Error(), results)
return
}

resp, err := client.Do(httpReq)

if err != nil {
results <- &result{err: err}
sendResult(req, &http.Response{}, latencyStart, err.Error(), results)
return
}

_, err = io.ReadAll(resp.Body)
defer resp.Body.Close()

if err != nil {
results <- &result{err: err}
sendResult(req, &http.Response{}, latencyStart, err.Error(), results)
return
}

sendResult(req, resp, latencyStart, results)
sendResult(req, resp, latencyStart, "", results)
}()
}
}
}

func sendResult(req *request, resp *http.Response, latencyStart time.Time, results chan<- *result) {
func sendResult(req *request, resp *http.Response, latencyStart time.Time, err string, results chan<- *result) {
latency := time.Now().Sub(latencyStart)
results <- &result{StatusCode: resp.StatusCode, Latency: latency, Request: req}
results <- &result{StatusCode: resp.StatusCode, Latency: latency, Request: req, ErrorMsg: err}
}
4 changes: 0 additions & 4 deletions pkg/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ func Replay(phasesStr string, silent, printStats, dryRun bool, timeout int) {
return
}

if result.err != nil {
panic(result.err)
}

stats.onResult(result)

jsonResult, err := json.Marshal(result)
Expand Down