Skip to content

Commit

Permalink
fix: handle non-utf8 strings when printing exceptions
Browse files Browse the repository at this point in the history
Fixes #66
  • Loading branch information
palkan committed Sep 12, 2023
1 parent 3c925c8 commit cb52658
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master

- Fix logging non-UTF8 strings. ([@palkan][])

Fixes [#66](https://github.com/palkan/isolator/issues/66)

## 0.10.0 (2023-08-15)

- Support multiple databases with DatabaseCleaner. ([@palkan][])
Expand Down
20 changes: 14 additions & 6 deletions lib/isolator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,23 @@ def connection_threshold(connection_id)

def debug!(msg)
return unless debug_enabled
msg = "[ISOLATOR DEBUG] #{msg}"

if backtrace_cleaner && backtrace_length.positive?
source = extract_source_location(caller)
separator = " ↳ "

msg = "#{msg}\n#{source.join("\n")}" unless source.empty?
begin
msg = "[ISOLATOR DEBUG] #{msg}"
if backtrace_cleaner && backtrace_length.positive?
source = extract_source_location(caller)

msg = "#{msg}\n #{separator}#{source.join("\n")}" unless source.empty?
end

$stdout.puts(colorize_debug(msg))
rescue Encoding::CompatibilityError
should_retry = separator != " - "
separator = " - "
retry if should_retry
end

$stdout.puts(colorize_debug(msg))
end

def extract_source_location(locations)
Expand Down
20 changes: 14 additions & 6 deletions lib/isolator/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,22 @@ def send_notifications?
def log_exception
return unless Isolator.config.logger

msg = "[ISOLATOR EXCEPTION]\n" \
"#{exception.message}"
separator = " ↳ "

filtered_backtrace.each do |offense_line|
msg += "\n#{offense_line}"
end
begin
msg = "[ISOLATOR EXCEPTION]\n" \
"#{exception.message}"

Isolator.config.logger.warn(msg)
filtered_backtrace.each do |offense_line|
msg += "\n #{separator}#{offense_line}"
end

Isolator.config.logger.warn(msg)
rescue Encoding::CompatibilityError
should_retry = separator != " - "
separator = " - "
retry if should_retry
end
end

def send_notifications
Expand Down
16 changes: 16 additions & 0 deletions spec/isolator/notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@
↳ second/line/of/backtrace
MSG
end

context "when logging non-utf8 details" do
let(:exception) do
job_args = ["abc123", "\xff".dup.force_encoding(Encoding::ASCII_8BIT)] # rubocop:disable Performance/UnfreezeString
details = "MyJob (#{job_args.join(", ")})"

Isolator::HTTPError.new(details)
end

specify do
subject

expect(logger).to have_received(:warn).once
expect(logger).to have_received(:warn).with(%r{- first/line/of/backtrace})
end
end
end
end
end

0 comments on commit cb52658

Please sign in to comment.