-
Notifications
You must be signed in to change notification settings - Fork 26
fix: Fix Railtie middleware insertion crashing on Rails initialization #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rafaeelaudibert
merged 8 commits into
PostHog:main
from
noreastergroup:johnnagro/issue-97/0
Feb 10, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a961dc4
fix: Fix Railtie middleware insertion crashing on Rails initialization
johnnagro 83ecaa0
fix: Prevent sending empty batches and handle non-JSON response bodies
johnnagro 0965e2d
fix: Use $current_url property so exception URL appears in PostHog UI
johnnagro ddeb7df
fix: Only include source context lines for in-app exception frames
johnnagro 21f7bd7
rubocop autocorrect applied
johnnagro 5b3eee8
chore: bump version to 3.5.2 and update CHANGELOG
johnnagro 5cb4ea3
Merge origin/main; bump to 3.5.3 and resolve CHANGELOG
johnnagro fe09e48
adding GH issues to CHANGELOG
johnnagro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module PostHog | ||
| VERSION = '3.5.2' | ||
| VERSION = '3.5.3' | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Minimal requires for testing the Railtie in isolation | ||
| require 'posthog' | ||
| require 'rails/railtie' | ||
|
|
||
| # The posthog-rails lib has its own gemspec and isn't in the default load path, | ||
| # so we add it manually for testing. | ||
| $LOAD_PATH.unshift File.expand_path('../../../posthog-rails/lib', __dir__) | ||
|
|
||
| # Load just enough of posthog-rails to define the Railtie. | ||
| # Middleware classes (CaptureExceptions, etc.) are only referenced inside | ||
| # initializer blocks, not at file-load time, so we don't need them here. | ||
| require 'posthog/rails/configuration' | ||
| require 'posthog/rails/railtie' | ||
|
|
||
| RSpec.describe PostHog::Rails::Railtie do | ||
| describe 'posthog.insert_middlewares initializer' do | ||
| it 'has insert_middleware_after accessible from initializer context' do | ||
| # Rails initializer blocks are executed via instance_exec on the Railtie | ||
| # instance (see railties/lib/rails/initializable.rb). This means `self` | ||
| # inside the block is the Railtie INSTANCE, not the class. | ||
| # | ||
| # Any method called without an explicit receiver in the block must be | ||
| # defined as an instance method (or delegated to one). | ||
| railtie = PostHog::Rails::Railtie.instance | ||
| expect(railtie).to respond_to(:insert_middleware_after) | ||
| end | ||
|
|
||
| it 'successfully calls insert_middleware_after when the initializer runs' do | ||
| # Stub the middleware constants referenced in the initializer block | ||
| stub_const('ActionDispatch::DebugExceptions', Class.new) | ||
| stub_const('ActionDispatch::ShowExceptions', Class.new) | ||
| stub_const('PostHog::Rails::RescuedExceptionInterceptor', Class.new) | ||
| stub_const('PostHog::Rails::CaptureExceptions', Class.new) | ||
|
|
||
| # Find the initializer by name | ||
| initializer = PostHog::Rails::Railtie.initializers.find { |i| i.name == 'posthog.insert_middlewares' } | ||
| expect(initializer).not_to be_nil | ||
|
|
||
| # During initialization, app.config.middleware is a MiddlewareStackProxy | ||
| # which only supports recording operations — NOT query methods like include?. | ||
| # The mock must reflect this accurately. | ||
| middleware_proxy = double('MiddlewareStackProxy', insert_after: true) | ||
| app = double('app', config: double('config', middleware: middleware_proxy)) | ||
|
|
||
| # Reproduce the exact execution context: the block is run via instance_exec | ||
| # on the Railtie instance, with the app passed as the block argument. | ||
| # This is how Rails runs initializer blocks internally. | ||
| railtie = PostHog::Rails::Railtie.instance | ||
| expect do | ||
| railtie.instance_exec(app, &initializer.block) | ||
| end.not_to raise_error | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a problem here, though. If
targetdoesn't exist then we won't insert our middleware, and that's a bad failure. I wonder if there's a better way. Will keep it like this for now, but I'll keep thinking about it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed - i opened #99 and we can think through a more robust approach