Fix Unicode encoding issues with detailed_message #817
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.
Goal
Ruby 3.2's
Exception#detailed_message
method returns a string that is encoded as UTF-8 but has aString#encoding
set toASCII_8BIT
. This causes issues when we later convert the string to UTF-8 (for sending as JSON) because the conversion is invalid:If the detailed message is forced to UTF-8 then it works as expected:
This can then be sent as JSON correctly
You can compare the bytes in this string with the "ASCII-8BIT" encoded string above and they match exactly1:
The bit in the middle is
(Exception)\n
that's displayed literally in the ASCII-8BIT output:Testing
Footnotes
You would expect this as
force_encoding
doesn't changing the underlying bytes, but this decoding back into the original input proves that it's really a UTF-8 string ↩