Class: Raven::Processor::UTF8Conversion

Inherits:
Raven::Processor show all
Defined in:
lib/raven/processor/utf8conversion.rb

Constant Summary collapse

REPLACE =

Slightly misnamed - actually just removes any bytes with invalid encoding Previously, our JSON backend required UTF-8. Since we now use the built-in JSON, we can use any encoding, but it must be valid anyway so we can do things like call #match and #slice on strings

"".freeze

Constants inherited from Raven::Processor

INT_MASK, REGEX_SPECIAL_CHARACTERS, STRING_MASK

Instance Method Summary collapse

Methods inherited from Raven::Processor

#initialize

Constructor Details

This class inherits a constructor from Raven::Processor

Instance Method Details

#process(value) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/raven/processor/utf8conversion.rb', line 9

def process(value)
  case value
  when Hash
    !value.frozen? ? value.merge!(value) { |_, v| process v } : value.merge(value) { |_, v| process v }
  when Array
    !value.frozen? ? value.map! { |v| process v } : value.map { |v| process v }
  when Exception
    return value if value.message.valid_encoding?
    clean_exc = value.class.new(remove_invalid_bytes(value.message))
    clean_exc.set_backtrace(value.backtrace)
    clean_exc
  when String
    # Encoding::BINARY / Encoding::ASCII_8BIT is a special binary encoding.
    # valid_encoding? will always return true because it contains all codepoints,
    # so instead we check if it only contains actual ASCII codepoints, and if
    # not we assume it's actually just UTF8 and scrub accordingly.
    if value.encoding == Encoding::BINARY && !value.ascii_only?
      value = value.dup
      value.force_encoding(Encoding::UTF_8)
    end
    return value if value.valid_encoding?
    remove_invalid_bytes(value)
  else
    value
  end
end