-
-
Notifications
You must be signed in to change notification settings - Fork 540
fix(rails): ensure Rails.error.set_context is not lost #3024
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
solnic
merged 4 commits into
master
from
2931-railserrorset_context-data-is-silently-lost-sentry-captures-before-the-rails-errorsubscriber-runs
Jul 27, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9c78141
fix(rails): ensure Rails.error.set_context is set in requests
solnic 4ec0aa4
fix(rails): ensure Rails.error.set_context is set in ActiveJob
solnic a822d41
refactor(rails): extract common serializer for AJ and Error Context
solnic 27b2ff6
refactor(rails): rename contexts to execution_context helper
solnic 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "sentry/rails/serializer" | ||
|
|
||
| module Sentry | ||
| module Rails | ||
| module ErrorReporterContext | ||
| SUPPORTS_EXECUTION_CONTEXT = Gem::Version.new(::Rails.version) >= Gem::Version.new("7.0.0") | ||
|
|
||
| if SUPPORTS_EXECUTION_CONTEXT | ||
| def execution_context | ||
| context = ::ActiveSupport::ExecutionContext.to_h | ||
| return {} if context.empty? | ||
|
|
||
| { "rails.error" => Sentry::Rails::Serializer.serialize(context) } | ||
| end | ||
| else | ||
| def execution_context | ||
| {} | ||
| end | ||
| end | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Sentry | ||
| module Rails | ||
| module Serializer | ||
| def self.serialize(value) | ||
| case value | ||
| when Range | ||
| serialize_range(value) | ||
| when Hash | ||
| value.transform_values { |item| serialize(item) } | ||
| when Array, Enumerable | ||
| value.map { |item| serialize(item) } | ||
| when ->(item) { item.respond_to?(:to_global_id) } | ||
| serialize_global_id(value) | ||
| else | ||
| value | ||
| end | ||
| end | ||
|
|
||
| def self.serialize_global_id(value) | ||
| value.to_global_id.to_s | ||
| rescue StandardError | ||
| value | ||
| end | ||
|
|
||
| def self.serialize_range(range) | ||
| return range.to_s if range.begin.nil? || range.end.nil? | ||
| return range.to_s if range.begin.is_a?(::ActiveSupport::TimeWithZone) | ||
|
|
||
| range.map { |item| serialize(item) } | ||
| end | ||
| 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
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,54 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| RSpec.describe Sentry::Rails::Serializer do | ||
| describe ".serialize" do | ||
| it "recursively serializes hash values" do | ||
| result = described_class.serialize(a: 1, b: { c: 2..3 }) | ||
|
|
||
| expect(result).to eq(a: 1, b: { c: [2, 3] }) | ||
| end | ||
|
|
||
| it "recursively serializes array elements" do | ||
| result = described_class.serialize([1, [2..3]]) | ||
|
|
||
| expect(result).to eq([1, [[2, 3]]]) | ||
| end | ||
|
|
||
| it "expands ranges into arrays" do | ||
| expect(described_class.serialize(1..3)).to eq([1, 2, 3]) | ||
| end | ||
|
|
||
| context "when the range is beginless" do | ||
| it "stringifies the range instead of expanding it" do | ||
| range = (..10) | ||
|
|
||
| expect(described_class.serialize(range)).to eq(range.to_s) | ||
| end | ||
| end | ||
|
|
||
| context "when the range is endless" do | ||
| it "stringifies the range instead of expanding it" do | ||
| range = (1..) | ||
|
|
||
| expect(described_class.serialize(range)).to eq(range.to_s) | ||
| end | ||
| end | ||
|
|
||
| context "when the range boundary is an ActiveSupport::TimeWithZone" do | ||
| it "stringifies the range instead of expanding it" do | ||
| zone = ActiveSupport::TimeZone["UTC"] | ||
| range = (zone.now - 1.day)...zone.now | ||
|
|
||
| expect(described_class.serialize(range)).to eq(range.to_s) | ||
| end | ||
| end | ||
|
|
||
| it "returns values without a serialization rule unchanged" do | ||
| object = Object.new | ||
|
|
||
| expect(described_class.serialize(object)).to equal(object) | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.