Skip to content

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

@czeitler-asp

Description

@czeitler-asp

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions