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

Missing Content-Type returns "got non-twirp exception while processing request" #46

Open
czeitler-asp opened this issue Sep 30, 2022 · 0 comments

Comments

@czeitler-asp
Copy link

If Content-Type is missing from the request, Twirpy returns a 500 response code instead of gracefully handling the error.

Make any request without a Content-Type. This error will be raised.
Error: got non-twirp exception while processing request
Trace:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/twirp/asgi.py", line 62, in __call__
    encoder, decoder = self._get_encoder_decoder(endpoint, headers)
  File "/usr/local/lib/python3.10/site-packages/twirp/base.py", line 104, in _get_encoder_decoder
    message="unexpected Content-Type: " + ctype
TypeError: can only concatenate str (not "NoneType") to str

See the code below. ctype = headers.get('content-type', None) sets ctype to None then attempts to concatenate the None type to a string in message="unexpected Content-Type: " + ctype while building the BadRoute error.

twirpy/twirp/base.py

Lines 93 to 106 in 6940030

def _get_encoder_decoder(self, endpoint, headers):
ctype = headers.get('content-type', None)
if "application/json" == ctype:
decoder = functools.partial(self.json_decoder, data_obj=endpoint.input)
encoder = functools.partial(self.json_encoder, data_obj=endpoint.output)
elif "application/protobuf" == ctype:
decoder = functools.partial(self.proto_decoder, data_obj=endpoint.input)
encoder = functools.partial(self.proto_encoder, data_obj=endpoint.output)
else:
raise exceptions.TwirpServerException(
code=errors.Errors.BadRoute,
message="unexpected Content-Type: " + ctype
)
return encoder, decoder

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

No branches or pull requests

1 participant