|
| 1 | +# typed: true |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "test_helper" |
| 5 | + |
| 6 | +class ErrorHandlingTest < Minitest::Test |
| 7 | + def setup |
| 8 | + @original_config = LogStruct.config |
| 9 | + LogStruct.configuration = LogStruct::Configuration.new |
| 10 | + end |
| 11 | + |
| 12 | + def teardown |
| 13 | + LogStruct.configuration = @original_config |
| 14 | + end |
| 15 | + |
| 16 | + def test_error_handling_mode_for_returns_standard_errors |
| 17 | + LogStruct.config.error_handling_modes.standard_errors = LogStruct::ErrorHandlingMode::Log |
| 18 | + mode = LogStruct.error_handling_mode_for(LogStruct::Source::App) |
| 19 | + |
| 20 | + assert_equal LogStruct::ErrorHandlingMode::Log, mode |
| 21 | + end |
| 22 | + |
| 23 | + def test_handle_exception_ignore |
| 24 | + LogStruct.config.error_handling_modes.standard_errors = LogStruct::ErrorHandlingMode::Ignore |
| 25 | + error = RuntimeError.new("ignore me") |
| 26 | + |
| 27 | + LogStruct.stub(:error, proc { flunk("should not log in ignore mode") }) do |
| 28 | + LogStruct.handle_exception(error, source: LogStruct::Source::App) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + def test_handle_exception_raise |
| 33 | + LogStruct.config.error_handling_modes.standard_errors = LogStruct::ErrorHandlingMode::Raise |
| 34 | + error = RuntimeError.new("boom") |
| 35 | + |
| 36 | + assert_raises(RuntimeError) do |
| 37 | + LogStruct.handle_exception(error, source: LogStruct::Source::App) |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + def test_handle_exception_log |
| 42 | + LogStruct.config.error_handling_modes.standard_errors = LogStruct::ErrorHandlingMode::Log |
| 43 | + error = RuntimeError.new("log me") |
| 44 | + logged = [] |
| 45 | + |
| 46 | + LogStruct.stub(:error, ->(log) { logged << log }) do |
| 47 | + LogStruct.handle_exception(error, source: LogStruct::Source::App) |
| 48 | + end |
| 49 | + |
| 50 | + assert_equal 1, logged.length |
| 51 | + assert_equal "log me", logged.first.message |
| 52 | + end |
| 53 | + |
| 54 | + def test_handle_exception_report |
| 55 | + LogStruct.config.error_handling_modes.standard_errors = LogStruct::ErrorHandlingMode::Report |
| 56 | + error = RuntimeError.new("report me") |
| 57 | + logged = [] |
| 58 | + reported = [] |
| 59 | + |
| 60 | + LogStruct.stub(:error, ->(log) { logged << log }) do |
| 61 | + LogStruct::MultiErrorReporter.stub(:report_error, ->(_err, _ctx) { reported << true }) do |
| 62 | + LogStruct.handle_exception(error, source: LogStruct::Source::App) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + assert_equal 1, logged.length |
| 67 | + assert_equal [true], reported |
| 68 | + end |
| 69 | + |
| 70 | + def test_handle_exception_log_production_raises_when_not_production |
| 71 | + LogStruct.config.error_handling_modes.standard_errors = LogStruct::ErrorHandlingMode::LogProduction |
| 72 | + error = RuntimeError.new("prod log") |
| 73 | + |
| 74 | + LogStruct.stub(:is_production?, false) do |
| 75 | + assert_raises(RuntimeError) do |
| 76 | + LogStruct.handle_exception(error, source: LogStruct::Source::App) |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + def test_handle_exception_log_production_logs_in_production |
| 82 | + LogStruct.config.error_handling_modes.standard_errors = LogStruct::ErrorHandlingMode::LogProduction |
| 83 | + error = RuntimeError.new("prod log") |
| 84 | + logged = [] |
| 85 | + |
| 86 | + LogStruct.stub(:is_production?, true) do |
| 87 | + LogStruct.stub(:error, ->(log) { logged << log }) do |
| 88 | + LogStruct.handle_exception(error, source: LogStruct::Source::App) |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + assert_equal 1, logged.length |
| 93 | + end |
| 94 | +end |
0 commit comments