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

http: Improve Response html()/json() error message if body is null #2195

Conversation

daniel-shuy
Copy link
Contributor

Closes #2188

When Response.body is nil, instead of displaying the error message invalid type <nil>, expected string, []byte or ArrayBuffer (which is being propagated from util.ToBytes()/util.ToString()), Response.html() displays the body is null so we can't transform it to html - this likely was because of a request error getting the response while Response.json() displays the body is null so we can't transform it to json - this likely was because of a request error getting the response.

@CLAassistant
Copy link

CLAassistant commented Oct 22, 2021

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @daniel-shuy,
thanks for your contribution! 🎉 👏

It would be good if we can add some test coverage for these cases.

@@ -72,14 +72,21 @@ func (h *HTTP) responseFromHttpext(resp *httpext.Response) *Response {

// HTML returns the body as an html.Selection
func (res *Response) HTML(selector ...string) html.Selection {
rt := common.GetRuntime(res.GetCtx())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need it, in this way, we are making this call also in the positive cases that it shouldn't be necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I've reverted it

js/modules/k6/http/response.go Outdated Show resolved Hide resolved
@daniel-shuy
Copy link
Contributor Author

@codebien I've added tests as you suggested, using httpbin's /status/{codes} to simulate an empty response body

@codebien
Copy link
Contributor

codebien commented Oct 22, 2021

@daniel-shuy the tests are failing, response.body is not really null so it panics on err.Error() because err is nil.

You could test using responseType: none

http.get("HTTPBIN_URL/some", {responseType: 'none'}).html();

or re-assigning the body

var res = http.get("HTTPBIN_URL/some"); res.body = null; res.html();

@daniel-shuy
Copy link
Contributor Author

@codebien using responseType: none worked, thanks!

I still can't figure out why golangci-lint is failing with:

paralleltest  Function TestResponse has missing the call to method parallel in the test run

@codecov-commenter
Copy link

codecov-commenter commented Oct 22, 2021

Codecov Report

Merging #2195 (6e59713) into master (42f2e60) will increase coverage by 0.07%.
The diff coverage is 79.14%.

❗ Current head 6e59713 differs from pull request most recent head 8666d19. Consider uploading reports for the commit 8666d19 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2195      +/-   ##
==========================================
+ Coverage   72.71%   72.79%   +0.07%     
==========================================
  Files         184      184              
  Lines       14571    14671     +100     
==========================================
+ Hits        10596    10680      +84     
- Misses       3333     3343      +10     
- Partials      642      648       +6     
Flag Coverage Δ
ubuntu 72.71% <79.14%> (+0.06%) ⬆️
windows 72.45% <79.14%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
cmd/login_cloud.go 0.00% <0.00%> (ø)
cmd/login_influxdb.go 0.00% <0.00%> (ø)
cmd/ui.go 20.61% <0.00%> (ø)
js/compiler/compiler.go 55.00% <ø> (ø)
lib/execution_segment.go 92.73% <ø> (ø)
lib/executor/ramping_vus.go 94.66% <ø> (ø)
lib/metrics/metrics.go 100.00% <ø> (ø)
ui/form_fields.go 45.71% <0.00%> (ø)
output/influxdb/output.go 78.57% <58.62%> (-3.61%) ⬇️
js/modules/k6/grpc/client.go 77.56% <78.94%> (-0.09%) ⬇️
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 66ed1ae...8666d19. Read the comment docs.

@daniel-shuy
Copy link
Contributor Author

Adding t.Parallel() causes the tests to fail with race detected during execution of test. I've ignored it using //nolint:paralleltest

Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also rebase the history squashing the fixups commits on the first commit, please? In this way, it will be easier for the next reviewer to check it. Thanks

js/modules/k6/http/response_test.go Outdated Show resolved Hide resolved
@daniel-shuy daniel-shuy force-pushed the feature/http-response-improve-error-message-if-body-is-null branch from 6e59713 to 8666d19 Compare October 22, 2021 15:52
@daniel-shuy
Copy link
Contributor Author

Can you also rebase the history squashing the fixups commits on the first commit, please? In this way, it will be easier for the next reviewer to check it. Thanks

Sure, I usually prefer to squash after its approved so that only the changes need to be re-reviewed.

codebien
codebien previously approved these changes Oct 22, 2021
Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉 let's wait @imiric's review

Copy link
Contributor

@imiric imiric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @daniel-shuy!

LGTM, just a suggestion to fix the casing on HTML/JSON and to concatenate the error message to avoid silencing the linter. The concatenation should be negligible and it's also more readable in the code IMO.

js/modules/k6/http/response.go Outdated Show resolved Hide resolved
js/modules/k6/http/response.go Outdated Show resolved Hide resolved
js/modules/k6/http/response_test.go Outdated Show resolved Hide resolved
js/modules/k6/http/response_test.go Outdated Show resolved Hide resolved
@daniel-shuy daniel-shuy force-pushed the feature/http-response-improve-error-message-if-body-is-null branch from 8666d19 to 7d00c3e Compare October 25, 2021 10:20
@daniel-shuy daniel-shuy force-pushed the feature/http-response-improve-error-message-if-body-is-null branch from 7d00c3e to 8967a65 Compare October 25, 2021 10:22
@daniel-shuy
Copy link
Contributor Author

@imiric done, thanks!

@codebien codebien merged commit 431d6e8 into grafana:master Oct 25, 2021
@daniel-shuy daniel-shuy deleted the feature/http-response-improve-error-message-if-body-is-null branch October 25, 2021 11:14
@na-- na-- added this to the v0.35.0 milestone Oct 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve exception message on response body being null
6 participants