Ruby 3.2's Exception#detailed_message method returns a string that is encoded as UTF-8 but has a String#encoding set to ASCII_8BIT. This causes issues when we later convert the string to UTF-8 (for sending as JSON) because the conversion is invalid:
New tests with UTF-8, UTF-16 & Shift JIS encoded messages
[^1]: 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
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 exactly[^1]:
The bit in the middle is
(Exception)\n
that's displayed literally in the ASCII-8BIT output:Testing
[^1]: 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