Improve error message when unexpected request.data encountered during parsing #111
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.
Ran into a scenario when sending a
POST
request to the gateway from a client-side javascript application, whererequest.body
was of typestring
instead of typeobject
. This unexpected data type is accepted initially, but is not handled or caught at any point in the request cycle, the result being that the finalrequest
that is passed to the backend Django view has an emptyrequest.POST
body.Walkthrough of request cycle:
gateway
using therequests
library, and passes anoptions
object with{body: <JSON API Resource Identifier Object>}
gateway
here: https://github.com/ZeroCater/gateway/blob/master/src/handleRoute.js#L30,this.request.body
is of typestring
(but the code "expects" anobject
)gateway
creates an event to be passed into the message bus here: https://github.com/ZeroCater/gateway/blob/master/src/handleRoute.js#L52 with kwargs wherebody
is of type `stringmicroservice_events_listener.py
microservice_events_listener.py
routes the request to the appropriate job task inmicroservice_events.py
microservice_events.py
callsevent_client.handle_request_event()
create_django_request()
here: https://github.com/ZeroCater/zc_events/blob/master/zc_events/client.py#L352string
body torequest.raw_body
here: https://github.com/ZeroCater/zc_events/blob/master/zc_events/django_request.py#L45request
object and parses the information in it viazc_common.remote_resource.parsers
here: https://github.com/ZeroCater/zc_common/blob/master/zc_common/remote_resource/parsers.py#L62raw_body
here https://github.com/ZeroCater/zc_common/blob/master/zc_common/remote_resource/parsers.py#L63 isTrue
, soresult
is set to thestring
bodyObject
we doresult.get('data')
here: https://github.com/ZeroCater/zc_common/blob/master/zc_common/remote_resource/parsers.py#L72, but it raises anAttributeError
sinceresult
is actually a stringrequest.data
is set to an empty dictionary.Essentially, data goes in on one end, then gets silently swallowed and discarded somewhere deep down along a long request cycle chain.
This PR detects the discrepancy in data type, and raises and appropriate error with explanatory message.