Repository navigation
fix: return an error instead of panicking in GetBatchResponseById - #450
Open
0z5a (0z5a) wants to merge 1 commit into
Open
0z5a (0z5a) wants to merge 1 commit into
0z5a (0z5a) wants to merge 1 commit into
Conversation
GetBatchResponseById panicked when the item had no body (a 204 or 302 item) because of the type assertion on a nil result, and when the id was not in the batch response because of a nil dereference. Return the zero value for items without a body, and an error for unknown ids.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Fixes #449.
GetBatchResponseByIdpanicked in two cases:"body": nullor nobodyfield), as for a204or302item.GetObjectValuereturnsnil, nil, andresult.(T)on a nil interface panics.GetResponseByIdreturnsnil, and*item.GetStatus()dereferences it.With this change, an item without a body returns the zero value of
Tand no error, and an unknown id returns the errorno response found for id: <id>. The parse error fromGetObjectValueis now returned before the type assertion.How I found it: I was reading
batch_requests.goand noticed the unchecked type assertion, then ran the204and302items from the existinggetDummyJSON()fixture through it.Where this can show up in practice: a batch of
DELETErequests gets204items back, and code that callsGetBatchResponseByIdfor each of its request ids panics on the first one. This is my estimate; I have not seen a field report.Notes
204, returningnil, nilmatches what the request adapter'sSendreturns for a204outside of a batch (I checked this with anhttptestserver).204item. The Python core also raises in this case (No response found for id: ...).302item is handled like a204here (zero value, no error), because the function already treats every status below 400 as success.Testing
batch_request_test.go, using the existinggetDummyJSON()fixture:TestGetBatchResponseByIdWithoutBody: items"4"(204) and"1"(302) returnniland no error, without panicking.TestGetBatchResponseByIdUnknownId: id"99"returnsno response found for id: 99, without panicking.go test ./...: without the change inbatch_requests.go, both new tests fail with the panicsinterface conversion: interface is nil, not msgraphgocore.Userableandruntime error: invalid memory address or nil pointer dereference; with it, all packages pass.TestGetResponseByIdForSuccessfulRequestand the twoTestGetResponseByIdFailedRequest*tests still pass.gofmt -l .is empty andgo vet ./...is clean.