{"record":{"id":"888159fb11e378cc","repo":"urbanadventurer/WhatWeb","slug":"can-t-convert-to-utf-8-e","errorCode":null,"errorMessage":"Can't convert to UTF-8 #{e}","messagePattern":"Can't convert to UTF-8 #(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"lib/helper.rb","lineNumber":47,"sourceCode":"    elsif obj.class == Array\n      obj.each do |x|\n        utf8_elements!(x)\n      end\n    elsif obj.class == String\n      convert_to_utf8(obj)\n    end\n  end\n\n  # Converts a string to UTF-8\n  def self.convert_to_utf8(str)\n    begin\n      if (str.frozen?)\n        str.dup.force_encoding(\"UTF-8\").scrub\n      else\n        str.force_encoding(\"UTF-8\").scrub\n      end\n    rescue => e\n      raise \"Can't convert to UTF-8 #{e}\"\n    end\n  end\n\n  #\n  # Takes an integer of certainty (between 1 - 100)\n  #\n  # returns String a word representing the certainty\n  #\n  def self.certainty_to_words(certainty)\n    case certainty\n    when 0..49\n      'maybe'\n    when 50..99\n      'probably'\n    when 100\n      'certain'\n    end\n  end","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/urbanadventurer/WhatWeb/blob/d279d93042d034f3fd29d5a893d44ccc0595d3f8/lib/helper.rb#L29-L65","documentation":"A generic RuntimeError raised in lib/helper.rb when the UTF-8 sanitization helper (force_encoding + scrub) fails for any reason; the original exception is re-raised with its message appended. It signals that a string pulled from a target response could not be normalized to valid UTF-8 for further matching/output.","triggerScenarios":"Passing binary or non-UTF-8 encoded data (e.g. responses in another encoding, gzip bodies decoded incorrectly, raw bytes from sockets) into the helper's encode/scrub method where force_encoding or scrub itself raises (e.g. Encoding::UndefinedConversionError or a frozen-string/argument issue not covered by the dup branch).","commonSituations":"Scanning servers returning Latin-1, Shift-JIS, or otherwise mislabeled content; plugin logic feeding raw binary blobs (images, compressed data) into text matching; Ruby frozen string literal magic comments interacting with the frozen? branch.","solutions":["Check the actual encoding of the input string (str.encoding) and convert with encode('UTF-8', invalid: :replace, undef: :replace) instead of force_encoding","Inspect the appended #{e} message in the raised error to identify the underlying failure (frozen string, bad byte sequence, etc.)","Call .b or .dup before force_encoding on strings you do not own","Pre-filter obviously binary payloads before handing them to the helper"],"exampleFix":"# before\nraise \"Can't convert to UTF-8 #{e}\"\n# after: convert safely instead of force_encoding\nstr.dup.force_encoding('ASCII-8BIT').encode('UTF-8', invalid: :replace, undef: :replace)","handlingStrategy":"validation","validationCode":"def safe_utf8?(str)\n  str.is_a?(String) && str.dup.force_encoding('UTF-8').valid_encoding?\nend","typeGuard":"def as_utf8(str)\n  return nil unless str.is_a?(String)\n  str.dup.force_encoding('UTF-8').scrub\nend","tryCatchPattern":"begin\n  text = EncodingHelper.scrub(raw)\nrescue RuntimeError => e\n  logger.warn(\"UTF-8 conversion failed: #{e.message}\")\n  text = raw.b.force_encoding('UTF-8', invalid: :replace, undef: :replace) rescue ''\nend","preventionTips":["Check str.encoding and str.valid_encoding? before processing","Use encode with invalid: :replace instead of force_encoding for foreign encodings","Avoid feeding binary blobs (images, compressed data) into text helpers","Read the underlying message appended after 'Can't convert to UTF-8' to diagnose"],"tags":["encoding","utf-8","string-processing"],"backgroundTag":"invalid-argument-value","analyzedSha":"d279d93042d034f3fd29d5a893d44ccc0595d3f8","analyzedAt":"2026-09-15T13:51:42.453Z","contentChangedAt":"2026-09-15T13:51:42.453Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}