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

Encoding invalid characters #2

Open
adrianschlatter opened this issue Oct 10, 2021 · 0 comments
Open

Encoding invalid characters #2

adrianschlatter opened this issue Oct 10, 2021 · 0 comments

Comments

@adrianschlatter
Copy link
Owner

Trying to encode a character which is not supported by a codec will raise an exception (=> good). However, the error message is a bit misleading in some cases.

Example:

'ä'.encode('datamatrix.EDIFACT')

UnicodeEncodeError: 'ascii' codec can't encode character '\xe4' in position 0: ordinal not in range(128)
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
/InteractiveInput-1 in <module>
----> 1 'ä'.encode('datamatrix.EDIFACT')

~/Projects/ppf.datamatrix/src/ppf/datamatrix/codec_edifact.py in encode_to_edifact(msg)
     46     elif n_rest == 1:
     47         if len(msg) < 2:
---> 48             enc = (b'\xF0' + pack(msg.encode('ascii'), b'\x1F\x00\x00'))
     49         else:
     50             enc = (b'\xF0' +

UnicodeEncodeError: 'ascii' codec can't encode character '\xe4' in position 0: ordinal not in range(128)

Note that we are trying to encode in 'datamatrix.EDIFACT'. But the error message says there is a failure when encoding to 'ascii'. We should catch this exception and re-raise an exception that say 'ä' is an invalid datamatrix.EDIFACT character.

Similar things happen in other codecs.

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