Exception: FlexColumns::Errors::IncorrectlyEncodedStringInDatabaseError
- Inherits:
-
InvalidDataInDatabaseError
- Object
- StandardError
- Base
- DataError
- InvalidDataInDatabaseError
- FlexColumns::Errors::IncorrectlyEncodedStringInDatabaseError
- Defined in:
- lib/flex_columns/errors.rb
Overview
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.
Instance Attribute Summary collapse
-
#first_bad_position ⇒ Object
readonly
Returns the value of attribute first_bad_position.
-
#invalid_chars_as_array ⇒ Object
readonly
Returns the value of attribute invalid_chars_as_array.
-
#raw_data_as_array ⇒ Object
readonly
Returns the value of attribute raw_data_as_array.
Attributes inherited from InvalidDataInDatabaseError
#additional_message, #data_source, #raw_string
Instance Method Summary collapse
-
#initialize(data_source, raw_string) ⇒ IncorrectlyEncodedStringInDatabaseError
constructor
A new instance of IncorrectlyEncodedStringInDatabaseError.
Constructor Details
#initialize(data_source, raw_string) ⇒ IncorrectlyEncodedStringInDatabaseError
Returns a new instance of IncorrectlyEncodedStringInDatabaseError.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# 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 |
Instance Attribute Details
#first_bad_position ⇒ Object (readonly)
Returns the value of attribute first_bad_position.
183 184 185 |
# File 'lib/flex_columns/errors.rb', line 183 def first_bad_position @first_bad_position end |
#invalid_chars_as_array ⇒ Object (readonly)
Returns the value of attribute invalid_chars_as_array.
183 184 185 |
# File 'lib/flex_columns/errors.rb', line 183 def invalid_chars_as_array @invalid_chars_as_array end |
#raw_data_as_array ⇒ Object (readonly)
Returns the value of attribute raw_data_as_array.
183 184 185 |
# File 'lib/flex_columns/errors.rb', line 183 def raw_data_as_array @raw_data_as_array end |