class FlexColumns::Errors::IncorrectlyEncodedStringInDatabaseError
Raised when the string stored in the database is not correctly encoded. We check for this situation before we even try to parse the string as JSON, because the kind of errors you get from this problem are otherwise maddeningly difficult to deal with – partly because the exceptions themselves often end up with bad encoding.
This class does a lot of work to filter out invalid characters, show them just as hex, and show you where into the string they occur. This is, again, so we don’t make things worse by raising an exception with invalid characters in its message, and so that you can figure out where the problems are.
Attributes
first_bad_position[R]
invalid_chars_as_array[R]
raw_data_as_array[R]
Public Class Methods
new(data_source, raw_string)
click to toggle source
Calls superclass method
FlexColumns::Errors::InvalidDataInDatabaseError::new
# File lib/flex_columns/errors.rb, line 185 def initialize(data_source, raw_string) @raw_data_as_array = raw_string.chars.to_a @valid_chars_as_array = [ ] @invalid_chars_as_array = [ ] @raw_data_as_array.each_with_index do |c, i| if (! c.valid_encoding?) @invalid_chars_as_array << c @first_bad_position ||= i else @valid_chars_as_array << c end end @first_bad_position ||= :unknown super(data_source, @valid_chars_as_array.join) end
Private Instance Methods
create_message()
click to toggle source
Calls superclass method
FlexColumns::Errors::InvalidDataInDatabaseError#create_message
# File lib/flex_columns/errors.rb, line 203 def create_message extra = %{\n\nThere are #{invalid_chars_as_array.length} invalid characters out of #{raw_data_as_array.length} total characters. (The string above showing the original JSON omits them, so that it's actually a valid String.) The first bad character occurs at position #{first_bad_position}. Some of the invalid chars are (in hex): } extra += invalid_chars_as_array[0..19].map { |c| c.unpack("H*") }.join(" ") super + extra end