|
| 1 | +# typed: false # rubocop:disable Sorbet/TrueSigil |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require_relative "../test_helper" |
| 5 | +require "action_mailer" |
| 6 | +require "action_view" |
| 7 | + |
| 8 | +class ActionMailerIntegrationTest < ActiveSupport::TestCase |
| 9 | + class CustomError < StandardError; end |
| 10 | + |
| 11 | + class AnotherError < StandardError; end |
| 12 | + |
| 13 | + # Test mailer with custom rescue_from handlers |
| 14 | + class TestMailer < ActionMailer::Base |
| 15 | + default from: "test@example.com" |
| 16 | + |
| 17 | + # User-defined custom error handler (should take precedence) |
| 18 | + rescue_from CustomError do |exception| |
| 19 | + Rails.logger.info "Custom handler caught: #{exception.message}" |
| 20 | + # Don't reraise - handle the error |
| 21 | + end |
| 22 | + |
| 23 | + rescue_from AnotherError do |exception| |
| 24 | + Rails.logger.info "AnotherError handler caught: #{exception.message}" |
| 25 | + # Reraise for demonstration |
| 26 | + raise exception |
| 27 | + end |
| 28 | + |
| 29 | + def test_email |
| 30 | + mail(to: "recipient@example.com", subject: "Test") do |format| |
| 31 | + format.text { render plain: "Test email" } |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + # These methods should trigger rescue_from when called during deliver process |
| 36 | + def custom_error_during_delivery |
| 37 | + raise CustomError, "This is a custom error" |
| 38 | + end |
| 39 | + |
| 40 | + def another_error_during_delivery |
| 41 | + raise AnotherError, "This is another error" |
| 42 | + end |
| 43 | + |
| 44 | + def standard_error_during_delivery |
| 45 | + raise StandardError, "This is a standard error" |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + # Mailer that inherits from a base class with handlers |
| 50 | + class ApplicationMailer < ActionMailer::Base |
| 51 | + default from: "app@example.com" |
| 52 | + |
| 53 | + # Application-level handler |
| 54 | + rescue_from StandardError do |exception| |
| 55 | + Rails.logger.error "App handler caught: #{exception.message}" |
| 56 | + raise exception # Reraise for retry |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + class InheritedMailer < ApplicationMailer |
| 61 | + # Include callbacks module to enable before_deliver |
| 62 | + include LogStruct::Integrations::ActionMailer::Callbacks |
| 63 | + |
| 64 | + def test_email |
| 65 | + mail(to: "recipient@example.com", subject: "Test") do |format| |
| 66 | + format.text { render plain: "Test email" } |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + def error_during_delivery |
| 71 | + raise StandardError, "Error in inherited mailer" |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + setup do |
| 76 | + @original_logger = Rails.logger |
| 77 | + @log_output = StringIO.new |
| 78 | + Rails.logger = Logger.new(@log_output) |
| 79 | + |
| 80 | + # Store original configuration |
| 81 | + @original_config = LogStruct.configuration.dup |
| 82 | + |
| 83 | + # Enable ActionMailer integration |
| 84 | + LogStruct.configure do |config| |
| 85 | + config.enabled = true |
| 86 | + config.integrations.enable_actionmailer = true |
| 87 | + end |
| 88 | + |
| 89 | + # Set up ActionMailer for testing |
| 90 | + ActionMailer::Base.delivery_method = :test |
| 91 | + ActionMailer::Base.perform_deliveries = true |
| 92 | + end |
| 93 | + |
| 94 | + teardown do |
| 95 | + Rails.logger = @original_logger |
| 96 | + LogStruct.configuration = @original_config |
| 97 | + ActionMailer::Base.deliveries.clear |
| 98 | + end |
| 99 | + |
| 100 | + test "user defined rescue_from handlers work without LogStruct interference" do |
| 101 | + # Setup LogStruct ActionMailer integration |
| 102 | + LogStruct::Integrations::ActionMailer.setup(LogStruct.configuration) |
| 103 | + |
| 104 | + # Patch MessageDelivery to include handle_errors |
| 105 | + LogStruct::Integrations::ActionMailer::Callbacks.patch_message_delivery |
| 106 | + |
| 107 | + # Include all our modules (simulates what happens on_load) |
| 108 | + TestMailer.include LogStruct::Integrations::ActionMailer::ErrorHandling |
| 109 | + |
| 110 | + # The custom error should be handled by the user's handler |
| 111 | + assert_nothing_raised do |
| 112 | + TestMailer.custom_error_during_delivery.deliver_now |
| 113 | + end |
| 114 | + |
| 115 | + # Check that the custom handler ran (it logged a message) |
| 116 | + log_content = @log_output.string |
| 117 | + |
| 118 | + assert_match(/Custom handler caught: This is a custom error/, log_content) |
| 119 | + |
| 120 | + # No structured error should have been logged since user handled it |
| 121 | + refute_match(/mailer_class/, log_content) |
| 122 | + end |
| 123 | + |
| 124 | + test "user handlers that reraise still trigger LogStruct logging" do |
| 125 | + # Setup LogStruct ActionMailer integration |
| 126 | + LogStruct::Integrations::ActionMailer.setup(LogStruct.configuration) |
| 127 | + LogStruct::Integrations::ActionMailer::Callbacks.patch_message_delivery |
| 128 | + TestMailer.include LogStruct::Integrations::ActionMailer::ErrorHandling |
| 129 | + |
| 130 | + # AnotherError is caught by user handler but reraises |
| 131 | + assert_raises(AnotherError) do |
| 132 | + TestMailer.another_error_during_delivery.deliver_now |
| 133 | + end |
| 134 | + |
| 135 | + log_content = @log_output.string |
| 136 | + |
| 137 | + # User handler should have logged |
| 138 | + assert_match(/AnotherError handler caught: This is another error/, log_content) |
| 139 | + |
| 140 | + # LogStruct should have also logged the error (since it was re-raised and not handled) |
| 141 | + assert_match(/This is another error/, log_content) |
| 142 | + end |
| 143 | + |
| 144 | + test "LogStruct handler works with inherited mailers" do |
| 145 | + # Setup LogStruct ActionMailer integration |
| 146 | + LogStruct::Integrations::ActionMailer.setup(LogStruct.configuration) |
| 147 | + LogStruct::Integrations::ActionMailer::Callbacks.patch_message_delivery |
| 148 | + InheritedMailer.include LogStruct::Integrations::ActionMailer::ErrorHandling |
| 149 | + |
| 150 | + # Error should be caught by ApplicationMailer's handler first |
| 151 | + assert_raises(StandardError) do |
| 152 | + InheritedMailer.error_during_delivery.deliver_now |
| 153 | + end |
| 154 | + |
| 155 | + log_content = @log_output.string |
| 156 | + # Both user handler and LogStruct should have logged |
| 157 | + assert_match(/App handler caught: Error in inherited mailer/, log_content) |
| 158 | + assert_match(/Error in inherited mailer/, log_content) |
| 159 | + end |
| 160 | + |
| 161 | + test "unhandled errors are caught by LogStruct default handler" do |
| 162 | + # Create a mailer without custom handlers |
| 163 | + simple_mailer = Class.new(ActionMailer::Base) do |
| 164 | + default from: "test@example.com" |
| 165 | + |
| 166 | + def error_during_delivery |
| 167 | + raise StandardError, "Unhandled error" |
| 168 | + end |
| 169 | + end |
| 170 | + |
| 171 | + # Setup LogStruct |
| 172 | + LogStruct::Integrations::ActionMailer.setup(LogStruct.configuration) |
| 173 | + LogStruct::Integrations::ActionMailer::Callbacks.patch_message_delivery |
| 174 | + simple_mailer.include LogStruct::Integrations::ActionMailer::ErrorHandling |
| 175 | + |
| 176 | + # Should be caught by our default handler |
| 177 | + assert_raises(StandardError) do |
| 178 | + simple_mailer.error_during_delivery.deliver_now |
| 179 | + end |
| 180 | + |
| 181 | + log_content = @log_output.string |
| 182 | + # Should have our error logging |
| 183 | + assert_match(/Unhandled error/, log_content) |
| 184 | + # Should have structured logging from LogStruct |
| 185 | + assert_match(/will retry/, log_content) |
| 186 | + end |
| 187 | + |
| 188 | + test "LogStruct error handling can be disabled" do |
| 189 | + # Disable ActionMailer integration |
| 190 | + LogStruct.configure do |config| |
| 191 | + config.integrations.enable_actionmailer = false |
| 192 | + end |
| 193 | + |
| 194 | + result = LogStruct::Integrations::ActionMailer.setup(LogStruct.configuration) |
| 195 | + |
| 196 | + assert_nil result, "Setup should return nil when disabled" |
| 197 | + |
| 198 | + # Without LogStruct modules, errors should not be handled by LogStruct |
| 199 | + # but user rescue_from handlers will still work |
| 200 | + assert_nothing_raised do |
| 201 | + TestMailer.custom_error_during_delivery.deliver_now |
| 202 | + end |
| 203 | + |
| 204 | + # Only the rails default logger should have any output (not LogStruct structured) |
| 205 | + log_content = @log_output.string |
| 206 | + |
| 207 | + refute_match(/will retry/, log_content) |
| 208 | + end |
| 209 | + |
| 210 | + test "rescue_from handlers are checked in correct inheritance order" do |
| 211 | + # Create a complex inheritance chain |
| 212 | + base_mailer = Class.new(ActionMailer::Base) do |
| 213 | + default from: "base@example.com" |
| 214 | + include LogStruct::Integrations::ActionMailer::Callbacks |
| 215 | + |
| 216 | + rescue_from StandardError do |e| |
| 217 | + Rails.logger.info "Base handler: #{e.message}" |
| 218 | + raise e |
| 219 | + end |
| 220 | + end |
| 221 | + |
| 222 | + child_mailer = Class.new(base_mailer) do |
| 223 | + rescue_from ArgumentError do |e| |
| 224 | + Rails.logger.info "Child ArgumentError handler: #{e.message}" |
| 225 | + end |
| 226 | + |
| 227 | + rescue_from RuntimeError do |e| |
| 228 | + Rails.logger.info "Child RuntimeError handler: #{e.message}" |
| 229 | + raise e |
| 230 | + end |
| 231 | + |
| 232 | + def test_argument_error |
| 233 | + raise ArgumentError, "Invalid argument" |
| 234 | + end |
| 235 | + |
| 236 | + def test_runtime_error |
| 237 | + raise "Runtime problem" |
| 238 | + end |
| 239 | + end |
| 240 | + |
| 241 | + # Setup LogStruct |
| 242 | + LogStruct::Integrations::ActionMailer.setup(LogStruct.configuration) |
| 243 | + LogStruct::Integrations::ActionMailer::Callbacks.patch_message_delivery |
| 244 | + child_mailer.include LogStruct::Integrations::ActionMailer::ErrorHandling |
| 245 | + |
| 246 | + # ArgumentError should be caught by child's specific handler |
| 247 | + assert_nothing_raised do |
| 248 | + child_mailer.test_argument_error.deliver_now |
| 249 | + end |
| 250 | + assert_match(/Child ArgumentError handler: Invalid argument/, @log_output.string) |
| 251 | + |
| 252 | + @log_output.truncate(0) |
| 253 | + |
| 254 | + # RuntimeError should be caught by child handler, then reraise (not to base handler) |
| 255 | + # Note: Rails rescue_from doesn't bubble to parent handlers after reraise |
| 256 | + assert_raises(RuntimeError) do |
| 257 | + child_mailer.test_runtime_error.deliver_now |
| 258 | + end |
| 259 | + log_content = @log_output.string |
| 260 | + |
| 261 | + assert_match(/Child RuntimeError handler: Runtime problem/, log_content) |
| 262 | + # Base handler should NOT be called - Rails doesn't bubble to parent rescue_from after reraise |
| 263 | + refute_match(/Base handler: Runtime problem/, log_content) |
| 264 | + end |
| 265 | +end |
0 commit comments