Skip to content

blank encoding in properties generates LookupError #125

@gabysbrain

Description

@gabysbrain

Setting the content_encoding property to an empty string will generate an exception when a message is encoded.

When sending a message with the following properties (for example)

{content_encoding: '', content_type: '', correlation_id: '', headers: {compression: False, x-delay: 60000}, message_id: '4d042d03fff943cda0a60e9b10175b7d', timestamp: datetime.datetime(2023, 6, 5, 9, 36, 19, 158600)}

Will generate an exception:

LookupError: unknown encoding: 
  File "amqpstorm/message.py", line 181, in publish
    return self._channel.basic.publish(body=self._body,
  File "amqpstorm/basic.py", line 193, in publish
    body = self._handle_utf8_payload(body, properties)
  File "amqpstorm/basic.py", line 354, in _handle_utf8_payload
    body = bytes(body, encoding=encoding)

I think the problem is that an empty string is not a valid encoding and the check for a valid content_encoding is only if the key is missing from properties.

if 'content_encoding' not in properties:
properties['content_encoding'] = 'utf-8'
encoding = properties['content_encoding']
if compatibility.is_unicode(body):
body = body.encode(encoding)
elif compatibility.PYTHON3 and isinstance(body, str):
body = bytes(body, encoding=encoding)
return body

Activity

jhogg

jhogg commented on Jun 5, 2023

@jhogg
Collaborator
gabysbrain

gabysbrain commented on Sep 1, 2023

@gabysbrain
Author

Ok, Yeah, I understand it would be complicated to check all encodings before. I just wasn't sure if a blank encoding was meant to be the same as None or actually identify something.

Anyway, I think it would help to have the extension wrapped a few levels up the stack with a more clear message. The reason is that right now you get an exception from the bytes method so you need to go into the amqpstorm source to understand what's happening. It would be easier to have an exception that just says that you passed an invalid content_encoding.

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jhogg@gabysbrain

        Issue actions

          blank encoding in properties generates LookupError · Issue #125 · eandersson/amqpstorm