diff --git a/.changeset/exclude-mime-negotiation-invalidtype.md b/.changeset/exclude-mime-negotiation-invalidtype.md new file mode 100644 index 0000000..e0529c7 --- /dev/null +++ b/.changeset/exclude-mime-negotiation-invalidtype.md @@ -0,0 +1,5 @@ +--- +'posthog-ruby': patch +--- + +posthog-rails: exclude `ActionDispatch::Http::MimeNegotiation::InvalidType` from captured exceptions by default. It is raised on malformed `Accept`/`Content-Type` headers (almost always scanner traffic) and mapped to a 406 by Rails, so it is noise rather than an app error. diff --git a/posthog-rails/lib/generators/posthog/templates/posthog.rb b/posthog-rails/lib/generators/posthog/templates/posthog.rb index 7a13aec..d5ac82d 100644 --- a/posthog-rails/lib/generators/posthog/templates/posthog.rb +++ b/posthog-rails/lib/generators/posthog/templates/posthog.rb @@ -130,6 +130,7 @@ # - ActionController::RoutingError # - ActionController::UnknownFormat # - ActionController::UnknownHttpMethod +# - ActionDispatch::Http::MimeNegotiation::InvalidType # - ActionDispatch::Http::Parameters::ParseError # - ActiveRecord::RecordNotFound # - ActiveRecord::RecordNotUnique diff --git a/posthog-rails/lib/posthog/rails/configuration.rb b/posthog-rails/lib/posthog/rails/configuration.rb index e755ebe..6f4b68e 100644 --- a/posthog-rails/lib/posthog/rails/configuration.rb +++ b/posthog-rails/lib/posthog/rails/configuration.rb @@ -56,6 +56,7 @@ def default_excluded_exceptions 'ActionController::RoutingError', 'ActionController::UnknownFormat', 'ActionController::UnknownHttpMethod', + 'ActionDispatch::Http::MimeNegotiation::InvalidType', 'ActionDispatch::Http::Parameters::ParseError', 'ActiveRecord::RecordNotFound', 'ActiveRecord::RecordNotUnique' diff --git a/spec/posthog/rails/configuration_spec.rb b/spec/posthog/rails/configuration_spec.rb new file mode 100644 index 0000000..b46a110 --- /dev/null +++ b/spec/posthog/rails/configuration_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'action_controller' +require 'action_dispatch' + +$LOAD_PATH.unshift File.expand_path('../../../posthog-rails/lib', __dir__) + +require 'posthog/rails/configuration' + +RSpec.describe PostHog::Rails::Configuration do + subject(:config) { described_class.new } + + describe '#should_capture_exception?' do + # MimeNegotiation::InvalidType is raised when a client sends a malformed + # Accept / Content-Type header (typically scanner traffic). Rails maps it + # to a 406 — see ActionDispatch::ExceptionWrapper.rescue_responses — so it + # is not a bug worth capturing. + { + 'ActionController::RoutingError' => ActionController::RoutingError.new('No route matches'), + 'ActionDispatch::Http::MimeNegotiation::InvalidType' => + ActionDispatch::Http::MimeNegotiation::InvalidType.new('"foo" is not a valid MIME type') + }.each do |name, exception| + it "excludes #{name} by default" do + expect(config.should_capture_exception?(exception)).to be false + end + end + + it 'captures application exceptions that are not in the excluded list' do + exception = StandardError.new('something broke') + expect(config.should_capture_exception?(exception)).to be true + end + + it 'honours user-supplied excluded_exceptions in addition to defaults' do + config.excluded_exceptions = ['MyApp::IgnorableError'] + stub_const('MyApp::IgnorableError', Class.new(StandardError)) + expect(config.should_capture_exception?(MyApp::IgnorableError.new)).to be false + # defaults still apply + expect(config.should_capture_exception?(ActionController::RoutingError.new('x'))).to be false + end + end +end